簡體   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