我正在创建一个简单的程序,用户可以在其中勾选不同颜色的复选框。如果在多个方框中打勾,则应显示颜色的混合。示例蓝色和黄色被打勾,然后标签显示绿色。 我有3个问题。
class LabDemo extends JFrame implements ActionListener {
JLabel displayColor = new JLabel("No chosen color");
JCheckBox blue = new JCheckBox("Blue");
JCheckBox yellow = new JCheckBox("Yellow");
JCheckBox red = new JCheckBox("Red");
JPanel panel = new JPanel();
public LabDemo() {
panel.setLayout(new GridLayout(4,1));
blue.addActionListener(this);
yellow.addActionListener(this);
red.addActionListener(this);
this.add(panel);
displayColor.setBackground(Color.WHITE);
panel.add(blue); panel.add(yellow); panel.add(red); panel.add(displayColor);
setSize(300,300);
setLocation(100,100);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
if (blue.isSelected()) {
displayColor.setText("Blue");
panel.setBackground(Color.BLUE);
}
else if (yellow.isSelected()) {
displayColor.setText("Yellow");
panel.setBackground(Color.YELLOW);
}
else if (red.isSelected()) {
displayColor.setText("Red");
panel.setBackground(Color.RED);
}
else if (blue.isSelected() && yellow.isSelected()) {
displayColor.setText("Blue + Yellow = Green");
panel.setBackground(Color.GREEN);
}
}
}
答案 0 :(得分:5)
您永远不会输入最后一种情况(蓝色和黄色),因为您使用的是if-else,并且上述语句之一将在之前被评估为true(仅蓝色是) , 在这种情况下)。如果只是这四个案例,则应首先检查“蓝色和黄色”。
答案 1 :(得分:2)
另一个选项:为每个选中的选项分配一个二进制位值,将三个复选框的值求和,并在该总和上switch
:
switch ((red.isSelected() ? 1 : 0) +
(green.isSelected() ? 2 : 0) +
(blue.isSelected() ? 4 : 0))
{
case 0: // none selected
case 1: // RED selected
case 2: // GREEN selected
case 3: // RED + GREEN
case 4: // BLUE
case 5: // BLUE + RED
case 6: // BLUE + GREEN
case 7: // RED + BLUE + GREEN
}
答案 2 :(得分:0)
正如我在评论中提到的,最好使用红色,绿色和蓝色,因为您要处理的是加色,例如光束的颜色,而不是减色,例如油漆的颜色。如果要处理减色,那么红色,黄色和蓝色将是合适的,但同样,这里不是这种情况。
解决方案的另一种选择是使用枚举来代表您的颜色,如下所示:
import java.awt.Color;
public enum MyColor {
RED("Red", Color.RED),
GREEN("Green", Color.GREEN),
BLUE("Blue", Color.BLUE);
private String name;
private Color color;
private MyColor(String name, Color color) {
this.name = name;
this.color = color;
}
public String getName() {
return name;
}
public Color getColor() {
return color;
}
}
然后,我们可以通过MyColor.values()
获取这些对象的数组,并将其用于创建JCheckBoxes。如果将复选框放入地图等集合中,则当一个复选框的状态更改时,我们可以检查所有复选框的状态:
private Map<MyColor, JCheckBox> colorMap = new EnumMap<>(MyColor.class);
private class CheckListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// initial individual color values
int r = 0;
int g = 0;
int b = 0;
// iterate through the check boxes, seeing which are selected
for (MyColor myColor : MyColor.values()) {
// if selected, extract its colors and add to rgb values
if (colorMap.get(myColor).isSelected()) {
r += myColor.getColor().getRed();
g += myColor.getColor().getGreen();
b += myColor.getColor().getBlue();
}
}
// let's avoid going beyond the maximum values allowed
r = Math.min(r, 255);
g = Math.min(g, 255);
b = Math.min(b, 255);
// create a color with the values and set the JPanel
Color color = new Color(r, g, b);
colorPanel.setBackground(color);
}
}
GUI可能如下所示:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EnumMap;
import java.util.Map;
import javax.swing.*;
@SuppressWarnings("serial")
public class ColorExample extends JPanel {
private JPanel colorPanel = new JPanel();
private Map<MyColor, JCheckBox> colorMap = new EnumMap<>(MyColor.class);
public ColorExample() {
colorPanel.setPreferredSize(new Dimension(400, 300));
colorPanel.setBackground(Color.BLACK);
JPanel gridPanel = new JPanel(new GridLayout(1, 0));
CheckListener checkListener = new CheckListener();
for (MyColor myColor : MyColor.values()) {
JCheckBox checkBox = new JCheckBox(myColor.getName());
checkBox.addActionListener(checkListener);
colorMap.put(myColor, checkBox);
gridPanel.add(checkBox);
}
setLayout(new BorderLayout());
add(colorPanel);
add(gridPanel, BorderLayout.PAGE_END);
}
private class CheckListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// initial individual color values
int r = 0;
int g = 0;
int b = 0;
// iterate through the check boxes, seeing which are selected
for (MyColor myColor : MyColor.values()) {
// if selected, extract its colors and add to rgb values
if (colorMap.get(myColor).isSelected()) {
r += myColor.getColor().getRed();
g += myColor.getColor().getGreen();
b += myColor.getColor().getBlue();
}
}
// let's avoid going beyond the maximum values allowed
r = Math.min(r, 255);
g = Math.min(g, 255);
b = Math.min(b, 255);
// create a color with the values and set the JPanel
Color color = new Color(r, g, b);
colorPanel.setBackground(color);
}
}
private static void createAndShowGui() {
ColorExample mainPanel = new ColorExample();
JFrame frame = new JFrame("Color Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}