问一个关于处理按钮点击事件的问题

beingyourself 2008-02-28 08:29:24
源代码:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ButtonTest {
public static void main(String[] args)
{
ButtonFrame frame=new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

}

class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("button test!");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
ButtonPanel panel=new ButtonPanel();
add(panel);
}
public static final int DEFAULT_WIDTH=200;
public static final int DEFAULT_HEIGHT=300;
}


class ButtonPanel extends JPanel
{
public ButtonPanel()
{
JButton yellowButton=new JButton("yellow");
JButton blueButton=new JButton("blue");
JButton greenButton=new JButton("green");

add(yellowButton);
add(blueButton);
add(greenButton);

ColorAction yellowAction=new ColorAction(Color.yellow);
ColorAction blueAction=new ColorAction(Color.blue);
ColorAction greenAction=new ColorAction(Color.green);

yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
greenButton.addActionListener(greenAction);

}
private class ColorAction implements ActionListener
{
private Color backgroundcolor;
public ColorAction(Color c)
{
backgroundcolor=c;
}

public void actionPerformed(ActionEvent e)
{
setBackground(backgroundcolor);
}
}

}

书上说因为ColorAction对象没有权限访问panel变量,所以把ColorAction设计为一个ButtonPanel的内部类。我想问为什么ColorAction类为什么要访问panel变量?

我看书上的说法好像是ColorAction类没有setBackground方法所以要用ButtonPanel类里面的setBackground()方法。setBackground方法不是定义再java.awt.Component里面吗?我已经加import java.awt.*;了,这是为什么啊!
...全文
110 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
ninesea 2008-02-28
  • 打赏
  • 举报
回复
哈 core java上的例子 呵呵 才看没多久 到是没考虑过你这个问题 刚想了一下
setBackground方法是要设置你的panel背景的 所以你要能在ColorAction里得到panel才行 那就在构造方法里加个参数
public ColorAction(Color c,JPanel j)

ColorAction yellowAction=new ColorAction(Color.yellow);
就要相应的改为
ColorAction yellowAction=new ColorAction(Color.yellow,this);

这样就可以把ColorAction 移到外面去了
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ButtonTest {
public static void main(String[] args)
{
ButtonFrame frame=new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

}

class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("button test!");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
ButtonPanel panel=new ButtonPanel();
add(panel);
}
public static final int DEFAULT_WIDTH=200;
public static final int DEFAULT_HEIGHT=300;
}


class ButtonPanel extends JPanel
{
public ButtonPanel()
{
JButton yellowButton=new JButton("yellow");
JButton blueButton=new JButton("blue");
JButton greenButton=new JButton("green");

add(yellowButton);
add(blueButton);
add(greenButton);

ColorAction yellowAction=new ColorAction(Color.yellow,this);
ColorAction blueAction=new ColorAction(Color.blue,this);
ColorAction greenAction=new ColorAction(Color.green,this);


yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
greenButton.addActionListener(greenAction);

}


}
class ColorAction implements ActionListener //private去掉
{
private Color backgroundcolor;
private JPanel panel;
public ColorAction(Color c,JPanel j)
{
backgroundcolor=c;
panel=j;
}

public void actionPerformed(ActionEvent e)
{
panel.setBackground(backgroundcolor);
}
}

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧