我正在从Java Swing
向select data
开发database
个应用程序。但我的Action Listener
无效。请帮我。执行程序时没有显示错误。请检查我的代码。
public class Availability implements ActionListener {
JPanel panelForMedicine,inputPanel;
JLabel mediLabel;
JComboBox listCombo;
JButton dbButton;
final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
final String DB_URL = "jdbc:mysql://localhost/paliative_care_unit";
final String USER = "root";
final String PASS = "root";
Connection conn = null;
Statement stmt=null;
static void guiBuilder(){
JFrame availabilityFrame=new JFrame("Check Stock");
availabilityFrame.setLocation(0, 0);
availabilityFrame.setSize(390,150);
availabilityFrame.setVisible(true);
availabilityFrame.setDefaultCloseOperation(availabilityFrame.DISPOSE_ON_CLOSE);
Availability availability=new Availability();
availabilityFrame.setContentPane(availability.mainPanel());
availability.mainPanel();
}
public JPanel mainPanel(){
JPanel availabilityPanel=new JPanel();
availabilityPanel.setLayout(null);
panelForMedicine=new JPanel();
//panelForMedicine.setLayout(null);
panelForMedicine.setSize(70, 40);
panelForMedicine.setLocation(30, 35);
availabilityPanel.add(panelForMedicine);
mediLabel=new JLabel("Medicine");
mediLabel.setLocation(0, 0);
mediLabel.setSize(70, 40);
panelForMedicine.add(mediLabel);
inputPanel=new JPanel();
inputPanel.setLayout(null);
inputPanel.setSize(150, 40);
inputPanel.setLocation(120,35);
availabilityPanel.add(inputPanel);
listCombo=new JComboBox();
listCombo.setSize(150, 30);
listCombo.setLocation(0, 0);
inputPanel.add(listCombo);
try{
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql="SELECT DISTINCT medi FROM paliative";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
listCombo.addItem(rs.getString("medi"));
//medicineField.setSelectedItem("attempt11");
}
}
catch(Exception e){System.out.println(e);}
dbButton=new JButton("GO");
dbButton.setSize(70, 30);
dbButton.setLocation(300, 35);
dbButton.addActionListener(this);
availabilityPanel.add(dbButton);
availabilityPanel.setOpaque(true);
return availabilityPanel;
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==dbButton){
System.out.println("Checking");
try{
System.out.println("Checking");
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql="SELECT * FROM paliative WHERE medi='"+listCombo.getSelectedItem().toString()+"'";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
}
}
catch (Exception ex) {
JFrame dialogFrame=new JFrame();
JOptionPane.showMessageDialog(dialogFrame, ex);
}
}
}
}
答案 0 :(得分:1)
正如@nicE cOw指出的那样 - 您在availability.mainPanel()
方法中拨打guiBuilder()
两次。这会在dbButton
变量添加到您的contentPane后更改您对if(e.getSource()==dbButton)
变量的引用,这会使availability.mainPanel();
条件在您的动作侦听器中失败
解决方案:移除guiBuilder()
此外: 请阅读Swing线程。您不应该在Event Dispatch Thread中执行长时间运行的操作(如数据库调用)。首先尝试Concurrency In Swing。