【发布时间】:2015-10-09 15:22:16
【问题描述】:
//这是其中颜色为dropbox类型的数组......
private final String[] Options = new String[] {"Select Color", "RED" , "YELLOW", "BLUE" , "GREEN"};
private final JComboBox cmb1 = new JComboBox (Options);
//这是switch case..如果我选择红色,整个框架都是红色的。 enter image description here
private class ButtonHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// Get the selected value on the JCOmboBox
String SelectedValue = cmb1.getSelectedItem().toString();
// Display
switch (SelectedValue){
//如果我选择红色,文本将显示在下面的框架中。我怎样才能让“红色”的框架变成红色??
case "RED" :
txt1.setText ("Carmine \nCrimson \nFlame \nFushia \nLava \nMagenta \nMaroon"); break;
case "YELLOW" :
txt1.setText ("Amber \nApricot \nBeige \nGold \nKhaki \nMustard \nSaffron"); break;
case "BLUE" :
txt1.setText ("Azure \nCerulean \nCobalt \nCyan \nSky Blue \nIndigo \nSapphire"); break;
case "GREEN" :
txt1.setText ("Asparagus \nAvocado \nEmerald \nForest Green \nLime \nMint \nOlive"); break;
default:
txt1.setText("");
}
}
}
这就是我所做的……想要插入颜色……
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ComboBoxColor extends JFrame {
// Declare Combo Box with Values from Array
private final String[] Options = new String[] { "Select Color", "RED",
"YELLOW", "BLUE", "GREEN" };
private final JComboBox cmb1 = new JComboBox(Options);
public void itemStateChanged(java.awt.event.ItemEvent evt) {
itemStateChanged(evt);
}
// Declare JTextArea and wrap it in a JScrollPane
private final JTextArea txt1 = new JTextArea(2, 20);
private final JScrollPane scr = new JScrollPane(txt1);
// ActionListener
private final ButtonHandler BH = new ButtonHandler();
public JComboBox combo;
// Constructor
public ComboBoxColor() {
// Add ActionListener
cmb1.addActionListener(BH);
// Set Layout
Container pane = this.getContentPane();
pane.setLayout(new GridLayout(2, 4));
// Add Components
pane.add(cmb1);
pane.add(scr);
// Set JFrame Properties
this.setTitle("COMBO BOX COLOR");
this.setSize(500, 150);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
// ActionListener Sub-Class
private class ButtonHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// Get the selected value on the JCOmboBox
String SelectedValue = cmb1.getSelectedItem().toString();
// Display
switch (SelectedValue) {
case "RED":
txt1.setText("Carmine \nCrimson \nFlame \nFushia \nLava \nMagenta \nMaroon");
break;
case "YELLOW":
txt1.setText("Amber \nApricot \nBeige \nGold \nKhaki \nMustard \nSaffron");
break;
case "BLUE":
txt1.setText("Azure \nCerulean \nCobalt \nCyan \nSky Blue \nIndigo \nSapphire");
break;
case "GREEN":
txt1.setText("Asparagus \nAvocado \nEmerald \nForest Green \nLime \nMint \nOlive");
break;
default:
txt1.setText("");
}
}
}
// Main Method
public static void main(String[] args) {
ComboBoxColor CC = new ComboBoxColor();
}
}
【问题讨论】:
-
设置组件的背景色 'setBackground(Color)' 注意 setOpaque(true)
-
谢谢...但是在哪里插入呢?
-
对于example。
-
发布minimal reproducible example 这样我们就可以准确地看到什么都没发生,然后帮助您找出原因。
-
当我运行程序时,没有出现颜色` - 你还没有发布
MCVE,所以我们无法提供更多帮助。
标签: java swing combobox switch-statement