就是,我建了一个数组对象Color[] color={Color.yellow,Color.red,Color.blue,.......};
然后JComboBox jcbColor=new JComboBox(color);
在面板上有文字,选中组合框jcbColor中的颜色,就让文字变成所选的颜色。这个没什么问题。
但是,组合框里的每一项,它的名称都为类似于这么一种形式 java.awt.Color[r=0,g=0,b=0].
我的问题是,如何让它的每一项名称改为“红色”,“蓝色”等,不然看着好不爽啊。
高手帮帮忙啦。。
----------------解决方案--------------------------------------------------------
new JComboBox(color) 里面使用的应该是字符串类型的数组,但是你的 color 是个 Color 类型的数组,所以你一启动程序,相当于是给每个 Color 做了一个 toString()。
弄两个数字试试,根据选择的 index 来调用 Color 类型数组里面的颜色。
----------------解决方案--------------------------------------------------------
楼上的兄弟,你说的那个方法,我实在是不知道怎么做才好。我文字,是写在我建的一个面板myPanel上的,我要设置字体的颜色,我就要用到方法myPanel.setForeground(Color.颜色); 括号里面我就用color[getSelectedIndex()]来设置我选中的颜色,至于你说的选择index调和颜色,我也想过,但没有成功。麻烦仁兄帮忙把方法具体写一下,拜托。
----------------解决方案--------------------------------------------------------
import java.awt.event.*;
import javax.swing.*;
public class ForegroundTest extends JFrame{
private String strColors[];
private Color allColors[];
private JComboBox colorList;
private JLabel textLabel;
public ForegroundTest(){
strColors = new String[] {"Black", "White", "Red", "Blue"};
allColors = new Color[] {Color.BLACK, Color.WHITE, Color.RED, Color.BLUE};
textLabel = new JLabel("Draw my text here");
this.getContentPane().add(textLabel, BorderLayout.CENTER);
colorList = new JComboBox(strColors);
colorList.setEditable(false);
colorList.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
JComboBox source = (JComboBox)event.getSource();
textLabel.setForeground(allColors[source.getSelectedIndex()]);
}
});
this.getContentPane().add(colorList, BorderLayout.SOUTH);
this.setLocationRelativeTo(this.getParent());
this.pack();
}
public static void main(String[] args){
ForegroundTest test = new ForegroundTest();
test.setVisible(true);
}
}
----------------解决方案--------------------------------------------------------
忘记点东西,修改一下
ForegroundTest test = new ForegroundTest();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.setVisible(true);
}
另外,偶不是“兄弟”啦~~
[此贴子已经被作者于2007-9-24 20:27:50编辑过]
----------------解决方案--------------------------------------------------------
谢谢啦,在看你的答案之前我已经做出来了,是我想得复杂了,其实我就只加了两行代码哇,看了你的回答,想法竟然是一样的,这难道是缘份吗?小妹妹??
----------------解决方案--------------------------------------------------------