if( source == yellow )
要用双等号,不能用单等号。
要用双等号,不能用单等号。
解决方案 »
- java.awt编写的软键盘,如何实现软键盘的输入显示到jsp的txt中
- 程序中一个类,比如类a,有时候有a.class.xxxx,这个class起什么作用呢
- 输入两个整数(N>0)输出其代数和
- hibernate从mysql数据库显示数据乱码
- 关于jsp系统和Exchange邮件的单一登陆
- 问这个的区别??
- 大家帮我看下这段用jdbc连接Oracle10G数据库的代码运行时会抛出异常,是怎么回事呢
- 这样的功能,java能实现吗?
- 能不能把class文件反编译到真正JAVA源代码?
- CodeWars Twice linear 算法问题
- 升级了!分享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();
}
}