Java JFrame在更改背景颜色时闪烁
我正在制作一个每750毫秒改变背景颜色的窗口。。。但是Java JFrame在更改背景颜色时闪烁,java,multithreading,swing,jframe,setbackground,Java,Multithreading,Swing,Jframe,Setbackground,我正在制作一个每750毫秒改变背景颜色的窗口。。。但是JFrame像这样闪烁 我想要这样的 我找到了如下解决方案: 1.-Frame.getContentPane().setBackground(颜色) 2.-制作一条新线 3.-Frame.getContentPane().repaint() 但它不起作用 我的代码 Thread ciclo=新线程(new Runnable()){ 我怎样才能解决这个问题? 感谢您的建议您试图在后台线程中更改Swing状态,这违反了Swing线程规则
JFrame
像这样闪烁
我想要这样的
我找到了如下解决方案:
1.-Frame.getContentPane().setBackground(颜色)
2.-制作一条新线
3.-Frame.getContentPane().repaint()
但它不起作用 我的代码 Thread ciclo=新线程(new Runnable()){ 我怎样才能解决这个问题?
感谢您的建议您试图在后台线程中更改Swing状态,这违反了Swing线程规则。请使用Swing计时器,因为这可能会解决您的问题
new Timer(750, new ActionListener() {
public void actionPerformed(ActionEvent e) {
contentPane.setBackground((Color.getHSBColor((c/360), 1, 1));
contentPane.repaint();
c= (c >= 360) ? 1 : c + 5;
}
}).start();
编辑或更好:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
@SuppressWarnings("serial")
public class BackgroundColorChange extends JPanel {
private static final int PREF_W = 400;
private static final int PREF_H = PREF_W;
private static final int TIMER_DELAY = 75;
public BackgroundColorChange() {
new Timer(TIMER_DELAY, new ActionListener() {
private int c = 1;
@Override
public void actionPerformed(ActionEvent arg0) {
setBackground(Color.getHSBColor((float) c / 360, 1f, 1f));
repaint();
c = (c >= 360) ? 1 : c + 5;
}
}).start();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
private static void createAndShowGui() {
BackgroundColorChange mainPanel = new BackgroundColorChange();
JFrame frame = new JFrame("BackgroundColorChange");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
@SuppressWarnings("serial")
public class BackgroundColorChange extends JPanel {
private static final int PREF_W = 400;
private static final int PREF_H = PREF_W;
private static final int TIMER_DELAY = 75;
public BackgroundColorChange() {
new Timer(TIMER_DELAY, new ActionListener() {
private int c = 1;
@Override
public void actionPerformed(ActionEvent arg0) {
setBackground(Color.getHSBColor((float) c / 360, 1f, 1f));
repaint();
c = (c >= 360) ? 1 : c + 5;
}
}).start();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
private static void createAndShowGui() {
BackgroundColorChange mainPanel = new BackgroundColorChange();
JFrame frame = new JFrame("BackgroundColorChange");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}