简体   繁体   English

在Java中将值从一类设置为另一类

[英]Setting value from one class to another in Java

I have got three Java classes: 我有三个Java类:

  • The First has a setter and a getter 第一个有一个setter和一个getter
  • The Second has a JComboBox 第二个有一个JComboBox
  • The third has a MySQL query and generating list. 第三个具有MySQL查询和生成列表。

I used the setter to set the value and I set the value from the combo box class. 我使用了设置器来设置值,并从组合框类中设置了值。

Now, I would like to get this value in another class. 现在,我想在另一个类中获得此值。

Here is my code: 这是我的代码:

public class Settings {

    private static String RootName;

    public static void setRootName(String rootName){
     RootName = rootName;
    }

    public static String getRootName(){
     return RootName;
    }
}

combobox.java 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();
            }
        });
    }
}

Now I want to use the selected value of combo box: 现在,我要使用组合框的选定值:

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){}

I want to do all of this on combo box's selected item. 我想对组合框的选定项目执行所有这些操作。

How can I achieve this? 我该如何实现?

Static variables are class levels. 静态变量是类级别。 So You are changing the fist class static variable in second calls. 因此,您将在第二次调用中更改fist类的静态变量。 So you can access the first class variable in third class. 因此,您可以访问第三类中的第一类变量。 You can get by using Settings.getRootName(); 您可以使用Settings.getRootName();获得。 in your third class 在你的三等班

In your second class create a Settings object 在第二堂课中,创建一个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);
}

Then pass this object to your method in your third class 然后将此对象传递给第三类中的方法

A side note: 旁注:

you shouldn't make your Settings class full of static things 你不应该让你的Settings类充满静态的东西

Eg This is better 例如,这更好

public class Settings {

    private String RootName;

    public void setRootName(String rootName){
     RootName = rootName;
    }

    public String getRootName(){
     return RootName;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM