按下按钮时重新绘制不起作用

时间:2013-05-22 06:01:19

标签: java user-interface applet repaint

我正在尝试让我的代码工作,以便当您推送“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(){

}

}

1 个答案:

答案 0 :(得分:1)

您尚未将任何ActionListener附加到go按钮...

尝试在go.addActionListener(this);

之后添加nameOk.addActionListener(this);