如何重新加载JFrame-Java

如何重新加载JFrame-Java,java,swing,jframe,Java,Swing,Jframe,我正在用java编程,我想刷新我的Jframe并通过循环更改颜色,但我无法重新加载帧,但我只能创建一个新的帧 package frames; import java.awt.Color; import javax.swing.*; public class Frame1 { public static void main(String[] args) { int num = 0; while (num<255) { num +=1;

我正在用java编程,我想刷新我的
Jframe
并通过循环更改颜色,但我无法重新加载帧,但我只能创建一个新的帧

package frames;
import java.awt.Color;

import javax.swing.*;

public class Frame1 {
public static void main(String[] args) 
{
    int num = 0;



    while (num<255)
    {
        num +=1;
        JFrame frame = new JFrame();
        frame.setSize(400,300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setBackground(new Color(num,num,num));
        frame.setTitle("");

    }
}
}
封装框架;
导入java.awt.Color;
导入javax.swing.*;
公共类框架1{
公共静态void main(字符串[]args)
{
int num=0;

当(num像这样时,将只创建一个帧,您可以调整颜色并进行所需的刷新

package frames;
import java.awt.Color;

import javax.swing.*;

public class Frame1 {
public static void main(String[] args) 
{
    int num = 0;

    JFrame frame = new JFrame();
    frame.setSize(400,300);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setTitle("");

    while (num<255)
    {
        num +=1;

        SwingUtilities.invokeLater(new Runnable() {
         public void run() {
         // Here, we can safely update the GUI
         // because we'll be called from the
         // event dispatch thread
          frame.getContentPane().setBackground(new Color(num,num,num));
          frame.pack();
          frame.repaint();
         }
        }
}
  });


    }
}
}
封装框架;
导入java.awt.Color;
导入javax.swing.*;
公共类框架1{
公共静态void main(字符串[]args)
{
int num=0;
JFrame=新JFrame();
框架。设置尺寸(400300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle(“”);
while(num
  • 您只需要使用一个帧(您正在创建255个帧)
  • 不要使用while循环尝试更改背景。请改用Swing计时器。请参阅
  • 在事件调度线程上运行所有Swing应用程序。请参阅
  • 下面是用这三点重构的代码

    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    import javax.swing.Timer;
    
    public class Frame1 {
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    final JFrame frame = new JFrame();
                    frame.getContentPane().setBackground(
                            new Color(0, 0, 0));
                    Timer timer = new Timer(10, new ActionListener() {
                        int num = 0;
                        public void actionPerformed(ActionEvent e) {
                            if (num > 255) {
                                ((Timer) e.getSource()).stop();
                            } else {
                                frame.getContentPane().setBackground(
                                        new Color(num, num, num));
                                num++;
                            }
                        }
                    });
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setSize(300, 300);
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                    timer.start();
                }
            });
        }
    }
    
    您是否尝试调用JFrame#invalidate()然后调用JFrame#validate()?