我正在学习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 :(得分: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());
}
});