if( source == yellow )
要用双等号,不能用单等号。
要用双等号,不能用单等号。
解决方案 »
- 一个很奇怪的问题,关于文件选择器的,谁帮忙解答一下……
- 怎么无法调动WINDOW_DESTROY?
- 有关泛型的问题,高手请进
- 【300分求解】如何弹出一个小窗口,显示一个图片并输入图片上的文字,然后返回输入的内容
- 小问
- 如何在一个程序中使用另个程序的类?
- 请教各位大虾————有关JAVA路径的设置问题(高分请教)
- 使用socket如何开发Server/Client程序?
- 组件如何放到窗体中?(请勿见笑)
- 我配置jb6的时候,Borland Enterprise Server 5.0 installation directory应该设置成什么呀???
- 升级了!分享Java资料
- 我的一个应用程序设置了MetalLookAndFeel,但是字体很难看,如何能够统一设置成我要的字体?
怎么可以这样写呢?
if(source == yellow)
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;class ButtonPanel extends JPanel implements ActionListener
{
public ButtonPanel()
{
JButton yellow = new JButton("yellow");
JButton blue = new JButton("blue");
add(yellow);
add(blue);
yellow.addActionListener(this);
blue.addActionListener(this);
}
public void actionPerformed(ActionEvent event)
{
Object source = event.getSource();
if( source == yellow ) //出错 variable yellow
setBackground( Color.yellow );
else setBackground( Color.blue );
repaint();
}
}class myframe extends JFrame
{
public myframe()
{
setSize(200,200);
setTitle("fuck");
ButtonPanel bp = new ButtonPanel();
Container c = getContentPane();
c.add(bp);
}
}public class ButtonTest
{
public static void main(String[] args)
{
myframe mf = new myframe();
mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mf.show();
}
}
JButton blue = new JButton("blue");
你这两个对象是在public ButtonPanel()方法中定义的,是区域变量,出了方法外面就无法调用,虽说你是在构造函数里申明,也很容易出现这样那样的问题,建议你这样:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
{
JButton yellow,blue;
class ButtonPanel extends JPanel implements ActionListener
{
public ButtonPanel()
{
yellow = new JButton("yellow");
blue = new JButton("blue");
add(yellow);
add(blue);
yellow.addActionListener(this);
blue.addActionListener(this);
}
public void actionPerformed(ActionEvent event)
{
Object source = event.getSource();
if( source == yellow ) //出错 variable yellow
setBackground( Color.yellow );
else setBackground( Color.blue );
repaint();
}
}class myframe extends JFrame
{
public myframe()
{
setSize(200,200);
setTitle("fuck");
ButtonPanel bp = new ButtonPanel();
Container c = getContentPane();
c.add(bp);
}
}public class ButtonTest
{
public static void main(String[] args)
{
myframe mf = new myframe();
mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mf.show();
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class ButtonPanel extends JPanel implements ActionListener
{
JButton yellow,blue;
public ButtonPanel()
{
yellow = new JButton("yellow");
blue = new JButton("blue");
add(yellow);
add(blue);
yellow.addActionListener(this);
blue.addActionListener(this);
}
public void actionPerformed(ActionEvent event)
{
Object source = event.getSource();
if( source == yellow ) //出错 variable yellow
setBackground( Color.yellow );
else setBackground( Color.blue );
repaint();
}
}class myframe extends JFrame
{
public myframe()
{
setSize(200,200);
setTitle("fuck");
ButtonPanel bp = new ButtonPanel();
Container c = getContentPane();
c.add(bp);
}
}public class ButtonTest
{
public static void main(String[] args)
{
myframe mf = new myframe();
mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mf.show();
}
}