简体   繁体   中英

How to turn off a JButton using another JButton

I would just like to preface this post by informing you that this is my first time posting, so if there are any mistakes, let me know.

I am attempting to code a blackjack game for my high school computer math (programming) class and I have a multitude of errors, but the one that is really annoying is this one.

My GUI has 2 JButtons, a hit me button, "Hit Me," and a stand button, "Stand." Hit Me uses Math.random to give the player a card and also tracks to see if they go bust. The Stand button needs to disable the Hit Me button when the player clicks it. I searched this site for answers and the one I got was using the setEnabled method and setting it to HitMe.setEnabled(false). I also found people who said the JButtons need to be set to final such that :

final JButton name = new JButton("");

So, I set the JButtons to final and inserted the setEnabled method in the code for the ActionListeners as such

private class StandListener implements ActionListener
{
   public void actionPerformed(ActionEvent e)
   {
      HitMe.setEnabled(false);      
   }
}

and

final JButton HitMe = new JButton("Hit Me");

The set.Enabled(false) does not work inside the ActionListener code. The "final" identifier does not work either. I really need help with this, so any assistance as soon as possible is appreciated.

I will post some more problems later. :)

EDIT 17 April 2014:

I am writing this because I just realized that, if anyone looks at this page, they may be able to diagnose the errors I got. I am using jGrasp for my java coding if that has an effect on your answers.

Here are the errors:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at PlayerBoxClubs$StandListener.actionPerformed(PlayerBoxClubs.java:105)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

You need to add an instance of your ActionListener (so an instance of StandListener) to the stand JButton.

So something like:

myStandButton.addActionListener(new StandListener());

You could also just add a new anonymous class instance as an ActionListener too, like:

myStandButton.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
        HitMe.setEnabled(false);
    }
});

Kind of easier.

People have probably mentioned to you to make the JButton final, because they probably assumed you were going to use an anonymous class, where you can only reference other Objects in an anonymous class if they are final.

Try This :

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

class MyGui1 extends JFrame implements ActionListener {
JButton b1,b2;
public MyGui1()
{
setVisible(true);
setSize(1000,1000);
setLayout(null);
b1=new JButton("Enable");
b1.setBounds(10,10,100,100);
add(b1);
b2=new JButton("on");
b2.setBounds(110,110,100,100);
add(b2);
add(b1);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
    if(e.getSource()==b1)
    {
    b2.setEnabled(false);
    b2.setText("off");
      }
    }
     public static void main (String[] args) {
     MyGui1 m=new MyGui1();
   }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM