我正在尝试让我的代码正常工作,以便当您按下“Go!”时 按钮,它要么抽一手牌,要么洗牌,具体取决于选中的复选框。我想我以后可以弄清楚,但现在的问题是当我按下“Go!”时 按钮,没有任何反应;它不使用 g.drawString() 绘制框或写入。
代码按顺序发生,这意味着您会看到一个文本框字段和一个按钮,按下该按钮会清除屏幕,然后显示“开始!” 按钮和复选框。但是当 go 被推动时什么也没有发生。
感谢您提供的任何帮助,非常感谢,这是代码(在其所有凌乱,未完成的荣耀中):
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class cardGame extends Applet implements ActionListener{
TextField nameField;
Button nameOk;
Button go;
Label title;
CheckboxGroup options;
Checkbox shuffle;
Checkbox deal;
Random rand;
Random rand2;
int[] deck;
public void init(){
setLayout(new FlowLayout());
menuFont = new Font("Arial",Font.BOLD,25);
nameField = new TextField("Type Name Here",35);
nameOk = new Button("Enter");
shuffle = new Checkbox("Shuffle Deck",options,false);
deal = new Checkbox("Deal",options,false);
go = new Button("Go!");
title = new Label("Welcome to the Card Table", Label.CENTER);
rand = new Random();
rand2 = new Random();
deck = new int[] {1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,11,11,11,11,12,12,12,12,13,13,13,13};
add(nameField);
add(nameOk);
add(title);
nameOk.addActionListener(this);
}
public void actionPerformed(ActionEvent evt){
if(evt.getSource() == nameOk){
clearAll();
newOptions();
}
if(evt.getSource() == go){
//if(deal.getState()==true&&shuffle.getState()==true)System.out.println("Only one, please");
if(shuffle.getState()){
shuffleDeck(deck);
}
clearAll();
repaint();
}
}
public void paint(Graphics g){
/*if(shuffle.getState()){
shuffleDeck(deck);
}*/
if(deal.getState())g.drawRect(100,100,100,100);
//if(deal.getState()==true&&shuffle.getState()==true)g.drawString("Only one, please",100,100);
}
public void clearAll(){
remove(nameField);
remove(nameOk);
remove(title);
remove(go);
remove(shuffle);
remove(deal);
revalidate();
}
public void newOptions(){
//Dimension d = getSize();
add(shuffle);
add(deal);
add(go);
revalidate();
}
public void shuffleDeck(int[] nums){
//int a=0,count2=0,count3=0,count4=0,count5=0,count6=0,count7=0,count8=0,count9=0,count10=0,countJ=0,countQ=0,countK=0;
int n;
int n2;
int temp;
for(int i = 52;i>0;i--){
n=rand.nextInt(52);
n2=rand2.nextInt(52);
temp=nums[n];
nums[n]=nums[n2];
nums[n2]=temp;
}
}
public void update(Graphics g){
paint(g);
}
public void stop(){
}
}