当前位置: 技术问答>java相关
事件处理的问题
来源: 互联网 发布时间:2015-10-23
本文导语: 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); ...
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();
}
}
why?
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();
}
}
why?
|
JButton yellow = new JButton("yellow");
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();
}
}
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();
}
}
|
if( source == yellow )
要用双等号,不能用单等号。
要用双等号,不能用单等号。
|
呵呵,楼主应该是不小心啦,不过以后自己也要仔细一点,这种问题查一下很容易就发现的。