Java 为什么';摆动计时器不能工作吗?
我正在用JButtons构建一个网格50x50。我想随机选择颜色(选择蓝色和红色之间)。这一共重复了3次(我使用buildGUI构建初始网格)。但由于某种原因,它不起作用。它显示网格的初始构建,但不更新3次。我还尝试重新绘制()。但这也无济于事Java 为什么';摆动计时器不能工作吗?,java,user-interface,Java,User Interface,我正在用JButtons构建一个网格50x50。我想随机选择颜色(选择蓝色和红色之间)。这一共重复了3次(我使用buildGUI构建初始网格)。但由于某种原因,它不起作用。它显示网格的初始构建,但不更新3次。我还尝试重新绘制()。但这也无济于事 import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.*; import javax
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.Timer;
import java.util.Random;
class Counter {
int n = 0;
JFrame frame = new JFrame("GridLayout demo");
JPanel panel = new JPanel();
JButton[][] buttons = new JButton[50][50];
int[][] isCooperating = new int[50][50];
Random rand = new Random();
void buildGUI() {
for (int x = 0; x < 50; x++) {
for (int y = 0; y < 50; y++) {
int randomNumber = rand.nextInt(2); // randomly chooses between 1 and 0; for cooperating status
isCooperating[x][y] = randomNumber; // 1 is cooperating
}
}
panel.setLayout(new GridLayout(50,50));
for (int x = 0; x < 50; x++) {
for (int y = 0; y < 50; y++) {
JButton btn = new JButton();
if (isCooperating[x][y] == 0) {
btn.setBackground(Color.RED);
btn.setOpaque(true);
btn.setBorder(null);
} else if (isCooperating[x][y] == 1) {
btn.setBackground(Color.BLUE);
btn.setOpaque(true);
btn.setBorder(null);
}
btn.setPreferredSize(new Dimension(10, 10));
panel.add(btn);
buttons[x][y] = btn;
}
}
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(800,800);
frame.setVisible(true);
}
public void solve() {
n = 0;
buildGUI();
ActionListener actListner = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
while (n!=3) {
buildGUI();
n++;
}
}
};
Timer timer = new Timer(500, actListner);
timer.start();
}
public static void main(String[]args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
(new Counter()).solve();
}
});
}
}
import java.awt.*;
导入java.awt.event.ActionListener;
导入java.awt.event.ActionEvent;
导入javax.swing.*;
导入javax.swing.Timer;
导入java.util.Random;
类计数器{
int n=0;
JFrame=新JFrame(“网格布局演示”);
JPanel面板=新的JPanel();
JButton[][]按钮=新JButton[50][50];
int[]isCooperating=新int[50][50];
Random rand=新的Random();
void buildGUI(){
对于(int x=0;x<50;x++){
对于(int y=0;y<50;y++){
int randomNumber=rand.nextInt(2);//在1和0之间随机选择;用于协作状态
isCooperating[x][y]=randomNumber;//1正在合作
}
}
面板设置布局(新网格布局(50,50));
对于(int x=0;x<50;x++){
对于(int y=0;y<50;y++){
JButton btn=新JButton();
如果(i合作[x][y]==0){
背景(颜色为红色);
btn.setOpaque(真);
btn.订单(空);
}else if(i合作[x][y]==1){
背景(蓝色);
btn.setOpaque(真);
btn.订单(空);
}
btn.setPreferredSize(新尺寸(10,10));
面板。添加(btn);
按钮[x][y]=btn;
}
}
框架。添加(面板);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
框架设置尺寸(800800);
frame.setVisible(true);
}
公共空间解决方案(){
n=0;
buildGUI();
ActionListener actListner=新建ActionListener(){
@凌驾
已执行的公共无效操作(操作事件){
而(n!=3){
buildGUI();
n++;
}
}
};
定时器=新定时器(500,actListner);
timer.start();
}
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
(新计数器()).solve();
}
});
}
}
请阅读添加的评论:
public class Counter {
private JFrame frame;
private JPanel panel;
private JButton[][] buttons;
private int[][] isCooperating;
private Random rand;
private ActionListener actListner;
private Timer timer;
private int n = 0;
Counter(){
//do the initialization in the constructor
buttons = new JButton[50][50];
isCooperating = new int[50][50];
rand = new Random();
frame = new JFrame("GridLayout demo");
frame.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
panel = new JPanel();
panel.setLayout(new GridLayout(50,50));
//no need to re construct action listener with each buildGUI run
actListner = new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
buildGUI();
System.out.println("building gui " +n);
n++;
if(n >=3) {timer.stop();}
}
};
//no need to re construct timer with each buildGUI run
timer = new Timer(1500, actListner);
timer.setRepeats(true);
buildGUI();
}
void buildGUI() {
//remove all components from panel, if any
//otherwise you keep adding components
panel.removeAll();
for (int x = 0; x < 50; x++) {
for (int y = 0; y < 50; y++) {
int randomNumber = rand.nextInt(2); // randomly chooses between 1 and 0; for cooperating status
isCooperating[x][y] = randomNumber; // 1 is cooperating
}
}
for (int x = 0; x < 50; x++) {
for (int y = 0; y < 50; y++) {
JButton btn = new JButton();
if (isCooperating[x][y] == 0) {
btn.setBackground(Color.RED);
btn.setOpaque(true);
btn.setBorder(null);
} else if (isCooperating[x][y] == 1) {
btn.setBackground(Color.BLUE);
btn.setOpaque(true);
btn.setBorder(null);
}
btn.setPreferredSize(new Dimension(10, 10));
panel.add(btn);
buttons[x][y] = btn;
}
}
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//let layout set size by using pack
//frame.setSize(800,800);
frame.pack();
frame.setVisible(true);
}
public void solve() {
timer.start();
}
public static void main(String[]args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Counter().solve();
}
});
}
}
公共类计数器{
私有JFrame;
私人JPanel小组;
专用JButton[]]按钮;
私有int[][]正在合作;
私有随机兰德;
私有ActionListener actListner;
私人定时器;
私有整数n=0;
计数器(){
//在构造函数中进行初始化
按钮=新的JButton[50][50];
isCooperating=新整数[50][50];
rand=新随机数();
frame=新JFrame(“GridLayout演示”);
frame.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE\u ON\u CLOSE);
panel=新的JPanel();
面板设置布局(新网格布局(50,50));
//无需在每次运行buildGUI时重新构造操作侦听器
actListner=newActionListener(){
@凌驾
已执行的公共无效操作(操作事件){
buildGUI();
System.out.println(“构建gui”+n);
n++;
如果(n>=3){timer.stop();}
}
};
//无需在每次运行buildGUI时重新构造计时器
计时器=新计时器(1500,actListner);
timer.setRepeats(真);
buildGUI();
}
void buildGUI(){
//从面板上拆下所有组件(如有)
//否则,您将继续添加组件
panel.removeAll();
对于(int x=0;x<50;x++){
对于(int y=0;y<50;y++){
int randomNumber=rand.nextInt(2);//在1和0之间随机选择;用于协作状态
isCooperating[x][y]=randomNumber;//1正在合作
}
}
对于(int x=0;x<50;x++){
对于(int y=0;y<50;y++){
JButton btn=新JButton();
如果(i合作[x][y]==0){
背景(颜色为红色);
btn.setOpaque(真);
btn.订单(空);
}else if(i合作[x][y]==1){
背景(蓝色);
btn.setOpaque(真);
btn.订单(空);
}
btn.setPreferredSize(新尺寸(10,10));
面板。添加(btn);
按钮[x][y]=btn;
}
}
框架。添加(面板);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//让布局使用pack设置大小
//框架设置尺寸(800800);
frame.pack();
frame.setVisible(true);
}
公共空间解决方案(){
timer.start();
}
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
新计数器().solve();
}
});
}
}
您的侦听器将保持紧密循环,直到n
为3。你认为什么时候n
会是3?(我看不到任何东西可以将其从0更改为…)您的侦听器运行到一个无休止的循环中。由于它在Swing事件线程上运行(这就是使用Swing计时器的全部意义),Swing永远没有机会更新GUI。是的,我刚刚添加了它,但它仍然不能正常工作。它只是添加了更多带有随机颜色的正方形,而我想要的是正方形