循环结束后如何禁用JButton

时间:2016-10-11 11:08:36

标签: java

你好,这是我的主要课程。如果我单击按钮,示例。 EASY按钮,它将出现另一种我循环进入的形式。无论如何,这是一个标志测验。如果循环达到9以上(currentItem> 9)看到下一个代码,它将再次设置主类,它具有EASY按钮,现在我的问题是,如何禁用EASY按钮一旦我完成循环?

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);
    }

} 

2 个答案:

答案 0 :(得分:2)

禁用按钮很简单 - 您只需致电myButton.setEnabled(true),但这不是您的问题所在。相反,你的问题,最简单的本质,是:

  

如何根据B类状态的变化(currentItem的值&gt; 9)更改A类的状态(按钮是否启用)

我看到了两种方法之一:

  • 当currentItem为&gt;时,要么让B类调用A类的方法。 9,或
  • 让A类通过监听currentItem的值或其他一些状态变量来监听A类状态的变化。

我自己,我最喜欢第二条路线,因为这样可以让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);
            }
        }
    }


}

答案 1 :(得分:0)

使全局变量代表您的游戏状态(其中三个),即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
    }