corejava第八章8.1.3从组件转向事件监听器的例子编译通不过,高手帮忙看一下。 b1,b2是构造函数中的局部变量,actionPerformed方法中当然不能访问。简单的解决方法是把b1,b2定义为该类的私有字段就行了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 同意import java.awt.*;import java.awt.event.*;import javax.swing.*;public class ButtonTest{ JButton b1,b2; public static void main(String[] args) { ButtonFrame f=new ButtonFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.show(); }}class ButtonFrame extends JFrame{ public ButtonFrame() { setTitle("wwww"); setSize(300,200); ButtonPanel panel1=new ButtonPanel(); Container contentPane=getContentPane(); contentPane.add(panel1); } }class ButtonPanel extends JPanel implements ActionListener{ public ButtonPanel() { b1=new JButton("yellow"); b2=new JButton("red"); add(b1); add(b2); b1.addActionListener(this); b2.addActionListener(this); } public void actionPerformed(ActionEvent Event) { Object s=Event.getSource(); if(s==b1) { setBackground(Color.YELLOW); } else if(s==b2) { setBackground(Color.RED); } } } 求助一个关于接口的问题 网络传输应该用什么方法 如何固定swt固定窗口的大小啊 ? 请求指点~~关于移位算法 一个转换String为Date的东东,我错在哪了啊?请大家赐教,在线等。。。。。 java程序怎样变成windows service Oracle如果字符集是US7ASCII,Java能不能正常读写中文? jdk的正确配置? 请看:JavaMail的问题 对java掌握到什么程度算真正的“了解”、“熟悉”、“精通”? 大家过年好,请教一个关于swing中tree的问题 求助!急,在线等啊!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class ButtonTest
{
JButton b1,b2;
public static void main(String[] args)
{
ButtonFrame f=new ButtonFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.show();
}
}
class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("wwww");
setSize(300,200);
ButtonPanel panel1=new ButtonPanel();
Container contentPane=getContentPane();
contentPane.add(panel1);
}
}
class ButtonPanel extends JPanel
implements ActionListener
{
public ButtonPanel()
{
b1=new JButton("yellow");
b2=new JButton("red");
add(b1);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent Event)
{
Object s=Event.getSource();
if(s==b1)
{
setBackground(Color.YELLOW);
}
else if(s==b2)
{
setBackground(Color.RED);
}
}
}