0

我正在开发一个Java Swing应用程序来select datadatabase。但我的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);
            }
        }
    }
}
4

1 回答 1

1

正如@nicE cOw 指出的那样-您availability.mainPanel()在方法中调用了两次guiBuilder()。这将在变量添加到您的 contentPane 后更改您对dbButton变量的引用,这将使if(e.getSource()==dbButton)条件在您的操作侦听器中失败

解决方案:删除第二个调用availability.mainPanel();inguiBuilder()

另外:请阅读 Swing 线程。您不应该在事件调度线程中执行长时间运行的操作(如数据库调用)。初学者可以试试Swing 中的并发。

于 2013-08-04T15:35:03.753 回答