1

我正在制作二十一点游戏。一切都完成了,我想制作一个重置按钮来重新开始游戏。我在 ActionListener 中 removeAll(),但是当 Board() 再次启动时,它不会添加 JButton 绘制或折叠。请帮助 PS Cards() 构造函数是我所有的卡片图像文件。

import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
import javax.imageio.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;

public class Board extends JPanel implements ActionListener
{
    Cards deck=new Cards();

    static ArrayList<BufferedImage> pcards=new ArrayList<BufferedImage>();
    static ArrayList<BufferedImage> ccards=new ArrayList<BufferedImage>();
    static BufferedImage back;

    static int w=1280/2;
    static int h=760/2;

    static ArrayList<String> p=new ArrayList<String>();
    static ArrayList<String> com=new ArrayList<String>();

    static Boolean last=true;
    static String end="";

    public Board()
    {
        setLayout(null);

        Color green=new Color(00,55,00);
        setBackground(green);

        try 
        {     
            String c="Deck\\"+deck.draw();
            p.add(c);           
            pcards.add(ImageIO.read(this.getClass().getResource(c)));
            c="Deck\\"+deck.draw();
            p.add(c);
            pcards.add(ImageIO.read(this.getClass().getResource(c)));
            c="Deck\\"+deck.draw();
            com.add(c);
            ccards.add(ImageIO.read(this.getClass().getResource(c)));
            c="Deck\\"+deck.draw();
            com.add(c);
            ccards.add(ImageIO.read(this.getClass().getResource(c)));

            back=(ImageIO.read(this.getClass().getResource("Deck\\b1fv.GIF")));
        } 
        catch (IOException e) 
        { 
          //Not handled. 
        }
        int cp=Integer.parseInt(Score(com));
        while(cp<17)
        {
            try
            {
                String c="Deck\\"+deck.draw();
                com.add(c);
                ccards.add(ImageIO.read(this.getClass().getResource(c)));
            }

            catch (IOException e) 
            { 
             //Not handled. 
            }
            cp=Integer.parseInt(Score(com));
        }

        JButton draw=new JButton("Draw");
        draw.setBounds(w-120,h-30,90,40);
        draw.setFont(new Font("Arabic", Font.PLAIN, 20));
        draw.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent a)
            {
                int pp=Integer.parseInt(Score(p));      
            if(pp>21||end.length()>0)
            {
                last=false;
                repaint();
            }
            else
            {
                try
                {
                    String c="Deck\\"+deck.draw();
                    p.add(c);           
                    pcards.add(ImageIO.read(this.getClass().getResource(c)));
                    repaint();
            }
                catch (IOException e) 
                { 
                //Not handled. 
                }
            }}
        });
        add(draw);


        JButton fold=new JButton("Fold");
        fold.setBounds(w+40,h-30,90,40);
        fold.setFont(new Font("Arabic", Font.PLAIN, 20));
        fold.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent a)
            {
                int pp=Integer.parseInt(Score(p));  
                int cp=Integer.parseInt(Score(com));
                end=End(pp,cp);
                last=false;
                repaint();
            }
        });
        add(fold);
    }

    public void paint(Graphics g)
    {
        super.paint(g);

        g.setFont(new Font("Arabic", Font.PLAIN, 50));
        g.setColor(Color.black);

        int pp=Integer.parseInt(Score(p));  
        int cp=Integer.parseInt(Score(com));
        if(pp>21)
        {
            end=End(pp,cp);
            last=false;
        }
        int i=0;
        for(BufferedImage temp:pcards)
        {
            g.drawImage(temp, (w-120)+i, (h+(h/2))-80,100,170, null);
            i+=60;
        }
        i=0;
        if(last)
        {
            for(BufferedImage temp:ccards)
            {
                g.drawImage(back, (w-120)+i, (h-(h/2))-100,100,170, null);
                i+=60;
            }
            g.drawString("Score:",(w-(w/2))-90,h-40);
            g.drawString("???", (w-(w/2))-70, h+40);
        }
        else
        {
            for(BufferedImage temp:ccards)
            {
                g.drawImage(temp, (w-120)+i, (h-(h/2))-100,100,170, null);
                i+=60;
            }
            g.drawString("Score:",w-(w/2)-90,h-40);
            g.drawString(Score(com), (w-(w/2))-50, h+40);
        }   
        g.drawString("Score:",w+(w/2)-50,h-40);
        g.drawString(Score(p), (w+(w/2)), h+40);

        int j=end.length();
        g.setFont(new Font("Arabic", Font.PLAIN, 70));
        Color l=new Color(66,153,204);
        g.setColor(l);
        g.drawString(end,(w-(j*20))+20,h-(h/2));

        if(end.length()>0)
        {
            g.setFont(new Font("Arabic", Font.PLAIN, 30));
            g.setColor(Color.black);
            g.drawString("Play Again?",(w-(w/2))-160,h+(h/2));

            JButton reset=new JButton("Yes");
            reset.setBounds((w-(w/2))-120,(h+(h/2))+30,90,40);
            reset.setFont(new Font("Arabic", Font.PLAIN, 20));
            reset.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e)
                {
                    last=true;
                    end="";
                    pcards.clear();ccards.clear();p.clear();com.clear();
                    repaint();
                    removeAll();
                    new Board();

                }
            });
            add(reset);
        }
    }

    public void actionPerformed(ActionEvent e)
    {
    }

    public static String End(int x,int y)
    {
        if(x>21 && y<=21)
            end="You Busted! Computer Won!";
        else if(x<=21 && y>21)
            end="You Won! Computer Busted!";
        else if(x>21 && y>21)
            end="Both Busted! Draw!";
        else if(x>y)
            end="You Win!";
        else if(x<y)                
            end="You Lost!";
        else
            end="You and Computer Tied! Draw!";
        return end;
    }

    public static String Score(ArrayList<String> x)
    {
        int sum=0;
        for(int i=0; i<x.size(); i++)
        {
            String z=x.get(i);
            if(z.charAt(6)=='2')
                sum+=2;
            if(z.charAt(6)=='3')
                sum+=3;
            if(z.charAt(6)=='4')
                sum+=4;
            if(z.charAt(6)=='5')
                sum+=5;
            if(z.charAt(6)=='6')
                sum+=6;
            if(z.charAt(6)=='7')
                sum+=7;
            if(z.charAt(6)=='8')
                sum+=8;
            if(z.charAt(6)=='9')
                sum+=9;
            if(z.charAt(6)=='1')
                sum+=10;
            if(z.charAt(6)=='j')
                sum+=10;
            if(z.charAt(6)=='q')
                sum+=10;
            if(z.charAt(6)=='k')
                sum+=10;
            if(z.charAt(6)=='a')
            {
                if(sum<=10)
                    sum+=11;
                else
                    sum+=1;
            }                           
        }
        String score=""+sum;
        return score;   
    }
}
public class BlackJack extends JFrame
{
    public BlackJack()
    {
        add(new Board());       
        System.out.println("Starting BlackJack...");
        setTitle("Blackjack");
        setSize(getToolkit().getScreenSize().width, getToolkit().getScreenSize().height-40);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(true);
    }
    public static void main(String args[]) 
    {
        new BlackJack();
    }
}
4

4 回答 4

2

问题:您创建了一个新的 Board 对象,但您将它添加到什么?

答:什么都没有,所以它没有办法表现出来。

话虽如此,更好的解决方案不是创建新的 Board 对象,而是通常弄清楚如何将当前 Board 重置为其初始状态。

于 2013-05-03T23:47:58.070 回答
2

永远不要通过任何paint方法修改 UI 的状态。这只会导致一次又一次地提出新的重绘请求......直到它消耗你的CPU(变成一个奇点并将你的PC吸到遗忘......)

此外,当我们这样做时,您应该覆盖paintComponent而不是paint. Paint 负责绘制您现在正在绘制的所有子组件。从长远来看,这将给你带来无尽的痛苦。

于 2013-05-03T23:49:56.407 回答
2
  1. 不要使用静态变量,没有必要这样做。

  2. 不要使用空布局。Swing 旨在与布局管理器一起使用。

  3. 不要覆盖paint() 方法。自定义绘画是通过覆盖 paintComponent() 方法来完成的。

  4. 不要在任何绘画方法中添加/删除组件。

于 2013-05-03T23:52:52.990 回答
1

我同意“camickr”……由于变量是static,它们不会被重新启动,它们会保持以前的设置和偏好。我建议这个...

public class BlackJack
{
    public static JFrame frame;

    public BlackJack()
    {
        frame = new JFrame("BlackJack");

        add(new Board());       
        System.out.println("Starting BlackJack...");
        setSize(getToolkit().getScreenSize().width, getToolkit().getScreenSize().height-40);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(true);
    }

    public static void main(String args[]) 
    {
        new BlackJack();
    }
}

制作 BlackJack JFramestatic这样您就可以从中移除板子,然后添加一个新板......将此添加到用户所做的重置它的操作中。像JButton'sActionListener或者如果用户点击键盘上的一个字母,比如 'R',然后 Reset... Like So...</p>

public void reset()
{
    frame.remove(this);
    frame.add(new Board());
}

如果这不能回答您的问题,请告诉我。如果这对任何人来说似乎都不合理,请帮助我弄清楚。:D

于 2014-03-28T02:05:58.953 回答