这个是《JAVA2核心编程》第8章的一个例题,我把原本作为内部类的ColorAction提出为外部类就出错了,但不知道具体为什么,因为才刚开始学JAVA, 希望高手赐教!不胜感激!
修改的程序:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;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()
{
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
setBounds(screenWidth / 4, screenHeight / 4, screenWidth / 2, screenHeight / 2);
setTitle("ButtonTest");
Image img = kit.getImage("icon.gif");
setIconImage(img);
ButtonPanel panel = new ButtonPanel();
add(panel);
}
}
class ButtonPanel extends JPanel
{
public ButtonPanel()
{
JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("blue");
JButton redButton = new JButton("Red");
add(yellowButton);
add(blueButton);
add(redButton);
ColorAction yellowAction = new ColorAction(Color.YELLOW);
ColorAction blueAction = new ColorAction(Color.BLUE);
ColorAction redAction = new ColorAction(Color.RED);
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
}
public void actionPerformed(ActionEvent event)
{
setBackground(backgroundColor);
}
}class ColorAction implements ActionListener
{
public ColorAction(Color c)
{
backgroundColor = c;
}
public void actionPerformed(ActionEvent event)
{
setBackground(backgroundColor);
}
private Color backgroundColor;
}
原程序:
……//与前面一样
class ButtonPanel extends JPanel
{
public ButtonPanel()
{
……//与前面一样
}
private class ColorAction implements ActionListener
{
public ColorAction(Color c)
{
backgroundColor = c;
}
public void actionPerformed(ActionEvent event)
{
setBackground(backgroundColor);
}
private Color backgroundColor;
}
}
修改的程序:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;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()
{
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
setBounds(screenWidth / 4, screenHeight / 4, screenWidth / 2, screenHeight / 2);
setTitle("ButtonTest");
Image img = kit.getImage("icon.gif");
setIconImage(img);
ButtonPanel panel = new ButtonPanel();
add(panel);
}
}
class ButtonPanel extends JPanel
{
public ButtonPanel()
{
JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("blue");
JButton redButton = new JButton("Red");
add(yellowButton);
add(blueButton);
add(redButton);
ColorAction yellowAction = new ColorAction(Color.YELLOW);
ColorAction blueAction = new ColorAction(Color.BLUE);
ColorAction redAction = new ColorAction(Color.RED);
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
}
public void actionPerformed(ActionEvent event)
{
setBackground(backgroundColor);
}
}class ColorAction implements ActionListener
{
public ColorAction(Color c)
{
backgroundColor = c;
}
public void actionPerformed(ActionEvent event)
{
setBackground(backgroundColor);
}
private Color backgroundColor;
}
原程序:
……//与前面一样
class ButtonPanel extends JPanel
{
public ButtonPanel()
{
……//与前面一样
}
private class ColorAction implements ActionListener
{
public ColorAction(Color c)
{
backgroundColor = c;
}
public void actionPerformed(ActionEvent event)
{
setBackground(backgroundColor);
}
private Color backgroundColor;
}
}
import java.awt.event.*;
import javax.swing.*; 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()
{
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
setBounds(screenWidth / 4, screenHeight / 4, screenWidth / 2, screenHeight / 2);
setTitle("ButtonTest");
Image img = kit.getImage("icon.gif");
setIconImage(img);
ButtonPanel panel = new ButtonPanel();
add(panel);
}
}
class ButtonPanel extends JPanel
{
public ButtonPanel()
{
JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("blue");
JButton redButton = new JButton("Red");
add(yellowButton);
add(blueButton);
add(redButton);
ColorAction yellowAction = new ColorAction(Color.YELLOW);
ColorAction blueAction = new ColorAction(Color.BLUE);
ColorAction redAction = new ColorAction(Color.RED);
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
}
/*public void actionPerformed(ActionEvent event)
{
setBackground(backgroundColor);//类中未定义变量backgroundColor
}*///无用代码
} class ColorAction implements ActionListener
{
public ColorAction(Color c)
{
backgroundColor = c;
}
public void actionPerformed(ActionEvent e)
{
((JButton)e.getSource()).setBackground(backgroundColor);//setBackground应该为JButton中的方法
}
private Color backgroundColor;
}
这个改变的是JPanel的颜色
import java.awt.*;
import java.awt.event.*;
import javax.swing.*; 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()
{
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
setBounds(screenWidth / 4, screenHeight / 4, screenWidth / 2, screenHeight / 2);
setTitle("ButtonTest");
Image img = kit.getImage("icon.gif");
setIconImage(img);
ButtonPanel panel = new ButtonPanel();
add(panel);
}
}
class ButtonPanel extends JPanel
{
public ButtonPanel()
{
JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("blue");
JButton redButton = new JButton("Red");
add(yellowButton);
add(blueButton);
add(redButton);
ColorAction yellowAction = new ColorAction(Color.YELLOW, this); //将Panel传入
ColorAction blueAction = new ColorAction(Color.BLUE, this); //
ColorAction redAction = new ColorAction(Color.RED, this); //
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
}
/*public void actionPerformed(ActionEvent event)
{
setBackground(backgroundColor);//类中未定义变量backgroundColor
}*///无用代码
} class ColorAction implements ActionListener
{
JPanel aimPanel;
public ColorAction(Color c, JPanel aim)
{
aimPanel = aim;
backgroundColor = c;
}
public void actionPerformed(ActionEvent e)
{
//((JButton)e.getSource()).setBackground(backgroundColor);//setBackground应该为JButton中的方法
aimPanel.setBackground(backgroundColor);//setBackground应该为JPanel中的方法
}
private Color backgroundColor;
}
class ButtonPanel extends JPanel
{
public ButtonPanel()
{
//与前面一样;
public void actionPerformed(ActionEvent event)
{
setBackground(ColorAction.getBackgroundColor);//在ColorAction类中定义了getBackgroundColor
// setBackground(ColorAction.backgroundColor);//类中未定义backgroundColor,
}
}class ColorAction implements ActionListener
{
//与前面一样; public Color getBackgroundColor()
{
return backgroundColor;
} private Color backgroundColor;
}
改成这样后还是有错,我发现是因为ColorAction.getBackgroundColor不是静态函数,不能使用类名.方法名方式调用,但将getBackgroundColor方法改为static, 和把backgroundColor改为静态变量了也还是不行。
难道这个程序只能按照wd9053的方式编写吗?请高手帮忙解惑,不胜感激!!
另外,以后贴代码最好按格式贴,如果不长最好全贴出来
http://topic.csdn.net/u/20090421/22/16f06a01-9688-4dc8-9352-62c82c6adfb5.html?82958import java.awt.*;
import java.awt.event.*;
import javax.swing.*; 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()
{
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
setBounds(screenWidth / 4, screenHeight / 4, screenWidth / 2, screenHeight / 2);
setTitle("ButtonTest");
Image img = kit.getImage("icon.gif");
setIconImage(img);
ButtonPanel panel = new ButtonPanel();
add(panel);
}
}
class ButtonPanel extends JPanel
{
public ButtonPanel()
{
JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("blue");
JButton redButton = new JButton("Red");
add(yellowButton);
add(blueButton);
add(redButton);
ColorAction yellowAction = new ColorAction(Color.YELLOW, this); //
ColorAction blueAction = new ColorAction(Color.BLUE, this); //
ColorAction redAction = new ColorAction(Color.RED, this); //
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
}
public void actionPerformed(ActionEvent event)
{
setBackground(ColorAction.getBackgroundColor());
}
} class ColorAction implements ActionListener
{
JPanel aimPanel;
public ColorAction(Color c, JPanel aim)
{
aimPanel = aim;
backgroundColor = c;
}
public static Color getBackgroundColor()
{
return backgroundColor;
}
public void actionPerformed(ActionEvent e)
{
//((JButton)e.getSource()).setBackground(backgroundColor);//setBackground应该为JButton中的方法
aimPanel.setBackground(backgroundColor);//setBackground应该为JPanel中的方法
}
private static Color backgroundColor;
}
在ButtonFrame中添加了一个ButtonPanel。
ButtonPanel中含有三个JButton,每个JButton各自添加了一个ColorAction的对象作为其事件监听者。
在ColorAction的构造方法中传入了ButtonPanel的一个引用,在actionPerformed方法中修改该ButtonPanel的背景色
不明白这样能否回答你的问题
class ButtonPanel extends JPanel{
public ButtonPanel(){
JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("blue");
JButton redButton = new JButton("Red");
add(yellowButton);
add(blueButton);
add(redButton);
//应该是改变Jpanel的背景色
ColorAction yellowAction = new ColorAction(Color.YELLOW,this);
ColorAction blueAction = new ColorAction(Color.BLUE,this);
ColorAction redAction = new ColorAction(Color.RED,this);
//如果要改变按钮的背景色用下面代码
//ColorAction yellowAction = new ColorAction(Color.YELLOW,yellowButton);
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
}
}class ColorAction implements ActionListener{
public ColorAction(Color c,JComponent componet){
backgroundColor = c;
this.componet = componet;
} public void actionPerformed(ActionEvent e){
componet.setBackground(backgroundColor);
} private Color backgroundColor;
private JComponent componet ;
}