我正在做一个项目,该项目需要我将颜色和颜色的十六进制代码从文本文件中读取到地图中。我创建了一个 TreeMap,它在屏幕上正确存储和打印,但我不知道如何让它将数据发送到组合框。到目前为止,这是我的代码...
public class Project extends JFrame{
JComboBox CBColor = new JComboBox(new String[]
{"", "AQUA", "BLACK", "BLUE", "BROWN", "FUCHSIA", "GRAY",
"GREEN", "INDIGO", "LIME", "MAROON", "NAVY", "ORANGE",
"PINK", "PURPLE", "RED", "SIENNA", "TAN", "TEAL", "WHITE", "YELLOW"});
JTextArea TAText = new JTextArea(5, 25);
JButton BApply = new JButton("Apply");
JButton BExit = new JButton("Exit");
public Project() {
JPanel SelectionPanel = new JPanel(new BorderLayout());
SelectionPanel.add(CBColor, BorderLayout.NORTH);
SelectionPanel.add(TAText, BorderLayout.CENTER);
JPanel ApplyPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
ApplyPanel.add(BApply);
ApplyPanel.add(BExit);
add(SelectionPanel, BorderLayout.NORTH);
add(ApplyPanel, BorderLayout.SOUTH);
BApply.addActionListener(new ButtonListener());
BExit.addActionListener(new ExitButtonListener());
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String BGColor = "";
if (CBColor.getSelectedItem() == "AQUA")
BGColor = "#00FFFF";
else if (CBColor.getSelectedItem() == "BLACK")
BGColor = "#000000";
else if (CBColor.getSelectedItem() == "BLUE")
BGColor = "#0000FF";
else if (CBColor.getSelectedItem() == "BROWN")
BGColor = "#A52A2A";
else if (CBColor.getSelectedItem() == "FUCHSIA")
BGColor = "#FF00FF";
else if (CBColor.getSelectedItem() == "GRAY")
BGColor = "#BEBEBE";
else if (CBColor.getSelectedItem() == "GREEN")
BGColor = "#008000";
else if (CBColor.getSelectedItem() == "INDIGO")
BGColor = "#4B0082";
else if (CBColor.getSelectedItem() == "LIME")
BGColor = "#00FF00";
else if (CBColor.getSelectedItem() == "MAROON")
BGColor = "#800000";
else if (CBColor.getSelectedItem() == "NAVY")
BGColor = "#000080";
else if (CBColor.getSelectedItem() == "ORANGE")
BGColor = "#FFA500";
else if (CBColor.getSelectedItem() == "PINK")
BGColor = "#FFC0CB";
else if (CBColor.getSelectedItem() == "PURPLE")
BGColor = "#800080";
else if (CBColor.getSelectedItem() == "RED")
BGColor = "#FF0000";
else if (CBColor.getSelectedItem() == "SIENNA")
BGColor = "#A0522D";
else if (CBColor.getSelectedItem() == "TAN")
BGColor = "#D2B48C";
else if (CBColor.getSelectedItem() == "TEAL")
BGColor = "#008080";
else if (CBColor.getSelectedItem() == "WHITE")
BGColor = "#FFFFFF";
else if (CBColor.getSelectedItem() == "YELLOW")
BGColor = "#FFFF00";
TAText.setBackground(Color.decode(BGColor));
}
}
private class ExitButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
public static void main(String[] args) throws Exception {
Map<String, String> ColorsHex = new TreeMap<String, String>();
BufferedReader input = new BufferedReader(new FileReader("colors.txt"));
String line = "";
while ((line = input.readLine()) != null) {
String parts[] = line.split(", ");
ColorsHex.put(parts[0], parts[1]);
}
input.close();
System.out.println(ColorsHex);
System.out.print(ColorsHex.keySet());
System.out.print(ColorsHex.get("RED"));
Project frame = new Project();
frame.setTitle("Colors");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
这会编译并运行,但我试图让组合框读取导入的值而不是输入它们,并让侦听器读取元素而不是输入它们。