2016-10-11 21 views
-1

嗨,您好,這是我的主類。如果我點擊按鈕,例如。 EASY按鈕,它會出現我製作循環的另一種形式。無論如何,這是一個標誌測驗。如果循環達到9以上(currentItem> 9)請參閱下一個代碼,它會將可見性設置爲具有EASY按鈕的主類,現在我的問題是,如何在完成循環後禁用EASY按鈕?如何在循環完成後禁用JButton

package rebuzz; 

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class Rebuzz extends JFrame implements ActionListener 
{ 

public static void main (String args []) 
{ 
    new Rebuzz().setVisible(true); 
} 
public Rebuzz() 
{ 
    super("sample"); 
    setSize(400,300); 
    setResizable(false); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
    setLocationRelativeTo(null); 
    Menu(); 
} 

/*private void Easy() 
{ 
    JButton next = new JButton("Next"); 
    setLayout(new FlowLayout(1,200,25)); 
    next.addActionListener(this); 
    JLabel pic = new JLabel(); 
    pic.setIcon(new ImageIcon("1easy.gif")); 
    add(pic); 
    add(answer); 
    add(next); 
}*/ 

    public void Menu() 
     { 
      easyButton = new JButton("easy");//EASY BUTTON 
      easyButton.setActionCommand("easy"); 
      setLayout(new FlowLayout(1,200,25)); 
      easyButton.addActionListener(this); 

      mediumButton = new JButton ("Medium");//MEDIUM BUTTON 
      mediumButton.setActionCommand("medium"); 
      setLayout(new FlowLayout(1,200,25)); 
      mediumButton.addActionListener(this); 

      insaneButton = new JButton ("Insane");//INSANE BUTTON 
      insaneButton.setActionCommand("insane"); 
      setLayout(new FlowLayout(1,200,25)); 
      insaneButton.addActionListener(this); 
      add(easyButton); 
      add(mediumButton); 
      add(insaneButton); 

     } 

@Override 
public void actionPerformed(ActionEvent e) 
{ 
    String open = e.getActionCommand(); 

    if (open.equals("easy")) 
    { 
     Easy easy = new Easy(); 
     this.setVisible(false); 
     easy.setVisible(true); 
    } 
    else if (open.equals("medium")) 
    { 
     Medium medium = new Medium(); 
     this.setVisible(false); 
     medium.setVisible(true); 
    } 
    else if (open.equals("insane")) 
    { 
     Insane insane = new Insane(); 
     this.setVisible(false); 
     insane.setVisible(true); 
    } 
} 
} 

這是我的EASY類

package rebuzz; 

import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JTextField; 


public class Easy extends JFrame implements ActionListener 
{ 
JLabel[] img = new JLabel[11]; 
JTextField[] answer = new JTextField[11]; 
String[] rightAnswer = {"cross breed", "forgive and forget", "side show",  "big brother", "cross roads", "quite right", "small talk", "too little too late", "see for yourself", "once in a blue moon"}; 
ImageIcon[] easy = new ImageIcon[rightAnswer.length]; 


//int currentItem=0; 
int currentItem; 
JLabel imageContainer; 
JTextField answerContainer; 
JButton submit; 
JButton home; 
JButton back; 
String ans; 
private String[] args; 
//private String[] frame; 

public Easy() 
{ 
    super("Easy Sample"); 
    setSize(300,400); 
    setResizable(true); 
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
    setLocationRelativeTo(null); 
    Easy1(); 
} 

public void Easy1() 
{ 
imageContainer = new JLabel(); 
for(int x = 0; x < rightAnswer.length ; x++) 
{ 
    System.out.println(Integer.toString(x));//loop output 
    easy[x] = new ImageIcon((x+1)+"easy.gif"); 

} 

//JPanel jpanel = new JPanel(); 

answerContainer = new JTextField(null, 10); 
currentItem = 0; 
imageContainer.setIcon(easy[currentItem]); 
ans = rightAnswer[currentItem]; 
submit = new JButton("Submit"); 
home = new JButton("Home"); 

submit.addActionListener(new java.awt.event.ActionListener() 
{ 
     public void actionPerformed(java.awt.event.ActionEvent evt) 
     { 
      submitActionPerformed(evt); 
     } 
}); 

home.addActionListener(new java.awt.event.ActionListener() 
{ 
     public void actionPerformed(java.awt.event.ActionEvent evt) 
     { 
      homeActionPerformed(evt); 

     } 
}); 
back.addActionListener(new java.awt.event.ActionListener() 
{ 
     public void actionPerformed(java.awt.event.ActionEvent evt) 
     { 
      backActionPerformed(evt); 

     } 
}); 

setLayout(new FlowLayout(1,20,10)); 
add(imageContainer); 
add(answerContainer); 
add(submit); 
add(home); 

}   
@Override 
public void actionPerformed(ActionEvent e) 
{ 

} 

private void submitActionPerformed(java.awt.event.ActionEvent evt) 
{ 
    if (answerContainer.getText().equalsIgnoreCase(ans)) 
    { 
      if (currentItem < 9) 
       { 
        JOptionPane.showMessageDialog(null, "No."+(currentItem+1)+" Easy Puzzle is correct!","Great!",JOptionPane.PLAIN_MESSAGE); 
        currentItem++; 
        imageContainer.setIcon(easy[currentItem]); 
        ans = rightAnswer[currentItem]; 
        answerContainer.setText(""); 

       } 
      else 
      { 
       JOptionPane.showMessageDialog(null,"Done!"); 
       this.setVisible(false); 
       Rebuzz.main(args); 
       Rebuzz rebuzz = new Rebuzz(); 
       rebuzz.easy = false; 
      } 
    } 
    else if (answerContainer.getText().equals("")) 
    { 
     JOptionPane.showMessageDialog(null,"Please enter your answer.","Errrr",JOptionPane.ERROR_MESSAGE); 
    } 
    else 
    { 
     JOptionPane.showMessageDialog(null, "Incorrect Answer!"); 
     answerContainer.setText(""); 
    } 
} 

    } 
    private void homeActionPerformed(ActionEvent evt) 
    { 
     Rebuzz.main(args); 
     this.setVisible(false); 
    } 

} 
+1

那裏在你的代碼中沒有循環。 – xenteros

+0

要禁用一個按鈕很簡單,可以調用'myButton.setEnabled(false)'或者如果它有一個分配給它的Action,則對該按鈕的Action執行相同的調用。 –

+0

@xenteros其實有一個for循環Easy類。 – eldo

回答

2

禁用按鈕很容易 - 你只需撥打myButton.setEnabled(true),但是這不是真的你的問題是什麼。相反,你的問題,蒸餾,以最簡單的本質,就是:

我怎樣才能改變A類的狀態(啓用與否按鈕)的基礎上修改的B類的狀態(CURRENTITEM值> 9 )

而且我看到的兩種方法可以做到這一個:

  • 要麼有B級呼叫類A的方法,一旦CURRENTITEM是> 9,或
  • 有A級更改聽由李,到A級的狀態轉到currentItem的值或其他一些狀態變量。

我自己,我更喜歡第二個途徑最好的,因爲這允許B類沿其快樂的方式去,而不必對A級的任何知識,一個體面的方式來做到這一點是有A類添加一個監聽器類當財產狀態發生變化時通知B。我會考慮使用PropertyChangeListener。 B可以創建一個方法setCurrentItem(int currentItem)並在其中通知其屬性更改支持,並且這將通知所有偵聽器currentItem已更改。它還需要int getCurrentItem()方法,以便A可以查詢B的狀態。

例如:

import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import java.beans.PropertyChangeEvent; 
import java.beans.PropertyChangeListener; 

import javax.swing.*; 

public class WatchState extends JPanel { 
    private A a = new A(); 
    private B b = new B(a); 

    public WatchState() { 
     setLayout(new BorderLayout()); 
     add(a); 
     add(b, BorderLayout.PAGE_END); 
    } 

    private static void createAndShowGui() { 
     WatchState mainPanel = new WatchState(); 

     JFrame frame = new JFrame("WatchState"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGui(); 
      } 
     }); 
    } 
} 

class A extends JPanel { 
    // a String for the property name 
    public static final String CURRENT_ITEM = "current item"; 
    private int currentItem = 0; 

    public A() { 
     add(new JButton(new AbstractAction("Press Me to Advance Current Item") { 

      @Override 
      public void actionPerformed(ActionEvent arg0) { 
       int temp = getCurrentItem(); 
       temp++; 
       setCurrentItem(temp); 
      } 
     })); 
     setBorder(BorderFactory.createTitledBorder("Class A")); 
    } 

    public int getCurrentItem() { 
     return currentItem; 
    } 

    public void setCurrentItem(int currentItem) { 
     int oldValue = this.currentItem; 
     int newValue = currentItem; 
     this.currentItem = currentItem; 
     firePropertyChange(CURRENT_ITEM, oldValue, newValue); 
    }  

} 

class B extends JPanel { 
    public static final int MAX_CURRENT_ITEM = 9; 
    private A a; 
    private JLabel currentItemStateLabel = new JLabel("00"); 
    private JButton someButton = new JButton("Some Button"); 

    public B(A a) { 
     this.a = a; 
     a.addPropertyChangeListener(A.CURRENT_ITEM, new CurrentItemListener()); 
     add(someButton); 
     add(new JLabel("Current Item: ")); 
     add(currentItemStateLabel); 
     setBorder(BorderFactory.createTitledBorder("Class B")); 
    } 

    private class CurrentItemListener implements PropertyChangeListener { 
     @Override 
     public void propertyChange(PropertyChangeEvent evt) { 
      int newValue = (int) evt.getNewValue(); 
      currentItemStateLabel.setText(String.format("%02d", newValue)); 
      if (newValue > MAX_CURRENT_ITEM) { 
       someButton.setEnabled(false); 
      } 
     } 
    } 


} 
0

使全局變量代表你的遊戲狀態(3臺),即boolean game_state_easy = false;

例如:

if (open.equals("easy")) 
    { 
     Easy easy = new Easy(); 
     this.setVisible(false); 
     easy.setVisible(true); 
     game_state_easy = true; 
    } 
在你的菜單

然後:

easyButton = new JButton("easy");//EASY BUTTON 
    easyButton.setActionCommand("easy"); 
    setLayout(new FlowLayout(1,200,25)); 
    easyButton.addActionListener(this); 
    if (game_state_easy){ 
     //disable the button 
    }