使用内部类的多个Jbutton和ActionListener

时间:2013-08-19 17:21:35

标签: java swing jbutton actionlistener paintcomponent

此应用有两个按钮。一种是在点击时随机改变颜色,另一种是在应用程序中更改标签。

问题是虽然我已经写了sepreate ActionListener类并分别用对应方法注册它们。

每次单击“更改标签”按钮时,颜色也会发生变化。这是怎么回事?

package my;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.ArrayList;

    import javax.swing.*;

    public class SimpleGui3C {

        JFrame frame;
        JLabel label;

        public static void main(String[] args){
            SimpleGui3C gui = new SimpleGui3C();
            gui.go();
        }

        public void go(){
            frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JButton labelButton = new JButton("Change Label");
            labelButton.addActionListener(new LabelListener());

            JButton colorButton = new JButton("Change Circle");
            colorButton.addActionListener(new ColorListener());

            label = new JLabel("I'm a label");

            MyDrawPanel drawPanel = new MyDrawPanel();

            frame.getContentPane().add(BorderLayout.SOUTH,colorButton);
            frame.getContentPane().add(BorderLayout.CENTER,drawPanel);
            frame.getContentPane().add(BorderLayout.EAST, labelButton);
            frame.getContentPane().add(BorderLayout.WEST, label);

            frame.setSize(300,300);
            frame.setVisible(true);


        }

        class LabelListener implements ActionListener{
            public void actionPerformed(ActionEvent event1) {
                ArrayList<String> labelContent = new ArrayList<String>();
                labelContent.add("Ouch!");
                labelContent.add("Damn!");
                labelContent.add("Holy shit!");
                labelContent.add("WTF?!");
                labelContent.add("Stop it!");
                labelContent.trimToSize();

                int i = (int)(Math.random()*5);
                String a = (String)labelContent.get(i);
                label.setText(a);
            }
        }

        class ColorListener implements ActionListener{
            public void actionPerformed(ActionEvent event2) {
                frame.repaint();
            }
        }
    }

            package my;
            import java.awt.*;
            import javax.swing.*;

            public class MyDrawPanel extends JPanel{
                /**
                 * 
                 */
                private static final long serialVersionUID = 1L;

                public void paintComponent(Graphics g){
                    Graphics2D g2d = (Graphics2D) g;

                    int red = (int)(Math.random()*255);
                    int blue = (int)(Math.random()*255);
                    int green = (int)(Math.random()*255);
                    Color startColor = new Color(red,blue,green);

                    red = (int)(Math.random()*255);
                    blue = (int)(Math.random()*255);
                    green = (int)(Math.random()*255);
                    Color endColor = new Color(red,blue,green);

                    int startPositionX = (int)(Math.random()*70);
                    int startPositionY = (int)(Math.random()*70);

                    int endPositionX = (int)(Math.random()*150);
                    int endPositionY = (int)(Math.random()*150);

                    GradientPaint gradient = new GradientPaint(startPositionX,startPositionY,startColor,endPositionX,endPositionY,endColor);
                    g2d.setPaint(gradient);
                    g2d.fillOval(20,60,100,100);

                }
            }

2 个答案:

答案 0 :(得分:2)

最有可能在更改标签时调用repaint()并调用MyDrawPanel.paintComponent()并使用随机颜色重新绘制框架。
更好的实施可以是:

class SimpleGui3C {
  Color startColor;
  Color endColor;

  ColorListener() {
    startColor = <code to generate a random color>;
    endColor = <code to generate a random color>;
    repaint();
  }
}

并且在 MyDrawPanel.paintComponent()使用听众生成的startColorendColor;记得初始化Color变量以避免NPE!

答案 1 :(得分:0)

每次点击labelButton时,都会为MyDrawPanel触发重绘事件。

这会导致颜色再次随机生成,从而改变MyDrawPanel的颜色。

我建议通过在MyDrawPanel上调用ColorListener中的方法来修复它,这会改变它的颜色。这将使面板在每次重新绘制时都不会改变颜色。

而这正是如何做到的:

class ColorListener implements ActionListener{
    MyDrawPanel colorPanel;
    public ColorListener(MyDrawPanel panel){
        this.colorPanel = panel;
    }

    public void actionPerformed(ActionEvent event2) {
        colorPanel.generateRandomColor();
        frame.repaint();
    }
}

public class MyDrawPanel extends JPanel{
    GradientPaint gradient;
    public MyDrawPanel(){ 
        generateRandomColor();
    }
    public void generateRandomColor(){
        int red = (int)(Math.random()*255);
        int blue = (int)(Math.random()*255);
        int green = (int)(Math.random()*255);
        Color startColor = new Color(red,blue,green);

        red = (int)(Math.random()*255);
        blue = (int)(Math.random()*255);
        green = (int)(Math.random()*255);
        Color endColor = new Color(red,blue,green);

        int startPositionX = (int)(Math.random()*70);
        int startPositionY = (int)(Math.random()*70);

        int endPositionX = (int)(Math.random()*150);
        int endPositionY = (int)(Math.random()*150);

        gradient = new GradientPaint(startPositionX,startPositionY,
            startColor, endPositionX,endPositionY,endColor);
     }
 }