嗨,您好,這是我的主類。如果我點擊按鈕,例如。 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);
}
}
那裏在你的代碼中沒有循環。 – xenteros
要禁用一個按鈕很簡單,可以調用'myButton.setEnabled(false)'或者如果它有一個分配給它的Action,則對該按鈕的Action執行相同的調用。 –
@xenteros其實有一個for循環Easy類。 – eldo