摆动计时器未在 Java GUI 中启动 *已更新



我正在尝试制作一个制作宾果游戏的GUI应用程序。我正在使用摆动计时器,以便每 4 秒创建一个新呼叫(例如 O63)并更新呼叫板。但是,计时器似乎永远不会启动。如果我将操作侦听器放在按钮上而不是计时器上,它会起作用。我无法弄清楚,所以任何帮助将不胜感激。

import java.awt.*;
import java.awt.event.*;
import javax.swing.BorderFactory; 
import javax.swing.border.*;
import javax.swing.*;
import java.util.Random;
public class Bin1 {
private static JPanel panel = new JPanel();
private static JPanel panelcb = new JPanel();
private static JPanel panelcb1 = new JPanel();
private static JPanel panelcs = new JPanel();
private static JPanel panelcpu = new JPanel();
private static JPanel panelp = new JPanel();
private static Border border = BorderFactory.createLineBorder(Color.black, 1);
private static JLabel[] brow;
private static JLabel[] irow;
private static JLabel[] nrow;
private static JLabel[] grow;
private static JLabel[] orow;
private static Timer timer;
private static JLabel call;

private static JLabel[] boardbin(String letter, int col){
JLabel[] row = new JLabel[16];
JLabel let = new JLabel(" " + letter + " ");
row[0] = let;
row[0].setBorder(border);
row[0].setForeground(Color.MAGENTA);
panelcb.add(row[0]);
for(int i = 1; i < 16; i++){
JLabel x = new JLabel( "  " + ("" + (i + (15 * col))) + " " );
row[i] = x;
row[i].setBorder(border);
row[i].setForeground(Color.blue);
panelcb.add(row[i]);
}
return row;
}
/*private static void pcallb(int x){
if(x == 0){
for(int b = 0; b < 15; b++){
panelcb.add(brow[b]);
}
for(int i = 0; i < 15; i++){
panelcb.add(irow[i]);
}
for(int n = 0; n < 15; n++){
panelcb.add(nrow[n]);
}
for(int g = 0; g < 15; g++){
panelcb.add(grow[g]);
}
for(int o = 0; o < 15; o++){
panelcb.add(orow[o]);
}
}else{
for(int b = 0; b < 15; b++){
panelcb1.add(brow[b]);
}
for(int i = 0; i < 15; i++){
panelcb1.add(irow[i]);
}
for(int n = 0; n < 15; n++){
panelcb1.add(nrow[n]);
}
for(int g = 0; g < 15; g++){
panelcb1.add(grow[g]);
}
for(int o = 0; o < 15; o++){
panelcb1.add(orow[o]);
}
}
}*/
private static void callb(int x){
if(x == 0){
brow = new JLabel[15];
brow = boardbin("B", 0);
irow = new JLabel[15];
irow = boardbin("I", 1);
nrow = new JLabel[15];
nrow = boardbin("N", 2);
grow = new JLabel[15];
grow = boardbin("G", 3);
orow = new JLabel[15];
orow = boardbin("O", 4);
//pcallb(0);
}else{
//pcallb(1);
}
}
private static void biname(String letter, int jbl){
if(jbl == 0){
JLabel b = new JLabel("    " + letter);
b.setBorder(border);
b.setForeground(Color.magenta);
panelcpu.add(b);
}else{
JLabel b = new JLabel("    " + letter);
b.setBorder(border);
b.setForeground(Color.magenta);
panelp.add(b);
}
}
private static void cardcpu(){
Random rand = new Random();
biname("B", 0);
biname("I", 0);
biname("N", 0);
biname("G", 0);
biname("O", 0);
JLabel a = new JLabel(" ");
panelcpu.add(a);
biname("B", 0);
biname("I", 0);
biname("N", 0);
biname("G", 0);
biname("O", 0);
for(int h = 0; h < 10; h++){
if(h % 2 == 0){
for (int i = 0; i < 5; i++){
if(h == 4 && i == 2 || h == 5 && i == 2){
JLabel c = new JLabel("*");
c.setBorder(border);
c.setForeground(Color.blue);
panelcpu.add(c);
}else{
String num = Integer.toString(rand.nextInt(15) + 1 + i * 15);
JLabel c = new JLabel(num);
c.setBorder(border);
c.setForeground(Color.blue);
panelcpu.add(c);    
}
}
JLabel b = new JLabel(" ");
panelcpu.add(b);
}else{
for (int i = 0; i < 5; i++){
if(h == 4 && i == 2 || h == 5 && i == 2){
JLabel c = new JLabel("*");
c.setBorder(border);
c.setForeground(Color.blue);
panelcpu.add(c);
}else{
String num = Integer.toString(rand.nextInt(15) + 1 + i * 15);
JLabel c = new JLabel(num);
c.setBorder(border);
c.setForeground(Color.blue);
panelcpu.add(c);    
}
}
}
}
}
private static void cardp(){
Random rand = new Random();
biname("B", 1);
biname("I", 1);
biname("N", 1);
biname("G", 1);
biname("O", 1);
JLabel a = new JLabel(" ");
panelp.add(a);
biname("B", 1);
biname("I", 1);
biname("N", 1);
biname("G", 1);
biname("O", 1);
for(int h = 0; h < 10; h++){
if(h % 2 == 0){
for (int i = 0; i < 5; i++){
if(h == 4 && i == 2 || h == 5 && i == 2){
JButton c = new JButton("*");
c.setBorder(border);
c.setForeground(Color.blue);
panelp.add(c);
}else{
String num = Integer.toString(rand.nextInt(15) + 1 + i * 15);
JButton c = new JButton(num);
//  c.addActionListener(new ButtonAction());
c.setBorder(border);
c.setForeground(Color.blue);
panelp.add(c);  
}
}
JLabel b = new JLabel(" ");
panelp.add(b);
}else{
for (int i = 0; i < 5; i++){
if(h == 4 && i == 2 || h == 5 && i == 2){
JButton c = new JButton("*");
c.setBorder(border);
c.setForeground(Color.blue);
panelp.add(c);
}else{
String num = Integer.toString(rand.nextInt(15) + 1 + i * 15);
JButton c = new JButton(num);
//c.addActionListener(new ButtonAction());
c.setBorder(border);
c.setForeground(Color.blue);
panelp.add(c);  
}
}
}
}
}
public static void main (String[] args) {
timer = new Timer(4000, new ButtonAction());
timer.start();
Border border2 = BorderFactory.createLineBorder(Color.black, 2);
JFrame frame = new JFrame ("Bingo");
frame.setVisible(true);
//frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 
frame.setResizable(true);
frame.setSize(750, 750);
frame.setContentPane(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
panel.setSize(new Dimension(400, 600));
panel.setLayout(new GridLayout(5, 17));
panel.add(panelcb);
panel.add(panelcs);
TitledBorder title;
title = BorderFactory.createTitledBorder(border2, "CALL BOARD");
title.setTitleColor(Color.GREEN);
title.setTitleFont(new Font("Dialog", Font.BOLD, 15));
panelcb.setBorder(title);
panelcb.setSize(new Dimension(400, 600));
panelcb.setLayout(new GridLayout(5, 17));
callb(0);  
panelcb1.setBorder(title);
panelcb1.setSize(new Dimension(400, 600));
panelcb1.setLayout(new GridLayout(5, 17));
callb(1); 
panelcs.setSize(new Dimension(400, 600));
panelcs.setLayout(new GridLayout(1, 2));
panelcs.add(panelcpu);
panelcs.add(panelp);
TitledBorder titlecpu;
titlecpu = BorderFactory.createTitledBorder(border2, "CPU BOARD");
titlecpu.setTitleColor(Color.blue);
titlecpu.setTitleFont(new Font("Dialog", Font.BOLD, 15));
panelcpu.setBorder(titlecpu);
panelcpu.setSize(new Dimension(600, 600));
panelcpu.setLayout(new GridLayout(6, 11));
cardcpu();
TitledBorder titlep;
titlep = BorderFactory.createTitledBorder(border2, "PLAYER BOARD");
titlep.setTitleColor(Color.red);
titlep.setTitleFont(new Font("Dialog", Font.BOLD, 15));
panelp.setBorder(titlep);
panelp.setSize(new Dimension(600, 600));
panelp.setLayout(new /*FlowLayout()*/ GridLayout(6, 14));
cardp();
JLabel bottom = new JLabel("Get Ready To Play Bingo!");
bottom.setForeground(Color.magenta);
bottom.setFont(new Font("Dialog", Font.BOLD, 20));
panel.add(bottom);
}
public static class ButtonAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
Random rand = new Random();
String n = Integer.toString(rand.nextInt(75));
JLabel ca = new JLabel(n);
call = ca;
JLabel star = new JLabel("*");
int x = Integer.parseInt(n);
//panel.add(call);
if(x >= 1 && x <= 15){
for(int b = 0; b < 15; b++){
if(call == brow[b]){
brow[b] = star;
JLabel upcallb = new JLabel("B" + n);
call = upcallb;
panel.add(call);
}
}
}else if(x >= 16 && x <= 30){
for(int i = 0; i < 15; i++){
if(call == irow[i]){
irow[i] = star;
JLabel upcalli = new JLabel("I" + n);
call = upcalli;
panel.add(call);
}
}
}else if(x >= 31 && x <= 45){
for(int m = 0; m < 15; m++){
if(call == nrow[m]){
nrow[m] = star;
JLabel upcalln = new JLabel("N" + n);
call = upcalln;
panel.add(call);
}
}
}else if(x >= 46 && x <= 60){
for(int g = 0; g < 15; g++){ 
if(call == grow[g]){
grow[g] = star;
JLabel upcallg = new JLabel("G" + n);
call = upcallg;
panel.add(call);
}
}
}else{
for(int o = 0; o < 15; o++){
if(call == orow[o]){
orow[o] = star;
JLabel upcallo = new JLabel("O" + n);
call = upcallo;
panel.add(call);
}
}
}
timer.restart();
}
}

}

看起来您的计时器实际上正在运行,但以下表达式的计算结果始终为 false:

  • if (call == brow[b])
  • if (call == irow[i])
  • if (call == nrow[m])
  • if (call == grow[g])
  • if (call == orow[o])

这是因为call为空。 在任何时候都不会call设置为任何这些数组的元素。

请注意,JLabel call = new JLabel();创建一个名为call的新局部变量,该变量与private static JLabel call;声明无关。 但即使是这样,将call设置为新的 JLabel 也不会导致上述 if 测试成功。

最新更新