最小化/最大化Java Swing GUI执行actionPerformed方法

时间:2018-07-31 17:14:36

标签: java swing

所以我是Java swing gui的完整入门者。我将以Head First Java作为入门书。我制作了一个简单的gui,其中有一个按钮,然后按它会在其上方的圆上创建不同的渐变。代码都在书中。当我单击按钮时,它工作正常。但是,当我最大化/最小化gui时,它的作用就像是按下了按钮并且渐变发生了变化。为什么会这样?

GUI代码:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class SimpleGuiC implements ActionListener {
    JFrame frame;

    public static void main(String[] args) {
    SimpleGuiC gui=new SimpleGuiC();
    gui.go();
}
    public  void go(){
    frame=new JFrame();
    frame.setTitle("Gradient changer");
    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);
}

    @Override
    public void actionPerformed(ActionEvent e) {

        frame.repaint();
    }
}

随机梯度生成代码:

import java.awt.*;
import  javax.swing.*;
public class MyDrawPanel extends  JPanel{
public void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    int red = (int) (Math.random() * 255);
    int green = (int) (Math.random() * 255);
    int blue = (int) (Math.random() * 255);
    Color startColor = new Color(red, green, blue);
    red = (int) (Math.random() * 255);
    green = (int) (Math.random() * 255);
    blue = (int) (Math.random() * 255);
    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);
    }

}

Minimized version After maximizing

2 个答案:

答案 0 :(得分:3)

这是我的评论中建议的实现方式:

public final class MyDrawPanel extends JPanel {

    private Color startColor;
    private Color endColor;

    public MyDrawPanel() {
        this.changeColors();
    }

    public void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        GradientPaint gradient = new GradientPaint(70, 70, startColor, 150, 150, endColor);
        g2d.setPaint(gradient);
        g2d.fillOval(70, 70, 100, 100);
    }

    public void changeColors() {
        int red = (int) (Math.random() * 255);
        int green = (int) (Math.random() * 255);
        int blue = (int) (Math.random() * 255);
        this.startColor = new Color(red, green, blue);

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

        this.repaint();
    }
}

如您所见,面板具有状态(即字段),其中包含圆必须具有的颜色。这些颜色在paintComponent()中不会改变。它们仅在调用changeColors()方法时更改。

public class SimpleGuiC implements ActionListener {
    private JFrame frame;
    private MyDrawPanel drawPanel;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            SimpleGuiC gui = new SimpleGuiC();
            gui.go();
        });
    }

    public void go() {
        frame = new JFrame();
        frame.setTitle("Gradient changer");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton button = new JButton("Change colors");
        button.addActionListener(this);
        this.drawPanel = new MyDrawPanel();
        frame.getContentPane().add(BorderLayout.SOUTH, button);
        frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
        frame.setSize(300, 300);
        frame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        this.drawPanel.changeColors();
    }
}

而且,如您在此处看到的,actionPerformed()方法更改面板的状态(即面板必须显示的颜色)。它执行按钮所说的操作:更改颜色。每次重新粉刷面板时,它将始终使用上次调用changeColors()时设置的颜色。

答案 1 :(得分:1)

评论中的人告诉您它为什么发生。您现在要做的就是创建一个布尔值,该布尔值将告诉您是否按下了该布尔值,然后进行检查。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class SimpleGuiC implements ActionListener {
    JFrame frame;

    MyDrawPanel drawPanel;

    public static void main(String[] args) {
    SimpleGuiC gui=new SimpleGuiC();
    gui.go();
}
    public  void go(){
    frame=new JFrame();
    frame.setTitle("Gradient changer");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button=new JButton("Change colors");
    button.addActionListener(this);
    drawPanel = new MyDrawPanel();
    frame.getContentPane().add(BorderLayout.SOUTH,button);
    frame.getContentPane().add(BorderLayout.CENTER,drawPanel);
    frame.setSize(300,300);
    frame.setVisible(true);
}

    @Override
    public void actionPerformed(ActionEvent e) {
        drawPanel.buttonPressed = true;

        frame.repaint();
    }
}

DrawPanel:

import java.awt.*;
import  javax.swing.*;
public class MyDrawPanel extends  JPanel{

public boolean buttonPressed = false;

private GradientPaint gradient;

public MyDrawPanel () {
    int red = (int) (Math.random() * 255);
    int green = (int) (Math.random() * 255);
    int blue = (int) (Math.random() * 255);
    Color startColor = new Color(red, green, blue);
    red = (int) (Math.random() * 255);
    green = (int) (Math.random() * 255);
    blue = (int) (Math.random() * 255);
    Color endColor = new Color(red, green, blue);
    gradient = new GradientPaint(70, 70, startColor, 150, 150, endColor);
}

public void paintComponent(Graphics g) {
    if (buttonPressed) {
        int red = (int) (Math.random() * 255);
        int green = (int) (Math.random() * 255);
        int blue = (int) (Math.random() * 255);
        Color startColor = new Color(red, green, blue);
        red = (int) (Math.random() * 255);
        green = (int) (Math.random() * 255);
        blue = (int) (Math.random() * 255);
        Color endColor = new Color(red, green, blue);
        gradient = new GradientPaint(70, 70, startColor, 150, 150, endColor);


        buttonPressed = false;
    }
    Graphics2D g2d = (Graphics2D) g;
    g2d.setPaint(gradient);
    g2d.fillOval(70, 70, 100, 100);
}

}

基本上,每次按下按钮时,布尔值将设置为true。然后在重新绘制时,它将检查它是否为真,如果是,它将对其进行绘制并将其设置为false以便再次进行。祝你好运!

编辑:

使其不消失。您需要分离代码以更改渐变和绘画。因此,每次绘制一次,但仅在按下按钮时更改渐变。我希望我刚刚更新的代码能够正常工作。

编辑2:

别忘了封装该变量,我不是因为着急,而只是将其设为私有并添加getter和setters!