gpt4 book ai didi

java - actionPerformed 在展开窗口时触发

转载 作者:行者123 更新时间:2023-11-29 04:18:34 27 4
gpt4 key购买 nike

我试图更好地理解 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();
}

}

我的绘图面板

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

最佳答案

不,actionPerformed 绝对不会被触发。而是触发了 repaint()。你的问题是你在 paintComponent 方法中随机化,你永远不应该从这个方法改变类的状态,因为你不能完全控制什么时候甚至 if 它应该或可以打电话。

而是在 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);(参见上面的代码)

关于java - actionPerformed 在展开窗口时触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50710716/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com