我有两个类,一个GUI和一个Customer类。 GUI构建框架和按钮,Customer类从数据库服务器获取信息。我在Customer类中有ActionPerformed,但是当我单击按钮时它似乎没有注意到。非常感谢任何帮助!
GUI类:
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
}
}
答案 0 :(得分: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() {