Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Default Swing button unresponsive

    I've been working on this for a couple hours, after losing my flash drive at school I had to restart thank luck for that. Anyway I'm trying to just play with Swing as in my Programming class we are learning how to use it and our assignment is to make a program on our own. SO! what I wish to do is create a program that displays an image of Shawn from Shawn of the Dead saying "Go to the Winchester have a pint and wait for this to all blow over" and two buttons that says "Grab a pint" and if you click it open another frame that will display an image of a pint of beer, and then another button that says "Don't Grab a pint." Which will show an image of a zombie. Then Close.
    I've gotten it to display the image and the buttons, but the action Listeners won't actually I guess get the action. I've got two Objects an extension of JPanel and JFrame one called (frame) Skeleton and one called board(panel) (as in backboard.) the image is attatched to the backboard and so are the buttons. Code below any one able to help?

    Java Code:
    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.JPanel;
    public class board extends JPanel implements ActionListener
    {
        ImageIcon Pint;    
        JLabel picture;
        JButton Grabn, NGrabn;
        JPanel buttonHolder;
        public board()
        {
          
          ImageIcon Pint = new ImageIcon( this.getClass().getResource("blowover.jpg"));  
          JLabel picture = new JLabel(new ImageIcon(Pint.getImage()));
          //add(picture);
          //Button:GRAB A PINT!
           JButton Grabn = new JButton("GRAB A PINT!");
           add(Grabn);
          //Button:Don't Grab a pint
           JButton NGrabn = new JButton("Don't grab a pint...:(");
           add(NGrabn);
           add(picture);
             
            Grabn.addActionListener(this);
          NGrabn.addActionListener(this);
            
        }
         public void actionPerformed(ActionEvent e) 
        {
            if(e.getSource() == Grabn)
            {            
                 System.out.println("New Game - button is working");
                 //we will be adding a couple more actions here later...
            }
            else if(e.getSource() == NGrabn)
            {
                System.out.println("Game is exiting - button is working");
                System.exit(0);
            }
        }
    
    }
    and then the Skeleton Object
    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    public class Skeleton extends JFrame 
    {
     
        public Skeleton()
        {
           add(new board());
           setTitle("Skeleton");
           setDefaultCloseOperation(EXIT_ON_CLOSE);
           setSize(500,500);
           setLocationRelativeTo(null);
           setVisible(true);
           setResizable(true);
           //actionlisteners
         
        }
    
        public static void main()
        {
            new Skeleton();
        }
       
    }

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: Swing button unresponsive

    First of all use standard Java naming conventions. How do you expect to pass the class if you don't follow the standards? Variable names do not start with an upper case character.

    Your problem is that you have defined your buttons twice, once as a class variable and the other as a local variable. Get rid of the local variable.

  3. #3
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Default Re: Swing button unresponsive

    Hey thanks that worked! I saw other people were doing that in example pieces of code and thought it was a standard way of doing thanks. I removed the local variable and it works now! And Sorry about the standard naming thing, we haven't been taught that at all in my Java Class

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Swing button unresponsive

    the standard naming thing, we haven't been taught that at all in my Java Class
    Judging from the posts you see here, you aren't alone.

    Just so you know: variables and methods start with a lower case letter and use camelCase. Types (classes, interfaces) start with AnUppercaseLetter. It makes code a lot easier to read.

    Bit of a rant follows... but if I were teaching Java I wouldn't go much past HelloWorld and the observation that Java works before introducing (and insisting on) coding standards. And documentation. And a how-to survival guide to deal with the handful of favorite compiler complaints (NullPointerException and friends). And you wouldn't get full credit for an assignment unless you *had* sought help on a public forum.

Similar Threads

  1. Swing Button and Text Area Function
    By ScoutDavid in forum AWT / Swing
    Replies: 13
    Last Post: 12-21-2010, 02:07 PM
  2. How to run a jar file from a Button click in swing.
    By killerf2006 in forum New To Java
    Replies: 1
    Last Post: 07-23-2010, 12:55 PM
  3. how to impliment minimize button in swing
    By prasad.vara in forum AWT / Swing
    Replies: 1
    Last Post: 07-20-2010, 09:34 AM
  4. Replies: 2
    Last Post: 11-06-2009, 02:56 PM
  5. [SOLVED] Gui becomes unresponsive!
    By FezKazi in forum AWT / Swing
    Replies: 10
    Last Post: 03-04-2009, 04:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •