我在JFrame中有一个简单的JPanel和JButton。按下按钮时,我希望它闪烁几次。主要问题是我尝试了几种不同的方法,使用Thread.sleep(200)和numBlinks的for循环。然而,这只是暂停"面板在切换颜色时只显示最终结果。
谢谢。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BlinkingJPanel implements ActionListener{
private JFrame window;
private JPanel panel;
private JButton button;
private final Color BACKGROUND1 = Color.GRAY;
private final Color BACKGROUND2 = Color.WHITE;
private final Color BORDER_COLOR = Color.BLACK;
private final int BORDER_SIZE = 1;
private final int WINDOW_SIZE = 200;
private final int PANEL_SIZE = 100;
private final int BUTTON_SIZE = 100;
public static void main(String[] args) {
BlinkingJPanel main = new BlinkingJPanel();
}
public BlinkingJPanel() {
setUpWindow();
setUpPanel();
setUpButton();
window.add(panel, BorderLayout.CENTER);
window.add(button, BorderLayout.NORTH);
}
private void setUpWindow() {
window = new JFrame("Blinking JPanel");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBackground(BACKGROUND1);
window.setSize(WINDOW_SIZE, WINDOW_SIZE);
window.setVisible(true);
}
private void setUpPanel() {
panel = new JPanel();
panel.setSize(PANEL_SIZE, PANEL_SIZE);
panel.setBorder(BorderFactory.createLineBorder(BORDER_COLOR, BORDER_SIZE));
panel.setBackground(BACKGROUND1);
panel.setVisible(true);
}
private void setUpButton() {
button = new JButton("Blink");
button.setSize(BUTTON_SIZE, BUTTON_SIZE / 2);
button.setActionCommand("Button1");
button.addActionListener(this);
button.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
switch(e.getActionCommand()) {
case "Button1": blink(1);
break;
default: System.out.println("Default Case");
break;
}
}
private void blink(int numBlinks) {
// TODO: make panel blink on it's own
if(panel.getBackground() == BACKGROUND1) {
panel.setBackground(BACKGROUND2);
} else {
panel.setBackground(BACKGROUND1);
}
}
}
答案 0 :(得分:1)
我使用Timer创建了一个解决方案。我试图保持简单。我所做的就是添加一个计时器,当你启动计时器时,它需要你想要的时间弹出它们(它们每隔几毫秒就会弹出,我把它设置为300毫秒)和一个我刚刚使用该类本身的actionlistener。当计时器弹出时,它会触发actionlistener,使其闪烁,直到顶部的BLINK_AMOUNT中的金额已被重新计算。然后停止计时器并将闪烁计数重置为0.定时器仅在您按下按钮时启动。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class BlinkingJPanel implements ActionListener{
private JFrame window;
private JPanel panel;
private JButton button;
private final Color BACKGROUND1 = Color.GRAY;
private final Color BACKGROUND2 = Color.WHITE;
private final Color BORDER_COLOR = Color.BLACK;
private final int BORDER_SIZE = 1;
private final int WINDOW_SIZE = 200;
private final int PANEL_SIZE = 100;
private final int BUTTON_SIZE = 100;
private final int BLINK_AMOUNT = 1;
private Timer blinkTimer;
private int blinkCount;
public static void main(String[] args) {
BlinkingJPanel main = new BlinkingJPanel();
}
public BlinkingJPanel() {
setUpWindow();
setUpPanel();
setUpButton();
window.add(panel, BorderLayout.CENTER);
window.add(button, BorderLayout.NORTH);
//Timer constructor takes pop time and action listener
blinkTimer = new Timer(300, this);
blinkCount = 0;
window.setVisible(true);
}
private void setUpWindow() {
window = new JFrame("Blinking JPanel");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setBackground(BACKGROUND1);
window.setSize(WINDOW_SIZE, WINDOW_SIZE);
}
private void setUpPanel() {
panel = new JPanel();
panel.setSize(PANEL_SIZE, PANEL_SIZE);
panel.setBorder(BorderFactory.createLineBorder(BORDER_COLOR, BORDER_SIZE));
panel.setBackground(BACKGROUND1);
panel.setVisible(true);
}
private void setUpButton() {
button = new JButton("Blink");
button.setSize(BUTTON_SIZE, BUTTON_SIZE / 2);
button.setActionCommand("Button1");
button.addActionListener(this);
button.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(button)){
blinkTimer.start();
}
else if(e.getSource() instanceof Timer){
if(blinkCount == BLINK_AMOUNT){
blinkCount = 0;
blinkTimer.stop();
}
blink();
blinkCount++;
}else{
System.out.println("Default");
}
}
private void blink() {
if(panel.getBackground() == BACKGROUND1) {
panel.setBackground(BACKGROUND2);
} else {
panel.setBackground(BACKGROUND1);
}
}
}
如果其中任何一个令人困惑,请随时提出。
正如你所说,thread.sleep()不会在所有swing运行在一个线程上,因此调用该函数将使整个线程停止运行(执行代码),然后运行代码的下一部分。
希望这有帮助。