Java Swing显示按钮2X 1次点击 - 为什么? 0_o

时间:2014-12-26 21:19:00

标签: java swing jcheckbox

我编写了一个Java Swing组件,它在单击时显示一个按钮两次。但是,我只希望按钮在单击时出现一次。我的代码如下。有谁知道我错过了什么?

import java.awt.event.*;



public class checkIt2 implements ItemListener { 

    public void itemStateChanged(ItemEvent e) { 


   if(e.getStateChange() == ItemEvent.SELECTED) { 



         javax.swing.JOptionPane.showMessageDialog(null, "Check box 2 is selected");
         JDBC_Demo6.check2.setSelected(true);


    } 

    if (e.getStateChange() == ItemEvent.DESELECTED) { 

        JDBC_Demo6.check1.setSelected(false);
    }




    }

}

以下是JDBC_Demo6类中与JCheckBox ItemListener类相关的代码:

    public class JDBC_Demo6 extends JFrame implements ActionListener {

    public static JTextArea textArea;
    static String critical_data; 
    private final static String newline = "\n";
    public static JCheckBox check1;
    public static JCheckBox check2;



public JDBC_Demo6(String s) {
    super(s);

}

public void addComponentsToPane(final Container pane) { 

    textArea = new JTextArea(20, 75);

   /* final JPanel compsToExperiment = new JPanel(new GridBagLayout()); */

    // GridBagConstraint for button 
    /* This puts the buttons in the very center of the GUI */




    //get Database data 
    JsonDB a = new JsonDB();

    //store the output from the getDB method in this ArrayList collection 
    ArrayList<String> tmp = a.getDB();

    //loop through the ArrayList and append each string to the text area
    JScrollPane scrollPane = new JScrollPane();
    //add a scrollpane to the GUI
    //scrollPane.setSize(new Dimension(100, 100));


    /* loop through the ArrayList with the results of the SQL query and then add them to the JPanel  */
    for(String s: tmp) {

        textArea.append("From the database -> " + s + "\n");
    }


    scrollPane.setViewportView(textArea);
    this.add(scrollPane);
    scrollPane.updateUI();

    /* add buttons */ 
    JButton jB = new JButton("Send To ESB");
    JButton jB2 = new JButton("Cancel");

    jB.addActionListener(new goButton());
    jB2.addActionListener(new cancelButton());
    this.add(jB);
    this.add(jB2);

    /* check boxes */ 
    check1 = new JCheckBox("Check Box 1");
    check2 = new JCheckBox("Check Box 2");

    check1.addItemListener(new checkIt());
    check2.addItemListener(new checkIt2());

    this.add(check1);
    this.add(check2);



}



/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event dispatch thread.
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JDBC_Demo6 frame = new JDBC_Demo6("JDBC Example #6");
    frame.setSize(50, 50);
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Add contents to the window.
    frame.addComponentsToPane(frame.getContentPane());

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    //Schedule a job for the event dispatch thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}

public void actionPerformed(ActionEvent ae) { 

}

} // end class 

2 个答案:

答案 0 :(得分:0)

使用check1check2

import java.awt.event.*;

public class checkIt2 implements ItemListener { 

    public void itemStateChanged(ItemEvent e) { 

        if(e.getStateChange() == ItemEvent.SELECTED) { 

            javax.swing.JOptionPane.showMessageDialog(null, "Check box 2 is selected");
            JDBC_Demo6.check1.setSelected(true);

        } 

        if (e.getStateChange() == ItemEvent.DESELECTED) { 

            JDBC_Demo6.check1.setSelected(false);
        }

    }

}

答案 1 :(得分:0)

发生了两次,因为来电JDBC_Demo6.check1.setSelected(true);会触发对ItemListener.itemStateChanged()的另一次调用。在这种情况下,使用ActionLister或ChangeLister而不是ItemListener会更容易,就像在后面的代码中一样。虽然调用JDBC_Demo6.check1.setSelected(true);不会触发另一个ActionListener.actionPerformed(),就像它对ItemListeners一样,但调用是多余的,你应该删除它。
测试此ActionListener以查看它是否适合您:

    public class checkIt2 implements ActionListener { 

        @Override
        public void actionPerformed(ActionEvent e) {
            javax.swing.JOptionPane.showMessageDialog(null, "selected: " +
                    ((JCheckBox)e.getSource()).isSelected());
        }

    }

并使用它来注册听众:

    check2.addActionListener(new checkIt2());