gpt4 book ai didi

java - 按下按钮时重绘不起作用

转载 作者:行者123 更新时间:2023-12-01 14:33:31 26 4
gpt4 key购买 nike

我正在尝试让我的代码正常工作,以便当您按下“Go!”时按钮,它要么抽取一手牌,要么洗牌,具体取决于所选的复选框。我想我可以稍后解决这个问题,但现在的问题是当我按下“开始!”时按钮,没有任何反应;它不绘制盒子或使用 g.drawString() 进行写入。

代码按顺序发生,这意味着您会看到一个文本框字段和一个按钮,按下该按钮会清除屏幕,然后显示“Go!”按钮和复选框。但是当 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(){

}

}

最佳答案

您尚未附加任何ActionListenergo按钮...

尝试添加go.addActionListener(this);之后nameOk.addActionListener(this);

关于java - 按下按钮时重绘不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16684513/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com