这是针对学校的一个项目我有两个不同的问题,我想请一些建议。以下是老师的要求:
"使用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);
}
}
}
答案 0 :(得分:3)
首先,我认为你误解了老师的指示。我想当他说&#34;存储多达20对&#34;这意味着你的程序应该足够灵活,最多可以接受20对,而不是20对。这意味着程序不会提前知道颜色。此外,当他说&#34; unique&#34;时,我相信他意味着你必须使用Java集合来确保颜色及其值是唯一的。
而且他肯定不打算在不使用它的情况下填写一个系列。我们的想法是使用该集合来构建GUI。
尽管如此,为每个颜色按钮创建一个单独的变量并浏览if语句列表并不是一个好的编程习惯。每当遇到大量类似的项目时,你必须执行类似的操作,应该是使用集合/数组和循环的提示,而不是一遍又一遍地编写相同的代码。特别是如果你不知道你是否需要使用10种颜色或20种颜色。
所以:
即使您决定不想努力做输入,至少将颜色放在静态数组中,您可以快速更改它,添加和删除颜色,并确保您阅读从那个数组到你的集合中使用循环而不是一个接一个 - 就像你是从某种输入源获取它一样。
重要的部分是只有一个地方可以更改数据。在你实现这个的方式中,如果你现在决定使用maroon而不是teal,你必须经历代码的许多部分,寻找蓝绿色,删除它并使用栗色代替。
现在,关于你的ActionListener
。同样,如果你要写几十个类似的if语句,你就走错了路。但是在这里,您缺少的是您在ActionEvent
中传递给actionPerformed
的实际所选按钮的引用。您可以使用此引用来了解要使用的颜色。例如,在创建按钮时,如果将按钮的名称设置为颜色,则可以使用
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<JRadioButton,Color>
。创建单选按钮时,还要创建关联的Color
并将其放在此地图中。现在在actionPerformed
中,您可以只查看此地图中的eventSource,然后立即获得Color
对象。
答案 1 :(得分:1)
这样的事情应该可以解决问题。您可以遍历散列映射中的条目,一旦您知道重新分区消失
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);
}
}
答案 2 :(得分:1)
您也可以在课程结束前添加这个小班:
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();
并且可以为你增加一些风格点数:)