2013-05-22 38 views
0

我想讓我的代碼工作,當你按下「Go!」時按鈕,它可以繪製一張牌或洗牌,具體取決於所選的複選框。我想我可以在以後解決,但現在的問題是,當我推「Go!」時按鈕,沒有任何反應;它不會繪製框或使用g.drawString()進行寫入。當按鈕被按下時,repaint不工作

代碼按順序發生,這意味着你顯示了一個文本框和一個按鈕,當按下該按鈕時,清除屏幕然後顯示「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(){ 

} 

}

+0

Becareful具有壓倒一切的' paint'。不調用'super.paint'可以防止它渲染任何子組件 – MadProgrammer

回答

1

任何ActionListenergo按鈕...

嘗試增加go.addActionListener(this);nameOk.addActionListener(this);

+1

謝謝,我知道我需要的只是一雙新鮮的眼睛! (和一些睡眠) – ClassicTheMedic

+1

+1睡覺:P – MadProgrammer