2012-11-12 274 views
-1

我需要在Java中对以下情况下小码:获取复选框值

按钮应该得到的选择复选框,并在我的形式执行代码 这些复选框。

+2

请执行尝试自己。指针:[Swing教程](http://docs.oracle.com/javase/tutorial/uiswing/components/index.html),'JButton#setAction'或'JButton#addActionListener','JCheckbox#isSelected' – Robin

+0

这里:http://stackoverflow.com/questions/1509682/how-to-check-the-status-of-checkbox-in-java-gui – WilliamShatner

回答

9

这是我为你做一个例子:

enter image description here

enter image description here

import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JCheckBox; 
import javax.swing.JFrame; 
import javax.swing.JOptionPane; 
import javax.swing.SwingUtilities; 

public class TestJCheckBox { 

    private JFrame frame; 
    private JCheckBox jcb; 
    private JButton button; 

    public TestJCheckBox() { 
     initComponents(); 
    } 

    private void initComponents() { 
     frame = new JFrame("Test"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setResizable(false); 

     jcb = new JCheckBox("JCheckBox1"); 

     button = new JButton("Is JCheckBox selected?"); 
     button.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent ae) { 
       if (jcb.isSelected()) { 
        JOptionPane.showMessageDialog(frame, "JCheckBox is selected"); 
       } else { 
        JOptionPane.showMessageDialog(frame, "JCheckBox is NOT selected"); 

       } 
      } 
     }); 

     frame.add(jcb, BorderLayout.CENTER); 
     frame.add(button, BorderLayout.SOUTH); 

     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new TestJCheckBox(); 
      } 
     }); 
    } 
} 
+1

ThanQ。我认为这对我的问题是适当的。抱歉的人,我甚至没有时间来编码的东西。无论如何,我很高兴与您的关注。 – Raj

+1

@Raj它的乐趣..作为一个有用的提示不要急于或没有SSCCE提问,因为他们有可能相对较快地关闭 - 就像这一个。在转向Q&A之前,请尝试做自己的工作,对涉及的单独主题的快速谷歌不会伤害:)。 –