繁体   English   中英

运行重绘方法后,JAVA swing GUI窗口变得震撼

[英]JAVA swing gui window gets jarred after repaint method is run

我开始学习Java Swing。 我试图创建,其中有2个按钮,一个GUI changeColor在底部和changeLabel右侧。 它的右边有一个标签,中间是一个JPanel ,它显示了渐变色的椭圆形。

当我单击changeLabel ,它可以正常工作并更改左侧的标签。 但是,当我单击changeColor ,会出现一个新的椭圆形,并且整个布局中断了,并叠加了一些新面板。 我正在看一本书,其中给出了相同的内容,但是它在paintComponent方法中使用了随机颜色生成,这不是我要做的事情。 该方法工作正常,但我尝试避免这种情况,并制作了一个单独的方法。 虽然这不起作用。

GUI类:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TwoButtons {
    public JFrame frame;
    private JLabel label;
    private MyDrawPanel panel;
    private boolean clicked = false;

    public static void main(String[] args) {
        TwoButtons gui = new TwoButtons();
        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 color");
        colorButton.addActionListener(new ColorListener());

        label = new JLabel("I'm a label");
        panel = new MyDrawPanel();
        frame.getContentPane().add(BorderLayout.SOUTH, colorButton);
        frame.getContentPane().add(BorderLayout.CENTER, panel);
        frame.getContentPane().add(BorderLayout.EAST, labelButton);
        frame.getContentPane().add(BorderLayout.WEST, label);

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

    class LabelListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (!clicked) {
                label.setText("Ouch!! (Click again to revert)");
                clicked = true;
            } else {
                clicked = false;
                label.setText("Change Label");
            }
        }
    }

    class ColorListener implements ActionListener {
       @Override//
       public void actionPerformed(ActionEvent e) {
           //frame.repaint();
               panel.changeColors();
    }
}
}

着色等级:

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

public class MyDrawPanel  extends  JPanel{
    private Color startColor,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);
        startColor = new Color(red, green, blue);
        red = (int) (Math.random() * 255);
        green = (int) (Math.random() * 255);
        blue = (int) (Math.random() * 255);
        endColor = new Color(red, green, blue);
        this.repaint();

    }

}

点击更改颜色之前

原始图形用户界面

单击更改颜色后

单击更改颜色后

为了避免呈现伪像:

public void paintComponent(Graphics g){ .. 

应该:

public void paintComponent(Graphics g){ 
    super.paintComponent(g); .. 

通过调用super方法,它将自动重新绘制背景和边框等,从而擦除较早的图形。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM