第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

多次更改JPanel的背景顏色

多次更改JPanel的背景顏色

四季花海 2024-01-25 21:40:30
我正在嘗試制作一個小程序,其中包括根據(jù)時間更改面板的顏色。現(xiàn)在我只是嘗試做那部分而不做其余的。所以我只寫了一個只有一個面板的小界面,我想在循環(huán)內(nèi)多次更改顏色。問題是,即使線程暫停正確的時間,面板的顏色也不會正確更改。它只是有時在循環(huán)中改變,而不是每次都改變。我的接口類:import javax.swing.*;import java.awt.*;//creates the Interfacepublic class Interface extends JFrame {    private JPanel frame1;    public Interface (String titel) {        super(titel);        setSize(600, 400);        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        this.frame1 = new JPanel();        this.frame1.setPreferredSize(new Dimension (200, 200));        setLayout(new FlowLayout());        add(frame1);        this.setVisible(true);    }    public JPanel getFrame1() {        return frame1;    }}我的暫停課程:import java.util.TimerTask;//supposed to pause the thread by @pause amount of millisecondspublic class Pause extends TimerTask {    private int pause;    public Pause(int pause){        this.pause = pause;    }    @Override    public void run() {        System.out.println("Timer"+ pause+" task started at:"+System.currentTimeMillis());        pause();        System.out.println("Timer task"+ pause+" ended at:"+System.currentTimeMillis());    }    public void pause() {        try {            Thread.sleep(this.pause);        } catch (InterruptedException e) {            e.printStackTrace();        }    }}我的眨眼課import javax.swing.*;import java.awt.*;public class Blink {    private JPanel frame1;    public Blink(Interface anInterface){        this.frame1 = anInterface.getFrame1();    }    // blink should change the color of the JPanel inside my Frame.     // Its supposed to change to red for 200 ms    // and then to white again for 1000 ms.    // this should be repeated 10 times.    public void blink() {        Pause pause1 = new Pause(200);        Pause pause2 = new Pause(1000);        pause2.run();        }    }
查看完整描述

1 回答

?
搖曳的薔薇

TA貢獻1793條經(jīng)驗 獲得超6個贊

根據(jù)Swing 的并發(fā)性,您不能簡單地Thread.sleep確定ThreadGUI 運行的位置,因為它會凍結 GUI,因此事件無法發(fā)生。相反,對于任何類型的動畫或長時間繁重的任務(視為Thread.sleep其中之一),應使用Swing Timers和Swing Workers 。對于你的情況,ajavax.swing.Timer更適合。

其用法的一個例子:

public class Blink {

? ? private JPanel frame1;

? ? private int pause1TimesRan;

? ? private int pause2TimesRan;


? ? private Timer pauser1, pauser2;


? ? public Blink(Interface anInterface) {

? ? ? ? this.frame1 = anInterface.getFrame1();

? ? ? ? //Create pauser 1 with delay 200ms

? ? ? ? pauser1 = new Timer(200, e -> {

? ? ? ? ? ? if (pause1TimesRan == 10) {

? ? ? ? ? ? ? ? pauser1.stop();

? ? ? ? ? ? ? ? return;

? ? ? ? ? ? }

? ? ? ? ? ? Color color = randomColor();

? ? ? ? ? ? frame1.setBackground(color);

? ? ? ? ? ? System.out.println("Pauser #1 changed background to: " + color);

? ? ? ? ? ? pause1TimesRan++;

? ? ? ? });

? ? ? ? //Create pauser 2 with delay 1000ms

? ? ? ? pauser2 = new Timer(1000, e -> {

? ? ? ? ? ? if (pause2TimesRan == 10) {

? ? ? ? ? ? ? ? pauser2.stop();

? ? ? ? ? ? ? ? return;

? ? ? ? ? ? }

? ? ? ? ? ? Color color = randomColor();

? ? ? ? ? ? frame1.setBackground(color);

? ? ? ? ? ? System.out.println("Pauser #2 changed background to: " + color);

? ? ? ? ? ? pause2TimesRan++;

? ? ? ? });

? ? }


? ? private static Color randomColor() {

? ? ? ? return new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255));

? ? }


? ? public void blink() {

? ? ? ? pauser1.start();

? ? ? ? pauser2.start();

? ? }


? ? public static void main(String[] args) {

? ? ? ? SwingUtilities.invokeLater(() -> {

? ? ? ? ? ? Interface anInterface = new Interface("Title");

? ? ? ? ? ? anInterface.setVisible(true);

? ? ? ? ? ? Blink blink = new Blink(anInterface);

? ? ? ? ? ? blink.blink();

? ? ? ? });

? ? }


? ? static class Interface extends JFrame {

? ? ? ? private JPanel frame1;


? ? ? ? public Interface(String titel) {

? ? ? ? ? ? super(titel);

? ? ? ? ? ? setSize(600, 400);

? ? ? ? ? ? setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

? ? ? ? ? ? this.frame1 = new JPanel();

? ? ? ? ? ? this.frame1.setPreferredSize(new Dimension(200, 200));

? ? ? ? ? ? setLayout(new FlowLayout());

? ? ? ? ? ? add(frame1);

? ? ? ? ? ? this.setVisible(true);

? ? ? ? }


? ? ? ? public JPanel getFrame1() {

? ? ? ? ? ? return frame1;

? ? ? ? }


? ? }

}

一個題外話的建議是正確命名你的方法(和變量)。您調(diào)用了該方法getFrame1(),但它實際上是 aJPanel而不是 a?JFrame。所以,更好的名字可能是getPanel().?


查看完整回答
反對 回復 2024-01-25
  • 1 回答
  • 0 關注
  • 136 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號