这段代码哪里出错了?为什么没有效果?为什么打印出来的name都是两个一样的,怎么改?各位大虾帮我看看!谢谢了
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.geom.*;
import java.io.*;
class PaintBoardFrame extends JFrame implements ItemListener
{
Color col;
JPanel canvas;
Container container;
JComboBox colorChoice;
public PaintBoardFrame()
{
container = this.getContentPane(); //获得窗口容器
container.setLayout(null); //自定义布局方式
canvas = new JPanel();//画布
canvas.setBackground(col);
canvas.setBounds(75, 2, 710, 500);//画布位置
canvas.setBorder(BorderFactory.createLoweredBevelBorder()); //画布边框
colorChoice=new JComboBox();
colorChoice.addItem("black");
colorChoice.addItem("red");
colorChoice.addItem("blue");
colorChoice.addItem("green");
colorChoice.addItem("其他...");
colorChoice.setEditable(false);
colorChoice.setBounds(3,190,60,20);
colorChoice.addItemListener(this);
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
container.add(colorChoice);
container.add(canvas);
setTitle("改变背景颜色");
setSize(new Dimension(800, 600));
}
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==colorChoice)
{
Object ob=colorChoice.getSelectedItem();
String name=ob.toString();
System.out.print(name);
if(name=="black")
{ col = new Color(0,0,0); }
else if(name=="red")
{ col = new Color(255,0,0); }
else if(name=="green")
{ col = new Color(0,255,0); }
else if(name=="blue")
{ col = new Color(0,0,255); }
}
}
public static void main(String[] args) {
// Create application frame.
PaintBoardFrame frame = new PaintBoardFrame();
// Show frame
frame.setVisible(true);
}
}
[此贴子已经被作者于2006-6-14 13:01:32编辑过]