这段代码哪里出错了?为什么没有效果?为什么打印出来的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编辑过]
----------------解决方案--------------------------------------------------------
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.*;
public 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.equals("black"))
{ col = new Color(0,0,0);}
else if(name.equals("red"))
{ col = new Color(255,0,0); }
else if(name.equals("green"))
{ col = new Color(0,255,0); }
else if(name.equals("blue"))
{ col = new Color(0,0,255); }
canvas.setBackground(col);
repaint();
}
}
public static void main(String[] args) {
// Create application frame.
PaintBoardFrame frame = new PaintBoardFrame();
// Show frame
frame.setVisible(true);
}
}
已经帮你改过来了,最后比较字符串是否相等时,不要用"==",而用equals()方法,虽然你这个题目用==也可以,但是养成一个好习惯很重要
还有,你为什么不变背景色,是因为你没有重新设过
----------------解决方案--------------------------------------------------------
itemStateChanged()应该改为
public void itemStateChanged(ItemEvent e)
{
if(e.getSource()==colorChoice)
{
Object ob=colorChoice.getSelectedItem();
String name=ob.toString();
System.out.print(name);
if(name.equals("black"))
{ col =Color.black; canvas.setBackground(col); }
else if(name.equals("red"))
{ col =Color.red; canvas.setBackground(col); }
else if(name.equals("green"))
{ col =Color.green; canvas.setBackground(col);}
else if(name.equals("blue"))
{ col =Color.blue; canvas.setBackground(col);}
}
}
----------------解决方案--------------------------------------------------------
你都没写让他变色 当然不会变了
----------------解决方案--------------------------------------------------------
我现在又碰到一个问题了,在这个程序中我实现了绘图的功能,但是每次改变背景颜色的时候都会清除以前所画的内容!
我已经调用了updata
----------------解决方案--------------------------------------------------------
这是没有办法的,重画的画就会重新画一遍
除非你分开来画,把要画的内容放在paintComponent里去
----------------解决方案--------------------------------------------------------
我也是这样实现了呀,我把画布作为一个单独的类继承了canvas,再将它加到上面的程序中,不过还是不能解决上述的问题呀
----------------解决方案--------------------------------------------------------
不要继承自Canvas,现在都提倡用javax.swing包里的东西了,AWT的能不用尽量不要用
----------------解决方案--------------------------------------------------------