【问题标题】:Using mapTree collection and need more colors使用 mapTree 集合并需要更多颜色
【发布时间】:2014-10-10 20:31:01
【问题描述】:

这是针对学校的一个项目,我有两个不同的问题想请教。以下是老师的要求:

“使用 Java 集合来存储成对的唯一颜色及其唯一的十六进制值。最多可以存储 20 个这样的对。然后编写一个 GUI,使用单选按钮选择一个值来显示颜色和/或十六进制值。选择时GUI 的背景应更改为该颜色。”

我读到这意味着将它们成对存储然后制作一个使用 JRadioButtons 更改背景颜色的 GUI。

我已经完成并且工作正常。我的问题是我真的觉得应该有一种更有效的方法来从treeMap制作jrb。我正在考虑调用下一组来填充 jrb,类似于使用数组。

第二部分是颜色本身。我在 netbeans 中只看到有限数量的颜色(即没有紫色!)如何才能将这些颜色包含在此项目中以确保准确性?

不确定,但非常感谢任何建议。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;


public final class FinalProject extends JFrame {

private static final int FRAME_WIDTH = 700;
private static final int FRAME_HEIGHT = 800;
private JRadioButton redButton;
private JRadioButton greenButton;
private JRadioButton blueButton;
private JRadioButton aquaButton;
private JRadioButton grayButton;
private JRadioButton fuchsiaButton;
private JRadioButton blackButton;
private JRadioButton limeButton;
private JRadioButton silverButton;
private JRadioButton maroonButton;
private JRadioButton navyButton;
private JRadioButton oliveButton;
private JRadioButton purpleButton;
private JRadioButton tealButton;
private JRadioButton yellowButton;
private JRadioButton cyanButton;
private JRadioButton orangeButton;
private JRadioButton tanButton;
private JRadioButton lavanderButton;
private JRadioButton plumButton;
private ActionListener listener;

public FinalProject()
{ 
TreeMap<String,String> colorselect = new TreeMap<String,String>();
colorselect.put("00FFFF","Aqua");
colorselect.put("008000","green");
colorselect.put("808080","gray");
colorselect.put("ff00ff","fuchaia");
colorselect.put("0000ff","blue");
colorselect.put("000000","black");
colorselect.put("00ff00","lime");
colorselect.put("c0c0c0","silver");
colorselect.put("800000","Maroon");
colorselect.put("000080","olive");
colorselect.put("ff0000","red");
colorselect.put("800080","purple");
colorselect.put("008080","teal");
colorselect.put("ffff00","yellow");
colorselect.put("00ffff","cyan");
colorselect.put("ffa500","orange");
colorselect.put("d2b4bc","tan");
colorselect.put("e6e6fa","lavander");
colorselect.put("dda0dd","plum");

// This listener is shared among all components
class ChoiceListener implements ActionListener
{ 
@Override
public void actionPerformed(ActionEvent event)
{ 
setColor();
}
}

listener = new ChoiceListener();

createControlPanel();

setSize(FRAME_WIDTH, FRAME_HEIGHT);
}

public void createControlPanel()
{

JPanel styleGroupPanel = createRadioButtons();

JPanel controlPanel = new JPanel();
controlPanel.setLayout(new GridLayout());

controlPanel.add(styleGroupPanel);

add(controlPanel, BorderLayout.NORTH);
}

public JPanel createRadioButtons()
{
redButton = new JRadioButton("Red");
redButton.addActionListener(listener);

greenButton = new JRadioButton("Green");
greenButton.addActionListener(listener);

aquaButton = new JRadioButton("aqua");
aquaButton.addActionListener(listener);

grayButton = new JRadioButton("gray");
grayButton.addActionListener(listener);

fuchsiaButton = new JRadioButton("fuchsia");
fuchsiaButton.addActionListener(listener);

blackButton = new JRadioButton("black");
blackButton.addActionListener(listener);

limeButton = new JRadioButton("lime");
limeButton.addActionListener(listener);

silverButton = new JRadioButton("silver");
silverButton.addActionListener(listener);

maroonButton = new JRadioButton("maroon");
maroonButton.addActionListener(listener);

navyButton = new JRadioButton("navy");
navyButton.addActionListener(listener);

oliveButton = new JRadioButton("olive");
oliveButton.addActionListener(listener);

purpleButton = new JRadioButton("purple");
purpleButton.addActionListener(listener);

tealButton = new JRadioButton("teal");
tealButton.addActionListener(listener);

yellowButton = new JRadioButton("yellow");
yellowButton.addActionListener(listener);

cyanButton = new JRadioButton("cyan");
cyanButton.addActionListener(listener);

orangeButton = new JRadioButton("orange");
orangeButton.addActionListener(listener);

tanButton = new JRadioButton("tan");
tanButton.addActionListener(listener);

lavanderButton = new JRadioButton("lavander");
lavanderButton.addActionListener(listener);

plumButton = new JRadioButton("plum");
plumButton.addActionListener(listener);

blueButton = new JRadioButton("Blue");
blueButton.addActionListener(listener);
blueButton.setSelected(true);
getContentPane().setBackground(Color.BLUE);

ButtonGroup group = new ButtonGroup();
group.add(redButton);
group.add(greenButton);
group.add(blueButton);
group.add(plumButton);
group.add(aquaButton);
group.add(grayButton);
group.add(fuchsiaButton);
group.add(blackButton);
group.add(limeButton);
group.add(silverButton);
group.add(maroonButton);
group.add(navyButton);
group.add(oliveButton);
group.add(purpleButton);
group.add(tealButton);
group.add(yellowButton);
group.add(cyanButton);
group.add(orangeButton);
group.add(tanButton);
group.add(lavanderButton);


JPanel panel = new JPanel();
panel.add(redButton);
panel.add(greenButton);
panel.add(blueButton);
panel.add(aquaButton);
panel.add(grayButton);
panel.add(fuchsiaButton);
panel.add(blackButton);
panel.add(limeButton);
panel.add(silverButton);
panel.add(maroonButton);
panel.add(navyButton);
panel.add(oliveButton);
panel.add(purpleButton);
panel.add(tealButton);
panel.add(yellowButton);
panel.add(cyanButton);
panel.add(orangeButton);
panel.add(tanButton);
panel.add(lavanderButton);
panel.add(plumButton);

panel.setPreferredSize(new Dimension(100,200));
return panel;
}
public void setColor()
{
if(redButton.isSelected())
{
  getContentPane().setBackground(Color.RED);
}
if(greenButton.isSelected())
{
  getContentPane().setBackground(Color.GREEN);
}
if(blueButton.isSelected())
{
  getContentPane().setBackground(Color.BLUE);
}
  if(aquaButton.isSelected())
{
  getContentPane().setBackground(Color.GREEN);
}
    if(grayButton.isSelected())
{
  getContentPane().setBackground(Color.GRAY);
}
      if(fuchsiaButton.isSelected())
{
  getContentPane().setBackground(Color.PINK);
}
        if(blackButton.isSelected())
{
  getContentPane().setBackground(Color.BLACK);
}
          if(limeButton.isSelected())
{
  getContentPane().setBackground(Color.green);
}
            if(silverButton.isSelected())
{
  getContentPane().setBackground(Color.LIGHT_GRAY);
}
              if(maroonButton.isSelected())
{
  getContentPane().setBackground(Color.RED);
}
                if(navyButton.isSelected())
{
  getContentPane().setBackground(Color.BLUE);
}
       if(oliveButton.isSelected())
{
  getContentPane().setBackground(Color.green);
}
        if(purpleButton.isSelected())
{
  getContentPane().setBackground(Color.MAGENTA);
}
          if(tealButton.isSelected())
{
  getContentPane().setBackground(Color.BLUE);
}
            if(yellowButton.isSelected())
{
  getContentPane().setBackground(Color.YELLOW);
}
          if(cyanButton.isSelected())
{
  getContentPane().setBackground(Color.CYAN);
}
           if(orangeButton.isSelected())
{
  getContentPane().setBackground(Color.ORANGE);
}
             if(tanButton.isSelected())
{
  getContentPane().setBackground(Color.darkGray);
}
            if(lavanderButton.isSelected())
{
  getContentPane().setBackground(Color.MAGENTA);
}
              if(plumButton.isSelected())
{
  getContentPane().setBackground(Color.MAGENTA);
}
}
}

【问题讨论】:

  • 您可以使用 Map.keySet 返回一组键,然后您可以在 for 循环中迭代此 Set,使用当前键从 Map 中提取值。然后,您将基于这些值构建一个新的 JRadioButton。如果您需要访问 JRadioButton,您可以将它们放在一个数组或列表中。至于颜色,请尝试在互联网上查找网络颜色

标签: java user-interface collections map colors


【解决方案1】:

首先,我认为您误解了老师的指示。我认为当他说“最多存储 20 对”时,这意味着您的程序应该足够灵活,最多可以接受 20 对,而不是 20 对。这意味着程序事先不知道颜色。另外,当他说“唯一”时,我相信他的意思是您必须使用 Java 集合来确保颜色及其值是唯一的。

而且他肯定不打算让你在不使用它的情况下填满一个集合。这个想法是使用集合来构建 GUI。

尽管如此,为每个颜色按钮创建一个单独的变量并遍历 if 语句列表并不是好的编程习惯。每当您遇到一大组相似的项目时,您必须对其执行相似的操作,应该提示您使用集合/数组和循环,而不是一遍又一遍地编写相同的代码。特别是如果您不知道需要使用 10 种颜色还是 20 种颜色。

所以:

  1. 决定将颜色输入程序的方式 - 从命令行、文件或其他方式。
  2. 确定可帮助您保持颜色名称和颜色值唯一性的集合。
  3. 编写一个循环来读取输入,检查唯一性,并在 20 或数据完成时停止。
  4. 当您在集合中有颜色时,使用迭代创建按钮并将它们添加到按钮组和面板中。

即使您决定不想努力进行输入,至少将颜色放在一个静态数组中,您可以快速更改它、添加和删除颜色,并确保您从中读取使用循环而不是一个一个地将数组放入您的集合中 - 就像您从某种输入源获取它一样。

重要的是只有一个地方可以更改数据。在您实现这一点的方式中,如果您现在决定使用栗色而不是蓝绿色,则必须检查代码的许多部分,查找蓝绿色,将其删除并改用栗色。

现在,关于您的ActionListener。同样,如果您要编写数十个类似的 if 语句,那么您就走错了路。但是在这里,您缺少的是对传递给actionPerformedActionEvent 中的实际选定按钮的引用。您可以使用此参考来了解要使用的颜色。例如,在创建按钮时,如果将按钮的名称设置为颜色,则可以使用

Object eventSource = event.getSource();
String colorName;
if ( eventSource instanceof JRadioButton ) {
    colorName = ((JRadioButton)eventSource).getName();
    // Now get the color value from the collection, build a Color and change the background.
}

还有其他选项,例如使用Map&lt;JRadioButton,Color&gt;。创建单选按钮时,还要创建关联的Color 并将该对放在此映射中。现在在actionPerformed 中,您只需查找您在此地图中获得的 eventSource,即可立即获得Color 对象。

【讨论】:

  • 非常感谢您的帮助。这个项目正在按照我一开始看到的方式进行。非常感谢您的帮助。
【解决方案2】:

这样的事情应该可以解决问题。您可以遍历哈希图中的条目,一旦您知道重新分区消失了

public final class FinalProject extends JFrame {

    private Map<String, Color> colors = new TreeMap<String, Color>();

    public FinalProject() {
        colors.put("aqua", Color.decode("#00ffff"));
        colors.put("green", Color.decode("#008000"));
        colors.put("gray", Color.decode("#808080"));
        colors.put("fuchsia", Color.decode("#ff00ff"));
        colors.put("blue", Color.decode("#0000ff"));
        colors.put("black", Color.decode("#000000"));
        colors.put("lime", Color.decode("#00ff00"));
        colors.put("silver", Color.decode("#c0c0c0"));
        colors.put("maroon", Color.decode("#800000"));
        colors.put("olive", Color.decode("#000080"));
        colors.put("red", Color.decode("#ff0000"));
        colors.put("purple", Color.decode("#800080"));
        colors.put("teal", Color.decode("#008080"));
        colors.put("yellow", Color.decode("#ffff00"));
        colors.put("cyan", Color.decode("#00ffff"));
        colors.put("orange", Color.decode("#ffa500"));
        colors.put("tan", Color.decode("#d2b4bc"));
        colors.put("lavender", Color.decode("#e6e6fa"));
        colors.put("plum", Color.decode("#dda0dd"));

        final JPanel colorSwash = new JPanel();
        colorSwash.setPreferredSize(new Dimension(600, 600));

        JPanel colorRadio = new JPanel();
        colorRadio.setLayout(new GridLayout(2, 10, 20, 20));

        for(final Map.Entry<String, Color> entry : colors.entrySet()){
            JRadioButton rb = new JRadioButton(entry.getKey());
            rb.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    colorSwash.setBackground(entry.getValue());
                }
            });
            colorRadio.add(rb);
        }

        this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add(BorderLayout.NORTH, colorRadio);
        this.getContentPane().add(BorderLayout.CENTER, colorSwash);

    }

    public static void main(String[] args) {
        FinalProject fp = new FinalProject();
        fp.pack();
        fp.setVisible(true);
        fp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

【讨论】:

    【解决方案3】:

    您也可以在课程结束前添加这个小班:

    protected static class TreeMapBuilder {
      private TreeMap<String,String> treeMap = new TreeMap<String, String>();
      public TreeMapBuilder put(String color, String colorName) {
        treeMap.put(color, colorName);
        return this;
      }
      public TreeMap<String, String> end() {
        return treeMap;
      }
    }
    

    它将允许您以这种方式创建 TreeMap:

      TreeMap<String,String> colorselect = new TreeMapBuilder()
         .put("00FFFF","Aqua")
         .put("008000","green")
         .put("808080", "grey")
         .put(.............) (all your colors)
         .end();
    

    并且可以为你的风格加分:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-31
      • 1970-01-01
      • 2011-09-05
      • 1970-01-01
      • 2014-01-17
      • 2017-04-27
      • 1970-01-01
      • 2022-11-20
      相关资源
      最近更新 更多