1
저는 텍스트 파일의 색상에 대한 색상 및 16 진수 코드를지도에 입력해야하는 프로젝트를 진행하고 있습니다. TreeMap을 만들어서 저장하고 화면에 제대로 인쇄하지만 데이터를 콤보 상자로 보내는 방법을 알 수는 없습니다. 여기 내 코드는TreeMap을 사용하여 ComboBox 채우기
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
색상 및 색상의 16 진수 값 – user1743835