2010-02-09 74 views
0

我是新來的鞦韆,我想問一下如何製作這段代碼,所以當提交按鈕被按下以獲得選定的複選框並顯示它們的名字時。selected複選框

import java.awt.BorderLayout; 
import java.awt.Container; 
import java.awt.GridLayout; 

import javax.swing.BorderFactory; 
import javax.swing.JButton; 
import javax.swing.JCheckBox; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.border.Border; 

public class ACheckBox { 
    public static void main(String args[]) { 
    String title = (args.length == 0 ? "CheckBox Sample" : args[0]); 
    JFrame frame = new JFrame(title); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    JPanel panel = new JPanel(new GridLayout(0, 1)); 
    Border border = BorderFactory.createTitledBorder("Pizza Toppings"); 
    panel.setBorder(border); 
    JCheckBox check = new JCheckBox("Anchovies"); 
    panel.add(check); 
    check = new JCheckBox("Garlic"); 
    panel.add(check); 
    check = new JCheckBox("Onions"); 
    panel.add(check); 
    check = new JCheckBox("Pepperoni"); 
    panel.add(check); 
    check = new JCheckBox("Spinach"); 
    panel.add(check); 
    JButton button = new JButton("Submit"); 
    Container contentPane = frame.getContentPane(); 
    contentPane.add(panel, BorderLayout.CENTER); 
    contentPane.add(button, BorderLayout.SOUTH); 
    frame.setSize(300, 200); 
    frame.setVisible(true); 
    } 
} 

回答

4

您需要定義一個定製ActionListener實現他們的工作是分析每個JCheckBox的,當一個動作被觸發選中狀態;即當點擊「提交」按鈕時。

// ActionListener implementation that maintains a reference to each JCheckBox. 
// We will register this listener with the Submit button. 
public class MyActionListener implements ActionListener { 
    private final List<JCheckBox> checkBoxes = new LinkedList<JCheckBox>(); 

    /** 
    * Adds the specified JCheckBox to the list of JCheckBoxes. 
    */ 
    public void addCheckBox(JCheckBox checkBox) { 
    this.checkBoxes.add(checkBox); 
    } 

    /** 
    * Called when the Submit button is pressed. 
    */ 
    public void actionPerformed(ActionEvent evt) { 
    StringBuilder sb = new StringBuilder(); 
    sb.append("Selected Check Boxes: "); 

    // Iterate over each JCheckBox and build message ready for display. 
    // Could do something more sophisticated here if required. 
    for (JCheckBox checkBox : checkBoxes) { 
     if (checkBox.isSelected()) { 
     sb.append(checkBox.getText()).append(' '); 
     } 
    } 

    JOptionPane.showMessageDialog(null, sb); 
    } 
} 

// 1. Create ActionListener implementation. 
MyActionListener al = new MyActionListener(); 

// 2. Register JCheckBoxes with ActionListener. 
al.addCheckBox(checkBox); 
// etc. 

// 3. Finally register ActionListener with Submit button. 
submitButton.addActionListener(al); 
1

如果你不希望創建一個新的Action每一個複選框(例如,如果複選框被動態添加),你可以做這樣的事情:

for (Component child: panel.getComponents()) { 
     if (child instanceof JCheckBox) { 
      JCheckBox checkBox = (JCheckBox) child; 
      if (checkBox.isSelected()) { 
       System.out.println(checkBox.getAction().getValue(Action.NAME)); 
      } 
     } 
    }