主题:[讨论]事件监听失败
题目要求:Create an application ChangeBallColor that displays a panel. This panel will contain two buttons (one for red and one for blue). It will also display a filled in circle. When the user clicks on a button, the circle should change to that color. So, if a user clicks the blue button, the circle will change to blue. Similarly, if a user clicks the red button, the circle will turn to red.
我的代码:
[size=4][color=800080]BallPanel[/color][/size]package javaLab9_1;类
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JButton;
import javax.swing.JPanel;
public class BallPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
final Graphics2D g2=(Graphics2D)g;
//create a circle
final Ellipse2D circle=new Ellipse2D.Double();
circle.setFrameFromCenter(30, 25, 60, 55);
//create button
JButton redButton=new JButton("red");
JButton blueButton=new JButton("blue");
//add button to the panel
add(redButton);
add(blueButton);
// add action to the button
redButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
g2.setPaint(Color.red);
g2.fill(circle);
}
});
blueButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
g2.setPaint(Color.blue);
g2.fill(circle);
}
});
}
}
BallFrame类
[color=000000][size=3]package javaLab9_1;
import javax.swing.JFrame;
public class BallFrame extends JFrame
{
public BallFrame()
{
setTitle("ChangeBallColor");
setSize(200,200);
BallPanel panel=new BallPanel();
add(panel);
}
}
[/size][/color]
[color=800080][size=4]ChangeBallColor 类 [/size][/color]
package javaLab9_1;
import javax.swing.JFrame;
public class ChangeBallColor
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
BallFrame frame=new BallFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
问题:为什么上面的程序不绘制园呢?而且出现了好多red blue按钮。
请大侠帮忙,不胜感激!
我的代码:
[size=4][color=800080]BallPanel[/color][/size]package javaLab9_1;类
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JButton;
import javax.swing.JPanel;
public class BallPanel extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
final Graphics2D g2=(Graphics2D)g;
//create a circle
final Ellipse2D circle=new Ellipse2D.Double();
circle.setFrameFromCenter(30, 25, 60, 55);
//create button
JButton redButton=new JButton("red");
JButton blueButton=new JButton("blue");
//add button to the panel
add(redButton);
add(blueButton);
// add action to the button
redButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
g2.setPaint(Color.red);
g2.fill(circle);
}
});
blueButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
g2.setPaint(Color.blue);
g2.fill(circle);
}
});
}
}
BallFrame类
[color=000000][size=3]package javaLab9_1;
import javax.swing.JFrame;
public class BallFrame extends JFrame
{
public BallFrame()
{
setTitle("ChangeBallColor");
setSize(200,200);
BallPanel panel=new BallPanel();
add(panel);
}
}
[/size][/color]
[color=800080][size=4]ChangeBallColor 类 [/size][/color]
package javaLab9_1;
import javax.swing.JFrame;
public class ChangeBallColor
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
BallFrame frame=new BallFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
问题:为什么上面的程序不绘制园呢?而且出现了好多red blue按钮。
请大侠帮忙,不胜感激!