0

我有两个类,一个 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

}

}
4

1 回答 1

0
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() {
于 2013-11-14T05:43:20.013 回答