【发布时间】:2014-01-11 18:43:27
【问题描述】:
在 JCombobox 中有可能的火车路线。我想根据事件处理程序获取 JCombobox 的值,然后在使用数据库的类中使用它。这个值将是一个Mysql查询的参数,我已经获取成功了,但是不能使用。我在java方面不是很有经验,我做错了什么。我在网站上搜索过类似的问题,看到了但没有清楚地理解它们。我错过了什么?
//imports...
public class Cashier extends JFrame{
//here is main...
public Cashier(){
//some Window code...
final String[] Routes = {"--Select--", "value1",....."valueN" };
final JComboBox comboBox = new JComboBox(Routes);
comboBox.addActionListener(new ActionListener() {
/*-->*/ public String d;
public void WhereTo(String dest){
this.d=dest;
System.out.println(d);
// comes out correct!
/*I want d for use in DBaccess class as query parameter, by invoking
GetRoute()*/
}
public void actionPerformed(ActionEvent e) {
int val = comboBox.getSelectedIndex();
this.d= Routes[val];
WhereTo(d);
}
});
comboBox.setBounds(165, 124, 130, 23);
contentPane.add(comboBox);
//this method will be used by DBaccess
public String GetRoute(){
return d;
}
//More Objects...
}
}
这是我的 DBaccess 类,我想在其中使用字符串 d,可能是通过调用 Cashier 的 Get Route()。
public DBaccess extends Cashier{
//connection code....
// Executing the query
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
//probably like this...
String go = Cashier.GetRoute();
sql = "SELECT FROM reservations WHERE destination='"+go+"'";
ResultSet rs = stmt.executeQuery(sql);
}
【问题讨论】:
-
请尝试描述您的确切问题。你的代码编译了吗?它运行吗?它会产生结果吗?此结果与您的预期不同吗?
-
neoprez ---我如你所说的那样声明,但也必须使其成为静态,包括 GetRoute。是的,它现在编译!但是在 DBaccess 中该值为 null 。无法查询!
-
总而言之,我正在尝试将 d 传递给 DBaccess 以进行查询
-
Java GUI 可能必须在多个平台、不同的屏幕分辨率和使用不同的 PLAF 上工作。因此,它们不利于组件的精确放置。要为强大的 GUI 组织组件,请改用布局管理器或 combinations of them,以及 white space 的布局填充和边框。