1
我正在研究一個項目,該項目需要我從文本文件中將顏色和十六進制代碼讀取到地圖中。我創建了一個TreeMap,並在屏幕上正確存儲和打印,但我無法弄清楚如何讓它將數據發送到組合框。這裏是我的代碼迄今...使用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);
}
}
這編譯和運行,但我試圖讓組合框讀進口而不是進入他們的價值觀,和聽衆讀取元素,而不是進入他們。
沒有工作......我添加了我目前擁有的頂級代碼... – user1743835
以上是作爲示例給出的。密鑰集中包含哪些類型的對象? – Reimeus
只是顏色和顏色的十六進制值 – user1743835