gpt4 book ai didi

java - 几次repaint()后如何重绘JComponent?

转载 作者:行者123 更新时间:2023-12-04 10:44:13 24 4
gpt4 key购买 nike

我的程序在 JButton 列表上循环。对于每个 JButton,它每秒都会重新绘制它:

public class a {
static JFrame frame = new JFrame();
static Set<MyButton> components = new HashSet<MyButton>();

public static void main(String[] args) {
JPanel lPanel = new JPanel(new GridLayout(0,18));

for (int i = 0; i < 500; i++) {
MyButton lButton = new MyButton("Hello" + i);
lPanel.add(lButton);
components.add(lButton);
}
frame.add(lPanel);
frame.pack();
frame.setVisible(true);
blinking();
}

public static void blinking() {
Timer blinkTimer = new Timer(500, new ActionListener() {
boolean on = false;

public void actionPerformed(ActionEvent e) {
for (MyButton lButton : components) {
lButton.blink(on);
}
on = !on;
}
});
blinkTimer.start();
}
}

public class MyButton extends JButton {

public MyButton(String text) {
super(text);
}

public void blink(boolean on) {
setBackground(on ? Color.DARK_GRAY : Color.LIGHT_GRAY);
setForeground(on ? Color.WHITE : Color.BLACK);
}
}

结果:
enter image description here

我想确保所有按钮同时更新它们的背景和前景色。
JButton extends JComponent , 和 JComponent.setBackground来电 repaint() ,以及 JComponent.setForeground .

因此,我的程序每秒都会调用 repaint()每个按钮两次,颜色并不总是同时更新。

如何确保所有按钮的颜色同时更新?我认为解决方案是只调用一个 repaint() ,也许在按钮容器上,但我该怎么做呢?

最佳答案

按钮都在面板的同一层吗?那么为什么不使用该层的背景而不是按钮的背景呢?

例如:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Main {
private static JButton newButton() {
final JButton b = new JButton("x");
b.setContentAreaFilled(false);
//b.setOpaque(false); //Do not make this call, because setContentAreaFilled documentation says so.
return b;
}

public static void main(final String[] args) {
final JButton tester = newButton();
final Dimension testerDim = tester.getPreferredSize();
final Dimension maxDim = new Dimension(1200, 700);

final int rows = maxDim.width / testerDim.width,
cols = maxDim.height / testerDim.height;

final JPanel buttonPanel = new JPanel(new GridLayout(0, cols));
final int n = rows * cols;
buttonPanel.add(tester);
for (int i = 1; i < n; ++i)
buttonPanel.add(newButton());

final JFrame frame = new JFrame("Button grid");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(buttonPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

final Color bg = buttonPanel.getBackground();
final Timer timer = new Timer(500, new ActionListener() {
private boolean on = false;

@Override
public void actionPerformed(final ActionEvent e) {
buttonPanel.setBackground(on? bg: bg.darker());
on = !on;
}
});
timer.setRepeats(true);
timer.start();
}
}

我认为这是你可以用单个核心/线程做的最好的事情。

此外,如果您想要按钮之间的其他组件,它们的背景与图层的背景不同,那么只需将它们设为不透明( setOpaque(true)),如果它们还没有。

关于java - 几次repaint()后如何重绘JComponent?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59789104/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com