簡體   English   中英

Swing Timer未在Java GUI中啟動*已更新

[英]Swing Timer is not starting in a Java GUI *UPDATED

我正在嘗試制作一個可以玩賓果游戲的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();
    }

}

}

看來您的Timer實際上正在運行,但是以下表達式始終求值為false:

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

這是因為call為null。 任何時候都不call設置為任何這些數組的元素。

注意, JLabel call = new JLabel(); 創建一個名為call新局部變量 ,該局部變量與您的private static JLabel call;無關private static JLabel call; 宣言。 但是即使是這樣,將call設置為新的JLabel也不會導致上述if-tests成功。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM