反复改变框架背景颜色

发布于 2025-01-17 12:41:23 字数 710 浏览 1 评论 0原文

因此,我希望我的方法每次更改框架的背景颜色,例如0.2秒,我尝试了很多不同的事情,例如新方法和威胁睡眠/等待,但是即使使用System.out.out.print进行尝试,也没有任何效果。我不知道现在可以起作用,所以如果有人帮助我,那将是ICE,谢谢您!

ps。:不要介意变量和方法名称与我的朋友xD玩得开心

public void poggersModeOn()
{
    f.setTitle("Insane rainbow pogg mode!!!!1!!!11!!1!! ");
    int rot = (int)(Math.random()*255+1);
    int gruen = (int)(Math.random()*255+1);
    int blau = (int)(Math.random()*255+1);
    f.setBackground(new Color(rot, gruen, blau));
}

public void poggersModeOff()
{
    f.setTitle("Navigationssystem");
    f.setBackground(new Color(255, 255, 255));
}

public void pogmethode() {
    while(pogmode==true)
    {
        poggersModeOn();
        //wait function right here
    }
}

So i want my methods to change the background color of my Frame every like 0.2 seconds and i tried a lot of diferent things like new methods and Threat sleep/wait but nothing worked even though trying it with System.out.print did. I have no idea what could work now so would be ice if someone helped me out, thank you beforehand!

PS.: dont mind the variable and method names was having fun with my friends xD

public void poggersModeOn()
{
    f.setTitle("Insane rainbow pogg mode!!!!1!!!11!!1!! ");
    int rot = (int)(Math.random()*255+1);
    int gruen = (int)(Math.random()*255+1);
    int blau = (int)(Math.random()*255+1);
    f.setBackground(new Color(rot, gruen, blau));
}

public void poggersModeOff()
{
    f.setTitle("Navigationssystem");
    f.setBackground(new Color(255, 255, 255));
}

public void pogmethode() {
    while(pogmode==true)
    {
        poggersModeOn();
        //wait function right here
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

依 靠 2025-01-24 12:41:23

假设您使用 Swing,那么您应该使用 Swing Timer,请参阅 如何使用 Swing Timers

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Main {
    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            Timer timer = new Timer(250, new ActionListener() {
                private Random rnd = new Random();
                @Override
                public void actionPerformed(ActionEvent e) {
                    int red = rnd.nextInt(255);
                    int green = rnd.nextInt(255);
                    int blue = rnd.nextInt(255);
                    setBackground(new Color(red, green, blue));
                }
            });
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }
    }
}

为什么?因为 Swing 是单线程的(并且不是线程安全的)。有关更多详细信息,请参阅 Swing 中的并发

Assuming your using Swing, then you should use a Swing Timer, see How to Use Swing Timers

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Main {
    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            Timer timer = new Timer(250, new ActionListener() {
                private Random rnd = new Random();
                @Override
                public void actionPerformed(ActionEvent e) {
                    int red = rnd.nextInt(255);
                    int green = rnd.nextInt(255);
                    int blue = rnd.nextInt(255);
                    setBackground(new Color(red, green, blue));
                }
            });
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }
    }
}

Why? Because Swing is single threaded (and not thread safe). See Concurrency in Swing for more details

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文