2013-03-27 41 views
1

我在學習java Swing,並從線上教程中獲得了此演示代碼。一旦你選擇了相應的複選框,你可以顯示4個不同顏色的面板(紅色,藍色,綠色,黃色)。 但是如果我想從6(「紅色」,「藍色」,「綠色」,「黃色」,「黑色」,「白色」)複選框中選擇4種顏色,如何根據此演示進行修改?關於選中複選框的顯示面板

import javax.swing.*; 
import java.awt.*; 
import java.awt.Color; 
import java.awt.event.*; 

public class CheckBoxExample_UltraExtended implements ActionListener{ 

    JCheckBox redCB, blueCB, greenCB, yellowCB; 
    JPanel redBox, blueBox, greenBox, yellowBox; 
    JButton refresh; 

    public JPanel createContentPane(){ 

     JPanel totalGUI = new JPanel(); 

     // We create four checkboxes to control what is currently on-screen. 
     // At the start, we set the red checkbox to 'ticked' or selected. 

     JPanel checkBoxPanel = new JPanel(); 
     checkBoxPanel.setLayout(new BoxLayout(checkBoxPanel, BoxLayout.PAGE_AXIS)); 
     checkBoxPanel.add(Box.createRigidArea(new Dimension(10,0))); 

     redCB = new JCheckBox("Red"); 
     redCB.setSelected(true); 

     checkBoxPanel.add(redCB); 
     checkBoxPanel.add(Box.createHorizontalGlue()); 

     blueCB = new JCheckBox("Blue");   
     checkBoxPanel.add(blueCB); 
     checkBoxPanel.add(Box.createHorizontalGlue()); 

     greenCB = new JCheckBox("Green"); 
     checkBoxPanel.add(greenCB); 
     checkBoxPanel.add(Box.createHorizontalGlue()); 

     yellowCB = new JCheckBox("Yellow"); 
     checkBoxPanel.add(yellowCB); 
     checkBoxPanel.add(Box.createRigidArea(new Dimension(10, 0))); 

     // Now we create a simple JPanel that displays our four coloured boxes. 

     JPanel boxPanel = new JPanel(new GridLayout(2, 2, 20, 20)); 

     redBox = createSquareJPanel(Color.red, 50); 
     blueBox = createSquareJPanel(Color.blue, 50); 
     greenBox = createSquareJPanel(Color.green, 50); 
     yellowBox = createSquareJPanel(Color.yellow, 50); 

     // This sets all bar the red box to be hidden. 

     blueBox.setVisible(false); 
     greenBox.setVisible(false); 
     yellowBox.setVisible(false); 

     boxPanel.add(redBox); 
     boxPanel.add(blueBox); 
     boxPanel.add(greenBox); 
     boxPanel.add(yellowBox); 

     // Now lets quickly add a refresh button with an actionListener to determine 
     // when it is pressed. 

     refresh = new JButton("Refresh"); 
     refresh.addActionListener(this); 

     totalGUI.add(checkBoxPanel); 
     totalGUI.add(boxPanel); 
     totalGUI.add(refresh); 

     totalGUI.setOpaque(true); 
     return totalGUI; 
    } 

    // In this method, we create a square JPanel of a colour and set size 
    // specified by the arguments. 

    private JPanel createSquareJPanel(Color color, int size) { 
     JPanel tempPanel = new JPanel(); 
     tempPanel.setBackground(color); 
     tempPanel.setMinimumSize(new Dimension(size, size)); 
     tempPanel.setMaximumSize(new Dimension(size, size)); 
     tempPanel.setPreferredSize(new Dimension(size, size)); 
     return tempPanel; 
    } 

    // This actionPerformed simply takes sets the visibility of each 
    // coloured box to the state of each checkbox. 

    public void actionPerformed(ActionEvent e) { 

     if(e.getSource() == refresh) 
     { 
      redBox.setVisible(redCB.isSelected()); 
      blueBox.setVisible(blueCB.isSelected()); 
      greenBox.setVisible(greenCB.isSelected()); 
      yellowBox.setVisible(yellowCB.isSelected()); 
     } 
    } 


    private static void createAndShowGUI() { 

     JFrame.setDefaultLookAndFeelDecorated(true); 
     JFrame frame = new JFrame("[=] JCheckBox [=]"); 

     CheckBoxExample_UltraExtended demo = new CheckBoxExample_UltraExtended(); 
     frame.setContentPane(demo.createContentPane()); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

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

你想限制更多然後4檢查選擇盒子? – 2013-03-27 07:57:43

+0

是的,6種顏色的選擇,但只顯示不超過4個面板。 – user2209981 2013-03-27 08:01:11

回答

3

你必須addActionListener的複選框,這樣。

private變量numberOfCheckedBoxes來跟蹤選中的複選框的編號。

private numberOfCheckedBoxes = 0; 

現在,當您創建複選框,增加的動作監聽這樣

redCB = new JCheckBox("Red"); 
redCB.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent e){ 
     if (numberOfCheckedBoxes == 4){ 
      JOptionMessage.showMessageDialog("Can not select more than 4 check boxes"); 
      return; 
     } 
     if (redCB.isSelected()){ 
      increaseNumberOfCheckedBoxes(); 
     }else{ 
      reduceNumberOfCheckedBoxes(); 
     } 
    } 
}); 

這你必須爲你創建的所有複選框做。現在是其他所需的方法。

public void increaseNumberOfCheckedBoxes(){ 
    numberOfCheckedBoxes ++; 
} 
public void reduceNumberOfCheckedBoxes(){ 
    numberOfCheckedBoxes --; 
} 

另一種方法是,創建一個方法handleCheckBoxSelection

public handleCheckBoxSelection(boolean isSelected){ 
    if (numberOfCheckedBoxes == 4){ 
     JOptionMessage.showMessageDialog("Can not select more than 4 check boxes"); 
     return; 
    } 
    if (isSelected){ 
     increaseNumberOfCheckedBoxes(); 
    }else{ 
     reduceNumberOfCheckedBoxes(); 
    } 
} 

,然後同時初始化複選框,調用這個函數

redCB = new JCheckBox("Red"); 
redCB.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent e){ 
     handleCheckBoxSelection(redCB.isSelected()); 
    } 
}); 
+0

刪除了我的最後一句話並更新了一種新方法。 – 2013-03-27 10:52:08