我有三个Java类:
我使用setter设置值,然后从组合框类中设置值。
现在,我想在另一个班级中获得这个价值。
这是我的代码:
public class Settings {
private static String RootName;
public static void setRootName(String rootName){
RootName = rootName;
}
public static String getRootName(){
return RootName;
}
}
combobox.java
public class ComboBoxDemo extends JPanel
implements ActionListener {
String connectionURL = "jdbc:mysql://localhost:3306/Trainpis";
JLabel picture;
public static String i="hello";
public String rootname;
public ComboBoxDemo() {
JComboBox combo=new JComboBox();
combo.addActionListener(this);
JFrame f=new JFrame();
JPanel p=new JPanel();
try{
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection conn = DriverManager.getConnection(connectionURL, "root", "");
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("Select route from route");
while(rs.next()){
combo.addItem(rs.getString("route"));
//System.out.println(rs.getString("route"));
}
}
catch(Exception e){}
p.add(combo);
f.add(p);
f.setExtendedState(JFrame.MAXIMIZED_BOTH);
f.setUndecorated(true);
f.setVisible(true);
}
/** Listens to the combo box. */
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox)e.getSource();
String selectedRoute = (String)cb.getSelectedItem();
// System.out.println(rootname);
String root1="Huda City Center - Noida City Center";
if(selectedRoute.equalsIgnoreCase(root1))
{
System.out.println("hello");
//new Test();
//Settings mysettings = new Settings();
Settings.setRootName(selectedRoute);
RootSelection1 r1 = new RootSelection1();
r1.print();
}
else{
System.out.println("bye");
}
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ComboBoxDemo();
}
});
}
}
现在我想使用所选的组合框值:
String rootSelection = Settings.getRootName();
String selectStoredProc = "SELECT sino,stationname,distance from station where route ='"+rootSelection+"'";
String [] root;
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(connectionURL, "root", "");
PreparedStatement ps = conn.prepareStatement(selectStoredProc);
ResultSet rs=ps.executeQuery();
while(rs.next()){
String s1=rs.getString("stationname");
nameList.add(s1);
root = nameList.toArray(new String[nameList.size()]);
}
}
catch(Exception e){}
我想在组合框的所选项目上完成所有这些操作。
我怎样才能做到这一点?
答案 0 :(得分:0)
静态变量是类级别。所以你在第二次调用中改变了第一类静态变量。因此,您可以访问第三类中的第一个类变量。你可以通过使用 Settings.getRootName();在你的第三节课
答案 1 :(得分:0)
在第二节课中创建一个Settings
对象
public void actionPerformed(ActionEvent e) {
JComboBox comboBox = (JComboBox)e.getSource();
String selectedRoute = (String)comboBox.getSelectedItem();
RootSelection r1=new RootSelection();
//Settings object
Settings mySettings = new Settings();
mySettings.setRootName(selectedRoute);
}
然后将此对象传递给第三个类中的方法
旁注:
你不应该让你的Settings
课程充满静态的东西
E.g。这是更好的
public class Settings {
private String RootName;
public void setRootName(String rootName){
RootName = rootName;
}
public String getRootName(){
return RootName;
}
}