修改或循环后平滑组件自动刷新(例如:计时器)

时间:2013-04-05 17:32:20

标签: java swing timer jframe jpanel

我看了很多地方,无法找到如何自动刷新我的画面。当我点击按钮时,它确实有效(因为我已经使用了许多例子),但不是自动的。

我想创建一个MineSweeper游戏,我在其中添加了Timer,除了计时器计数之外一切正常,但只有在点击按钮时才会更新。因此,简单地重做布局将无法正常工作......

我知道我的计时器有效,如果我把System.out放入其中,我会以稳定的二级速率看到它。

我会在下面放一个小程序,而不是我的游戏,因为代码太长了,但是我构建了一个关于相同情况的小程序,修复它将解决整个问题。

在这种情况下,我的其他程序中没有完善的按钮,但只有在我弄乱窗口时才会刷新时间。

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ClockTest {
JFrame frame = new JFrame();
JButton test1 = new JButton();
JTextField timer1 = new JTextField();
int hour = 0, min = 0, sec = 0;
String timing;

public ClockTest() {
    frame.setLayout(new BorderLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(center(), BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
}

public Component center() {
    JPanel pan = new JPanel();
    pan.setLayout(new GridLayout(1,2));
    test1.setText("Dummy");
    timer1.setEditable(false);
    timer1.setText(timing);
    pan.add(test1);
    pan.add(timer1);
    return pan;
}

public void count() {
    sec++;
    if(sec == 60) {
        sec -= 60;
        min++;
    }
    if(min == 60) {
        min -= 60;
        hour++;
    }
    timing = hour + ":" + min + " : " + sec;
//This is what i do in my game to refresh, to set flags, get mine "exploded"...
//But since i'm not pressing anything... it does not happen.
    frame.add(center(), BorderLayout.CENTER);
}

public static void main(String[]args) {
    final   ClockTest test1 = new ClockTest();
    TimerTask task = new TimerTask() {
        public void run() {
            test1.count();
        }
    };
    Timer t = new Timer();
    t.schedule(task,0,1000);
}
}

2 个答案:

答案 0 :(得分:1)

JPanel in puzzle game not updating

更改组件后,您需要通过调用invalidate() or revalidate()

来“刷新”Swing组件

答案 1 :(得分:0)

以下是JavaNewb想法使用revalidate();

的答案
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

public class ClockTest {
JFrame frame = new JFrame();
JButton test1 = new JButton();
JTextField timer1 = new JTextField();
int hour = 0, min = 0, sec = 0;
String timing;

public ClockTest() {
    frame.setLayout(new BorderLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(center(), BorderLayout.CENTER);
    frame.setResizable(true);
    frame.pack();
    frame.setVisible(true);
}

public JPanel center() {
    JPanel pan = new JPanel();
    pan.setLayout(new GridLayout(1,2));
    test1.setText("Dummy");
    timer1.setEditable(false);
    timer1.setText(timing);
    pan.add(test1);
    pan.add(timer1);
    return pan;
}

public void count() {
    sec++;
    if(sec == 60) {
        sec -= 60;
        min++;
    }
    if(min == 60) {
        min -= 60;
        hour++;
    }
    timing = " " + hour + ":" + min + " : " + sec;
// Make the modification here on your components, then .revalidate(); them all
    timer1.setText(timing);
    timer1.revalidate();
}
public static void main(String[]args) {
    final   ClockTest test1 = new ClockTest();
    TimerTask task = new TimerTask() {
        public void run() { 
            test1.count();
        }
    };
    Timer t = new Timer();
    t.schedule(task,0,1000);
}
}