展开窗口时触发actionPerformed

时间:2018-06-06 00:32:19

标签: java swing user-interface actionlistener

我试图更好地理解Java中的GUI编程,并且发生了一些奇怪的行为。程序显示一个带有渐变颜色的圆圈,当您单击按钮时,颜色会发生变化。非常简单,除了我注意到当我意外地扩展窗口时,触发了actionPerformed,并且按钮会不断地改变颜色,好像我在发送垃圾按钮一样。我的问题是为什么会这样?我正在使用Windows环境,如果这很重要。我的代码:

public class SimpleGui3C implements ActionListener {
JFrame frame;

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 button = new JButton("Change Colors");
    button.addActionListener(this);

    MyDrawPanel drawPanel = new MyDrawPanel();

    frame.getContentPane().add(BorderLayout.SOUTH, button);
    frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
    frame.setSize(300, 300);
    frame.setVisible(true);
}

public void actionPerformed(ActionEvent event) {
    frame.repaint();
}

}

MyDrawPanel

class MyDrawPanel extends JPanel {

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

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

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

    GradientPaint gradient = new GradientPaint(70,70,startColor, 150,150, endColor);
    g2d.setPaint(gradient);
    g2d.fillOval(70,70,100,100);
}
}

1 个答案:

答案 0 :(得分:3)

不,actionPerformed绝对是被触发。而是触发repaint()。你的问题是你在paintComponent方法中进行随机化,你永远不应该从这个方法改变类的状态,因为你没有完全控制何时甚至如果它应该或者可以被称为。

而是在actionPerformed方法中进行随机化,设置红色,蓝色和绿色实例字段,调用重绘,并在paintComponent方法中使用这些字段。

类似的东西:

private int red1, green1, blue1, red2, green2, blue2;

@Override
public void actionPerfomed(ActionEvent e) {
    red1 = (int) (Math.random() * 256);
    green1 = (int) (Math.random() * 256);
    blue1 = (int) (Math.random() * 256);

    red2 = (int) (Math.random() * 256);
    green2 = (int) (Math.random() * 256);
    blue2 = (int) (Math.random() * 256);
    repaint();
}

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

    Color startColor = new Color(red1, green1, blue1);
    Color endColor = new Color(red2, green2, blue2);

    GradientPaint gradient = new GradientPaint(70,70,startColor, 150,150, endColor);
    g2d.setPaint(gradient);
    g2d.fillOval(70,70,100,100);
}

如,

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import javax.swing.*;

public class SimpleGui3C {

    @SuppressWarnings("serial")
    private static void createAndShowGui() {
        final MyDrawPanel gui = new MyDrawPanel();

        JButton drawButton = new JButton(new AbstractAction("Randomize") {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                gui.randomizeColor();
            }
        });

        JFrame frame = new JFrame("SimpleGui3C");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(gui);
        frame.getContentPane().add(drawButton, BorderLayout.PAGE_END);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

@SuppressWarnings("serial")
class MyDrawPanel extends JPanel {
    private static final int PREF_W = 800;
    private static final int PREF_H = 650;
    private static final int OVAL_X = 70;
    private static final int OVAL_W = 100;
    private int red1, green1, blue1, red2, green2, blue2;

    public MyDrawPanel() {
        int w = OVAL_W + 2 * OVAL_X;
        setPreferredSize(new Dimension(w, w));
        randomizeColor();
    }

    public void randomizeColor() {
        red1 = (int) (Math.random() * 256);
        green1 = (int) (Math.random() * 256);
        blue1 = (int) (Math.random() * 256);

        red2 = (int) (Math.random() * 256);
        green2 = (int) (Math.random() * 256);
        blue2 = (int) (Math.random() * 256);
        repaint();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g); // ***** Don't forget this *****
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        Color startColor = new Color(red1, green1, blue1);
        Color endColor = new Color(red2, green2, blue2);

        GradientPaint gradient = new GradientPaint(70, 70, startColor, 150, 150, endColor);
        g2d.setPaint(gradient);
        g2d.fillOval(OVAL_X, OVAL_X, OVAL_W, OVAL_W);
    }
}

另外,请勿忘记在覆盖中拨打super.paintComponent(g);(请参阅上面的代码)