java - 需要有关 java GUI 的帮助

标签 java swing

我正在 Java 上创建一个三张牌扑克游戏。 我现在基本上已经完成了编码,但是我在 GUI 方面遇到了一些问题。 这是我的图形部分的代码:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.Scanner;

import javax.*;

public class Board extends dealer{
    final static boolean shouldFill = true;
    final static boolean shouldWeightX = true;
    final static boolean RIGHT_TO_LEFT = false;
    public static Image cardback;
    public static Image comp1;
    public static Image comp2;
    public static Image comp3;
    public static Image play1;
    public static Image play2;
    public static Image play3;
    public static int x,y;
    public static int numgames;
    public static int bet;
    public static int wlt=7;
    public static int dealorfold=1;
    public static int showcard=1;
    static int playermoney;
    static int compmoney; 
    protected static hand player;
    protected static hand computer;
    static class deal implements ActionListener{
        public void actionPerformed(ActionEvent e){
            dealorfold=2;
        }
    }

    static class fold implements ActionListener{
        public void actionPerformed(ActionEvent e){
            dealorfold=3;
        }
    }

    static class allin implements ActionListener{
        public void actionPerformed(ActionEvent e){
            bet=playermoney;
        }
    }

    static class ten implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if(playermoney<10)
                wlt=9;          
            else
                bet=10;
        }
    }

    static class fifty implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if(playermoney<50)
                wlt=9;          
            else
                bet=50;
        }
    }

    static class hundred implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if(playermoney<100)
                wlt=9;          
            else
                bet=100;
        }
    }

    static class fivehund implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if(playermoney<500)
                wlt=9;          
            else
                bet=500;
        }
    }

    static class thaud implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if(playermoney<1000)
                wlt=9;          
            else
                bet=1000;
        }
    }

    static class fivethaud implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if(playermoney<5000)
                wlt=9;          
            else
                bet=5000;
        }
    }

    static class tenthaud implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if(playermoney<10000)
                wlt=9;          
            else
                bet=10000;
        }
    }   

    private static void createAndShowGUI() {
        setcards();
        JFrame frame = new JFrame("Three Card Poker");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(1366,768));
        addComponentsToPane(frame.getContentPane());
        frame.pack();
        frame.setVisible(true);

        int p1 = checkpattern(player);
        int c1 = checkpattern(computer);
        while(dealorfold==1){
            if(dealorfold==2){
                if(p1>c1){
                    compmoney = compmoney - bet;
                    playermoney = playermoney + bet;
                    showcard=2;
                    wlt=1;
                }
                else if (p1<c1){
                    compmoney = compmoney + bet;
                    playermoney = playermoney - bet;
                    showcard=2;
                    wlt=2;
                }
                else if(p1==0 && c1==0){
                    if(checkhighercardinhand(player, computer)==0){
                        compmoney = compmoney - bet;
                        playermoney = playermoney + bet;
                        showcard=2;
                        wlt=1;

                    }
                    else if(checkhighercardinhand(player, computer)==1){
                        compmoney = compmoney + bet;
                        playermoney = playermoney - bet;
                        showcard=2;
                        wlt=2;
                    }
                    else if(checkhighercardinhand(player, computer)==2){
                        showcard=2;
                        wlt=3;
                    }
                }
                else if(p1==1 && c1==1){
                    if(checkhigherpair(player, computer)==0){
                        compmoney = compmoney - bet;
                        playermoney = playermoney + bet;
                        showcard=2;
                        wlt=1;
                    }
                    else if(checkhigherpair(player, computer)==1){
                        compmoney = compmoney + bet;
                        playermoney = playermoney - bet;
                        showcard=2;
                        wlt=2;
                    }
                    else if(checkhigherpair(player, computer)==2){
                        showcard=2;
                        wlt=3;
                    }
                }
                else if(p1 == c1 && p1 >= 3){
                    int check = checkhigherthanstraight(player, computer);

                    if(check == 0){
                        compmoney = compmoney - bet;
                        playermoney = playermoney + bet;
                        showcard=2;
                        wlt=1;
                    }
                    else if (check == 1){
                        compmoney = compmoney + bet;
                        playermoney = playermoney - bet;
                        showcard=2;
                        wlt=2;
                    }
                    else{
                        showcard=2;
                        wlt=3;
                    }
                }
                else if(p1 == c1 && p1 == 2){
                    showcard=2;
                    wlt=3;
                }
            }

            else{
                playermoney=playermoney-50;
                showcard=2;
                wlt=4;
            }                   
        }
    }

    public static void addComponentsToPane(Container pane) {
        if (RIGHT_TO_LEFT) {
            pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        }

        pane.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        JLabel comp = new JLabel("Computer:");
        if (shouldWeightX) {
            c.weightx = 0.5;
        }
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        c.insets=new Insets(0,0,100,0);
        pane.add(comp, c);

        JLabel c1 = new JLabel(computer.get(0)+"");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 1;
        c.gridy = 0;
        pane.add(c1, c);

        JLabel c2 = new JLabel(computer.get(1)+"");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 2;
        c.gridy = 0;
        pane.add(c2, c);

        JLabel  c3= new JLabel(computer.get(2)+"");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 3;
        c.gridy = 0;
        pane.add(c3, c);

        JLabel compcash=new JLabel("Dealer has: "+compmoney);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 6;
        c.gridy = 1;
        pane.add(compcash, c);

        JLabel player1 = new JLabel("Player:");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx=.5;
        c.gridx = 0;
        c.gridy = 2;
        pane.add(player1, c);

        JLabel p1 = new JLabel(player.get(0)+"");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 1;
        c.gridy = 2;
        pane.add(p1, c);

        JLabel p2 = new JLabel(player.get(1)+"");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 2;
        c.gridy = 2;
        pane.add(p2, c);

        JLabel p3 = new JLabel(player.get(2)+"");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 3;
        c.gridy = 2;
        pane.add(p3, c);

        JLabel playcash=new JLabel("You have: "+playermoney);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 6;
        c.gridy = 2;
        pane.add(playcash, c);

        JLabel pattern=new JLabel("Your hand is: ");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 6;
        c.gridy = 3;
        pane.add(pattern, c);

        JButton deal = new JButton("Deal");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 0;
        c.gridy = 5;
        pane.add(deal, c);
        deal.addActionListener(new deal());

        JButton fold = new JButton("Fold(Penalty: minus $50)");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 1;
        c.gridy = 5;
        pane.add(fold, c);
        fold.addActionListener(new fold());

        JLabel bet = new JLabel("             Bet:");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 2;
        c.gridy = 5;
        pane.add(bet, c);

        JButton allin = new JButton("All IN");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 3;
        c.gridy = 5;
        pane.add(allin, c);
        allin.addActionListener(new allin());

        JButton ten= new JButton("10");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 4;
        c.gridy = 5;
        pane.add(ten, c);
        ten.addActionListener(new ten());

        JButton fifty = new JButton("50");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 5;
        c.gridy = 5;
        pane.add(fifty, c);
        fifty.addActionListener(new fifty());

        JButton hundred = new JButton("100");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 6;
        c.gridy = 5;
        pane.add(hundred, c);
        hundred.addActionListener(new hundred());

        JButton fivehund = new JButton("500");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 7;
        c.gridy = 5;
        pane.add(fivehund, c);
        fivehund.addActionListener(new fivehund());

        JButton thaud = new JButton("1000");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 8;
        c.gridy = 5;
        pane.add(thaud, c);
        thaud.addActionListener(new thaud());

        JButton fivethaud = new JButton("5000");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 9;
        c.gridy = 5;
        pane.add(fivethaud, c);
        fivethaud.addActionListener(new fivethaud());

        JButton tenthaud = new JButton("10000");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 10;
        c.gridy = 5;
        pane.add(tenthaud, c);
        tenthaud.addActionListener(new tenthaud());

        String mess;
        if(wlt==1)
            mess="You Win The Round!";
        else if(wlt==2)
            mess="Dealer Wins The Round!";
        else if(wlt==3)
            mess="You And Dealer Tie The Round";
        else if(wlt==4)
            mess="You Folded. You Recieve Penalty of Minus $50";
        else if(wlt==5)
            mess="Dealer Loses All Of Its Money, YOU WIN THE WHOLE GAME!";
        else if(wlt==6)
            mess="You Lose All Of You Money, You Lose The Whole Game!";
        else if(wlt==7)
            mess="Please Click Deal Or Fold";
        else if(wlt==8)
            mess="Please Place Your Bet";
        else
            mess="Please Select Another Bet.";

        JLabel message = new JLabel(mess);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0;
        c.gridx = 6;
        c.gridy = 4;
        pane.add(message, c);

        JLabel label = new JLabel("");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 40;
        c.weightx = 0.0;
        c.gridwidth = 3;
        c.gridx = 0;
        c.gridy = 2;
        pane.add(label, c);

        label = new JLabel("");
        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;       
        c.weighty = 1.0;   
        c.anchor = GridBagConstraints.PAGE_END; 
        c.insets = new Insets(10,0,0,0);  
        c.gridx = 1;       
        c.gridwidth = 2;   
        c.gridy = 2;      
        pane.add(label, c);
    }

    public static void setmoney(){
        compmoney=10000000;
        playermoney=10000000;   
    }

    public static void setcards(){
        player = new hand();
        computer= new hand();
        player.addCard(new cards());
        player.addCard(new cards());
        player.addCard(new cards());
        computer.addCard(new cards());
        computer.addCard(new cards());
        computer.addCard(new cards());
        cardswoprob.correctduplicates(player, computer);
    }

    public static void play(){
        for(int index=0;compmoney>=10&&playermoney>=10;index++){
            wlt=7;
            int p1 = checkpattern(player);
            int c1 = checkpattern(computer);
            while(dealorfold==1){
                if(dealorfold==2){
                    if(p1>c1){
                        compmoney = compmoney - bet;
                        playermoney = playermoney + bet;
                        showcard=2;
                        wlt=1;
                    }
                    else if (p1<c1){
                        compmoney = compmoney + bet;
                        playermoney = playermoney - bet;
                        showcard=2;
                        wlt=2;
                    }
                    else if(p1==0 && c1==0){
                        if(checkhighercardinhand(player, computer)==0){
                            compmoney = compmoney - bet;
                            playermoney = playermoney + bet;
                            showcard=2;
                            wlt=1;

                        }
                        else if(checkhighercardinhand(player, computer)==1){
                            compmoney = compmoney + bet;
                            playermoney = playermoney - bet;
                            showcard=2;
                            wlt=2;
                        }
                        else if(checkhighercardinhand(player, computer)==2){
                            showcard=2;
                            wlt=3;
                        }
                    }
                    else if(p1==1 && c1==1){
                        if(checkhigherpair(player, computer)==0){
                            compmoney = compmoney - bet;
                            playermoney = playermoney + bet;
                            showcard=2;
                            wlt=1;
                        }
                        else if(checkhigherpair(player, computer)==1){
                            compmoney = compmoney + bet;
                            playermoney = playermoney - bet;
                            showcard=2;
                            wlt=2;
                        }
                        else if(checkhigherpair(player, computer)==2){
                            showcard=2;
                            wlt=3;
                        }
                    }
                    else if(p1 == c1 && p1 >= 3){
                        int check = checkhigherthanstraight(player, computer);

                        if(check == 0){
                            compmoney = compmoney - bet;
                            playermoney = playermoney + bet;
                            showcard=2;
                            wlt=1;
                        }
                        else if (check == 1){
                            compmoney = compmoney + bet;
                            playermoney = playermoney - bet;
                            showcard=2;
                            wlt=2;
                        }
                        else{
                            showcard=2;
                            wlt=3;
                        }
                    }
                    else if(p1 == c1 && p1 == 2){
                        showcard=2;
                        wlt=3;
                    }
                }

                else{
                    playermoney=playermoney-50;
                    showcard=2;
                    wlt=4;
                }                   
            }
        }
        if(playermoney > compmoney)
            wlt=5;
        else
            wlt=6;
    }

    public static void main(String[] args) {
        setmoney();
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                while(compmoney>=10&&playermoney>=10)
                createAndShowGUI();
                if(playermoney > compmoney)
                    wlt=5;
                else
                    wlt=6;
            }
        });
    }
}

主要问题是,在 createAndShowGUI() 中,如果我将我尝试过的 while 循环(for 循环、do 循环)放在那里,JPanel 将只显示出来,但其中什么也没有。如果那里没有 while 循环,JPanel 将定期显示其中的所有内容。 但我需要 while 循环内的代码,因为代码基本上就是游戏。

我尝试创建一个自己的方法(play()),但如果我在 main() 或任何地方调用它,JPanel 将为白色(无任何内容)。

这是我的第二个问题:如果删除 while 循环并且 JPanel 正常显示,您将看到计算机的手和玩家的手,但以文本形式。我想知道如何用图像替换文本。

最佳答案

有一些东西,并不是所有的东西都直接与 GUI 相关:

丢失静态成员,它们不应该在那里,我假设您使用了它们,因为所有内容都是从主方法调用的,但这不是正确的方法。 Java是一种面向对象的编程语言,创建类、实例化它们并使用它们,不要在一个类中执行所有工作,该类甚至没有实例化。

重构重复代码,不要创建具有一个值差异的不同类,而是创建更通用的类。例如,而不是像这样 5 个不同的类:

static class ten implements ActionListener{
    public void actionPerformed(ActionEvent e){
        if(playermoney<10)
            wlt=9;          
        else
            bet=10;
    }
}

声明一个类,如下所示:

static class ten implements ActionListener{
    int money;
    public ten(int money) {
        this.money = money
    }
    public void actionPerformed(ActionEvent e){
        if(playermoney<money)
            wlt=9;          
        else
            bet=money;
    }
}

并根据情况使用适当的int实例化它。

让应用程序由事件驱动。由于玩家必须进行交互,因此不要使用大的 while 循环,而是从监听器(即用户按下按钮)触发事件(例如现在发生了什么情况)

与上一点相关,不要在 UI 线程上执行长操作(就像您的整个程序一样),它不会让 UI 更新,在后台线程上执行所有工作,当您需要更改 UI 时,请使用 SwingUtilities.invokeLater(Runnable) 。我想你可能会找到this tutorial有帮助。

关于java - 需要有关 java GUI 的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6053458/

相关文章:

java - 在静态查询中设置变量

java - 关闭 JFileChooser 和 JDialog 后 JFrame 被禁用

java - 使用 Java 填写表格,可能控制浏览器?

java - 有没有办法改进这个切入点?

java - 使用 SwingWorker 在 JTextPane 中突出显示语法

java - 在 C++ 中获取 java.home 的位置

java - 如何检查 JTabbedPane 中的选项卡是否处于 Activity 状态?

JAVA - 无法在 JPanel 上绘制组件(也是 Runnable 和 KeyListener)

java - 如何更改 JComboBox 中的箭头样式

java - 用 Java 设计源代码编辑器,设计问题 :