0

我看了很多地方,不知道如何自动刷新我的框架。当我单击按钮时,它确实有效(因为我已经在那里使用了许多示例),但不是自动的。

我想创建一个扫雷游戏,并在其中添加计时器,除计时器计数外,一切正常,但仅在单击按钮时更新。因此,简单地重做布局是行不通的......

艰难我知道我的计时器可以工作,如果我把 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);
}
}
4

2 回答 2

1

益智游戏中的JPanel没有更新

更改组件后,您需要通过调用“刷新” Swing 组件invalidate() or revalidate()

于 2013-04-05T18:23:31.263 回答
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);
}
}
于 2013-04-08T20:18:02.343 回答