我正在制作一个二十一点游戏。一切都完成了,我想制作一个重置按钮来开始游戏。我在ActionListener中删除了All(),但是当Board()再次启动时,它不会添加JButton draw或fold。请帮忙 附: 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();
}
}
答案 0 :(得分:2)
问题:您创建了一个新的Board对象,但是将它添加到了什么?
答案:没什么,所以没有办法让它显示出来。
话虽如此,更好的解决方案不是创建一个新的Board对象,而是通常要弄清楚如何将当前的Board重置为初始状态。
答案 1 :(得分:2)
请勿使用任何paint
方法修改用户界面的状态。这只会导致一个新的重绘请求被反复提出....直到它消耗你的CPU(变成一个奇点并让你的PC被遗忘......)
此外,在我们处理此问题时,您应该覆盖paintComponent
而不是paint
。 Paint负责绘制您正在绘制的所有子组件。从长远来看,这将使你永无止境。
答案 2 :(得分:2)
不要使用静态变量,不需要这样做。
不要使用null布局。 Swing旨在与布局管理器一起使用。
不要覆盖paint()方法。通过覆盖paintComponent()方法完成自定义绘制。
不要在任何绘画方法中添加/删除组件。
答案 3 :(得分:1)
我同意&#34; camickr&#34; ...由于变量是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 JFrame static
这样你就可以从中移除棋盘然后再添加一个...将其添加到用户重置它的内容中。与JButton
ActionListener
一样,或者如果用户点击键盘上的字母,例如&#39; R&#39;,则重置...就像这样......
public void reset()
{
frame.remove(this);
frame.add(new Board());
}
如果这没有回答你的问题,请告诉我。如果这对任何人都不合理,请帮我解决。 :d