【问题标题】:Button's ActionPerformed not working from another classButton 的 ActionPerformed 不能在另一个类中工作
【发布时间】:2013-11-26 23:23:20
【问题描述】:

我有两个类,一个 GUI 和一个 Customer 类。 GUI 构建框架和按钮,Customer 类从数据库服务器获取信息。我在 Customer 类中有 ActionPerformed,但是当我单击按钮时它似乎没有引起注意。非常感谢任何帮助!

图形界面类:

class GUI { 

private static JFrame frame;
public static JTextField textField;
public static JButton btnGetInfo = new JButton("Get Info");
public static JTextArea textArea = new JTextArea();

  public static void main (String args []) 
  throws SQLException { 

DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());


GUI window = new GUI();


frame = new JFrame();
frame.setBounds(100, 100, 630, 470);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);


textArea.setEditable(false);
textArea.setLineWrap(true);
textArea.setBounds(10, 179, 594, 241);
frame.getContentPane().add(textArea);


textField = new JTextField();
textField.setBounds(255, 69, 86, 20);
frame.getContentPane().add(textField);
textField.setColumns(10);


JButton btnClearScreen = new JButton("Clear Screen");
btnClearScreen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

        textArea.setText("");

    }
});


btnClearScreen.setBounds(492, 11, 112, 23);
frame.getContentPane().add(btnClearScreen);


JLabel lblEnterCustomerId = new JLabel("Enter Customer ID (1-6)");
lblEnterCustomerId.setBounds(240, 43, 153, 14);
frame.getContentPane().add(lblEnterCustomerId);


btnGetInfo.setBounds(255, 115, 89, 23);
frame.getContentPane().add(btnGetInfo);


window.frame.setVisible(true);

} 
} 

客户类别:

public class Customer implements ActionListener {

public static void getInfoButton() {

GUI.btnGetInfo.addActionListener(new ActionListener() {


    public void actionPerformed(ActionEvent arg0) {

        System.out.println("TEST");

        String getCustID = GUI.textField.getText();
        String query = ("select * from customers where CUSTID =" + getCustID);

        final String user, pass;

        user = "asdf";
        pass = "asdf";


        try{

            Connection conn = DriverManager.getConnection
                    ("jdbc:oracle:thin:@asdf",user,pass);
                    Statement stmt = conn.createStatement (); 

            ResultSet rset = stmt.executeQuery (query);

            while (rset.next ()) 
                { 

                    GUI.textArea.append((rset.getString("CUSTID") + "  -  " + rset.getString("CUSTSSN")
                    + "  -  " + rset.getString("LNAME") + "  -  " + rset.getString("FNAME") + "  -  " +
                    rset.getString("STREET") + "  -  " + rset.getString("CITY") + "  -  " + rset.getString("STATE") +
                    "  -  " + rset.getString("ZIP") + "  -  " + rset.getString("PHONE") + System.getProperty("line.separator")
                    + System.getProperty("line.separator")));

                }    


        }

            catch (SQLException e)
        {
            System.out.println ("Could not load the db"+e); 
        }

    }});


}

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub

}

}

【问题讨论】:

  • 你真的不应该使用静态变量来保存对 GUI 组件的引用——这就像自找麻烦。
  • 好的。但这不是我遇到的问题。

标签: java button user-interface actionlistener


【解决方案1】:
public static JButton btnGetInfo = new JButton("Get Info");

你应该把 actionlistener 添加到这个

btnGetInfo.addActionListener(new Customer());

您的Customer 应该是这样的

public class Customer implements ActionListener {
    // delete the uneccessary
    @Override
    public void actionPerformed(ActionEvent arg0) {
        ...
    }
}

不必要的代码

public static void getInfoButton() {

GUI.btnGetInfo.addActionListener(new ActionListener() {

【讨论】:

    猜你喜欢
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    相关资源
    最近更新 更多