回 帖 发 新 帖 刷新版面

主题:[讨论]事件监听失败

题目要求: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按钮。

请大侠帮忙,不胜感激!    

回复列表 (共1个回复)

沙发

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 javax.swing.JButton;
import javax.swing.JPanel;

public class BallPanel extends JPanel implements ActionListener{

    //create button
    JButton redButton = new JButton("red");

    JButton blueButton = new JButton("blue");

    Color c = Color.red;
    
    Ellipse2D circle = new Ellipse2D.Double();

    public BallPanel() {
        circle.setFrameFromCenter(30, 25, 60, 55);
        //add button to the panel
        add(redButton);
        add(blueButton);
        redButton.addActionListener(this);
        redButton.setActionCommand("red");
        blueButton.addActionListener(this);
        blueButton.setActionCommand("blue");
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setPaint(c);
        g2.fill(circle);
    }

    public void actionPerformed(ActionEvent e) {
        String color = e.getActionCommand().trim();
        if("blue".equals(color)) {
            c = Color.blue;
        }
        else {
            c = Color.red;
        }
        paintComponent(getGraphics());
    }
}

能实现功能,具体的完善就自己搞定拉

我来回复

您尚未登录,请登录后再回复。点此登录或注册