如何更新GUI中的组件,然后让它休眠。

时间:2018-02-22 03:26:20

标签: java swing timer jframe actionlistener

我觉得我到处都搜索过。我相信我知道我的代码不起作用但不完全理解它或如何解决我的问题的原因。最初我尝试使用TimeUnit导入和Thread.sleep(),并且在等待给定秒数之后,这两个都更新了JFrame。我想让JFrame显示游戏结果,等待几秒钟,然后再次隐藏结果。

根据我的阅读,使用import javax.swing.Timer是我需要前进的方向,但对于我的生活,我无法弄清楚如何在我的代码中实现它。任何帮助是极大的赞赏。

//////////////以下是我认为需要编辑的代码部分//////////////// /

btnStay.addActionListener(l -> {
        //System.out.println("btnStay was pressed");
        btnStay.setEnabled(false);
        btnHit.setEnabled(false);
        btnConfirm.setVisible(false);
        lblAccount.setVisible(false);
        lblBet.setVisible(false);
        txtAccount.setVisible(false);
        txtBet.setVisible(false);

        d.hitUntil();
        String result = g.winner();
        System.out.println(result);

        if(result.equals("wbj")){
            account = account + (bet*1.5);
            lblResult.setText("You have blackjack...");
            lblResult.setVisible(true);

        }else if(result.equals("lbj")){
            account = account - bet;
            lblResult.setText("The dealer has blackjack...");
            lblResult.setVisible(true);

        }else if(result.contains("w")){
            account = account + bet;
            lblResult.setBounds(940, 185, 300, 100);
            lblResult.setFont(new Font("Courier New", Font.BOLD, 40));
            lblResult.setText("You won...");
            lblResult.setVisible(true);


        }else if(result.contains("l")){
            account = account - bet;
            lblResult.setBounds(940, 185, 300, 100);
            lblResult.setFont(new Font("Courier New", Font.BOLD, 40));
            lblResult.setText("You lost...");
            lblResult.setVisible(true);

        }else{
            lblResult.setText("Draw");
            lblResult.setVisible(true);

        }
        txtAccount.setText(f.format(account));
        //lblResult.setVisible(false);
        });

//////////////如果有任何帮助,全班同学/////////////////

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JFrame;
import java.util.Timer;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.text.DecimalFormat;

public class GUI extends JFrame{

Game g;
Player p; 
Dealer d;

int aW = 1280;
int aH = 800;

Font buttonFont = new Font("TimesRoman", Font.PLAIN, 30);

Color background = new Color(39,119,20);
Color buttonColor = new Color(204,204,0);

JLabel lblHeader = new JLabel();
JLabel lblCreater = new JLabel();
JLabel lblBet = new JLabel();
JLabel lblAccount = new JLabel();
JLabel lblResult = new JLabel();

JTextField txtBet = new JTextField();
JTextField txtAccount = new JTextField();

Button btnHit = new Button();
Button btnStay = new Button();
Button btnExit = new Button();
Button btnConfirm = new Button();

int gridX = 50;
int gridY = 50;
int gridW = 800;
int gridH = 700;

int cardSpacing = 10;
int cardTW = gridW/5;
int cardTH = gridH/2 - 50;
int cardAW = cardTW - 2*cardSpacing;
int cardAH = cardTH-2*cardSpacing;

double account;
double bet;

DecimalFormat f = new DecimalFormat("0.00");
int tPassed = 0;

public GUI(Player p, Dealer d){

    this.p = p;
    this.d = d;
    g = new Game(p, d);

    this.setSize(aW+6, aH+29);
    this.setTitle("Blackjack Java Edition");
    this.setVisible(true);
    this.setResizable(false);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Board board = new Board();
    this.setContentPane(board);
    this.setLayout(null);

    lblHeader.setBounds(925, 40, 450, 80);
    lblHeader.setText("Blackjack Java Edition");
    lblHeader.setFont(new Font("TimesRoman", Font.BOLD, 30));
    board.add(lblHeader);

    lblCreater.setBounds(990, 80, 450, 80);
    lblCreater.setText("By Jonny Bloom");
    lblCreater.setFont(new Font("TimesRoman", Font.PLAIN, 25));
    board.add(lblCreater);

    lblBet.setBounds(925, 210, 100, 30);
    lblBet.setText("Bet");
    lblBet.setFont(new Font("Courier New", Font.BOLD, 25));
    lblBet.setVisible(false);
    board.add(lblBet);

    lblAccount.setBounds(925, 165, 120, 30);
    lblAccount.setText("Account");
    lblAccount.setFont(new Font("Courier New", Font.BOLD, 25));
    board.add(lblAccount);

    txtBet.setBounds(980, 210, 240, 30);
    txtBet.setVisible(false);
    board.add(txtBet);

    txtAccount.setBounds(1040, 165, 180, 30);
    board.add(txtAccount);

    lblResult.setText(null);
    board.add(lblResult);

    btnHit.setBounds(925, 500, 120, 80);
    btnHit.setBackground(buttonColor);
    btnHit.setFont(buttonFont);
    btnHit.setLabel("Hit");
    btnHit.setEnabled(false);
    board.add(btnHit);

    btnHit.addActionListener(l -> {
        //System.out.println("btnHit was pressed");
        p.hit();
        System.out.println(p.h.hand);

        if(p.h.bust() == true || p.h.getValue() == 21 || p.h.hand.size() >= 5){
            d.hitUntil();
            btnHit.setEnabled(false);
        }
    });

    btnStay.setBounds(1100, 500, 120, 80);
    btnStay.setBackground(buttonColor);
    btnStay.setFont(buttonFont);
    btnStay.setLabel("Stay");
    btnStay.setEnabled(false);
    board.add(btnStay);

    btnStay.addActionListener(l -> {
        //System.out.println("btnStay was pressed");
        btnStay.setEnabled(false);
        btnHit.setEnabled(false);
        btnConfirm.setVisible(false);
        lblAccount.setVisible(false);
        lblBet.setVisible(false);
        txtAccount.setVisible(false);
        txtBet.setVisible(false);

        d.hitUntil();
        String result = g.winner();
        System.out.println(result);

        if(result.equals("wbj")){
            account = account + (bet*1.5);
            lblResult.setText("You have blackjack...");
            lblResult.setVisible(true);

        }else if(result.equals("lbj")){
            account = account - bet;
            lblResult.setText("The dealer has blackjack...");
            lblResult.setVisible(true);

        }else if(result.contains("w")){
            account = account + bet;
            lblResult.setBounds(940, 185, 300, 100);
            lblResult.setFont(new Font("Courier New", Font.BOLD, 40));
            lblResult.setText("You won...");
            lblResult.setVisible(true);


        }else if(result.contains("l")){
            account = account - bet;
            lblResult.setBounds(940, 185, 300, 100);
            lblResult.setFont(new Font("Courier New", Font.BOLD, 40));
            lblResult.setText("You lost...");
            lblResult.setVisible(true);

        }else{
            lblResult.setText("Draw");
            lblResult.setVisible(true);

        }
        txtAccount.setText(f.format(account));
        //lblResult.setVisible(false);
        });

    btnExit.setBounds(960, 630, 225, 80);
    btnExit.setBackground(buttonColor);
    btnExit.setFont(buttonFont);
    btnExit.setLabel("Exit");
    board.add(btnExit);

    btnExit.addActionListener(l -> {
        //System.out.println("btnExit was pressed");
        this.dispose();

    });

    btnConfirm.setBounds(975, 270, 200, 50);
    btnConfirm.setBackground(buttonColor);
    btnConfirm.setFont(buttonFont);
    btnConfirm.setLabel("Confirm");
    board.add(btnConfirm);

    btnConfirm.addActionListener(l -> {
        //System.out.println("btnConfirm was pressed");
        try{
            txtAccount.setText(f.format(Double.parseDouble(txtAccount.getText())));
            account = Double.parseDouble(txtAccount.getText());
            txtAccount.setEnabled(false);
            lblBet.setVisible(true);
            txtBet.setVisible(true);
        }catch(Exception e1){
            System.out.println(e1);
            txtAccount.setText(null);
        }

        //Second Half of Confirm Button

        try{
            txtBet.setText(f.format(Double.parseDouble(txtBet.getText())));
            bet = Double.parseDouble(txtBet.getText());
            if(bet > account || bet < 0){
                throw new Exception("Bet exceeds maximum or minimum amount");
            }
            btnHit.setEnabled(true);
            btnStay.setEnabled(true);
            txtBet.setEnabled(false);
            btnConfirm.setEnabled(false);
            for(int i = 0; i < 2; i++){
                p.hit();
                d.h.dealCard();
            }
            System.out.println(p.h.hand); //DISPLAY CARD HERE
            System.out.println(d.h.hand); //DISPLAY CARD HERE
        }catch(Exception e2){
            System.out.println(e2);
            txtBet.setText(null);

        }

    });
}
public class Board extends JPanel {

    public void paintComponent(Graphics g){ 
        g.setColor(background);
        g.fillRect(0, 0, aW, aH);

        g.setColor(Color.BLACK);
        g.drawRect(gridX, gridY, gridW, gridH);
        g.drawRect(gridX + gridW + 50, gridY, gridW / 3 + 75, gridH / 2 - 50);
        g.drawRect(gridX + gridW + 50, 450, gridW / 3 + 75, gridH /2 - 50);
        g.drawRect(gridX + gridW + 50, gridY, gridW / 3 + 75, (gridH / 2 - 50) / 3 - 10);


        for(int i = 0; i < 5; i++) {
            g.drawRect(gridX+i*cardTW+cardSpacing, gridY+cardSpacing, cardAW, cardAH - 75);
            g.drawRect(gridX + i * cardTW + cardSpacing, gridY + cardSpacing + cardTH + 175, cardAW, cardAH - 75);

        }
    }
}
}

1 个答案:

答案 0 :(得分:0)

private void hideResult() {
    new Thread(new Runnable(){
        @Override
        public void run() {
            // wait 2 secons
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
            // update lblResult in the edt
            SwingUtilities.invokeLater(new Runnable(){
                @Override
                public void run() {
                    lblResult.setVisible(false);
                    board.repaint();
                }
            });
        }
    }).start();
}