展开窗口时触发actionPerformed?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (210)

我的代码:

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);
}
}
提问于
用户回答回答于

行动执行是最明确的不被触发。更确切地说repaint()是被触发的。你的问题是,你正在油漆组件方法中随机化,而且你不应该从这个方法中更改类的状态,因为你无法完全控制何时甚至是如果它应该或者可以被称为。

相反,在actionPerformate方法中执行随机化,设置红色、蓝色和绿色实例字段,调用重新绘制,并在油漆组件方法中使用这些字段。

类似于:

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);
    }
}

扫码关注云+社区

领取腾讯云代金券