Results 1 to 4 of 4
  1. #1
    Fily55 is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default Jlabel and Jbutton class with no instance variable-help

    Hello,
    I am making a program that adds a button and a label to a GUI. Initially the text of the label is set to 0. When you click the button, the text of the label increments and then changes to 1. Then 2, then 3 and so on. I got this program to work with an instance variable but I've to get it to work with no instance variables! I was wondering maybe you have to pass a variable onto public void actionPerformed() but I can't figure out how. This is my code:

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

    public class Button {

    public Button() {
    createFrame();
    }

    private void createFrame() {
    JButton button= new JButton("1-1");
    JLabel label = new JLabel("0");
    JFrame frame = new JFrame( "Buttons Grid" );
    GridLayout grid = new GridLayout( 0,1 );
    frame.setLayout(grid);
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    frame.setSize(200, 200);
    frame.setVisible(true);

    addComponents(frame.getContentPane(), button, label);

    }

    private void addComponents(Container contentPane, JButton button, JLabel label) {
    button.addActionListener(new ButtonListener(label));
    label.setHorizontalAlignment( SwingConstants.CENTER );
    contentPane.add(button);
    contentPane.add(label);

    }
    private class ButtonListener implements ActionListener {
    JLabel label;
    private ButtonListener(JLabel label){
    this.label=label;
    }
    @Override
    public void actionPerformed( ActionEvent event ) {
    int clicks;
    clicks = Integer.parseInt(label.getText() );
    clicks++;
    label.setText(""+clicks+"");
    }
    }

    public static void main( String[] args ) {
    Button bal = new Button();
    }

    }

    As you can see in the inner class I have : JLabel label;
    That can't be there. Any help would be appreciated, thank you.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Fily55 View Post
    Hello,
    I am making a program that adds a button and a label to a GUI. Initially the text of the label is set to 0. When you click the button, the text of the label increments and then changes to 1. Then 2, then 3 and so on. I got this program to work with an instance variable but I've to get it to work with no instance variables! I was wondering maybe you have to pass a variable onto public void actionPerformed() but I can't figure out how. This is my code:

    ....

    As you can see in the inner class I have : JLabel label;
    That can't be there. Any help would be appreciated, thank you.
    Please define precisely what you mean by "get it to work with no instance variables"? No instance variables in the outer class, Button? No instance variables in the private inner class, ButtonListener? There has to be an accessible reference to the JLabel object somewhere for this to work. In fact if you post the actual assignment requirements, we'd rid ourselves of a bit of ambiguity.

    Also, to get the best help here, you'll want to repost your code, but post formatted code using code tags. To learn how to use code tags, please see the first link in my signature links below.
    Last edited by Fubarable; 02-17-2011 at 01:13 AM.

  3. #3
    Fily55 is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Post

    Oops sorry.

    Code:
    Java Code:
    <script type="text/javascript">
    
    public class Button {
    
    public Button() {
    createFrame();
    }
    
    private void createFrame() {
    JButton button= new JButton("1-1");
    JLabel label = new JLabel("0");
    JFrame frame = new JFrame( "Buttons Grid" );
    GridLayout grid = new GridLayout( 0,1 );
    frame.setLayout(grid);
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    frame.setSize(200, 200);
    frame.setVisible(true);
    
    addComponents(frame.getContentPane(), button, label);
    
    }
    
    private void addComponents(Container contentPane, JButton button, JLabel label) {
    button.addActionListener(new ButtonListener(label));
    label.setHorizontalAlignment( SwingConstants.CENTER );
    contentPane.add(button);
    contentPane.add(label);
    
    }
    private class ButtonListener implements ActionListener {
    JLabel label;
    private ButtonListener(JLabel label){
    this.label=label;
    }
    @Override
    public void actionPerformed( ActionEvent event ) {
    int clicks;
    clicks = Integer.parseInt(label.getText() );
    clicks++;
    label.setText(""+clicks+"");
    }
    }
    
    public static void main( String[] args ) {
    Button bal = new Button();
    }
    
    }
    </script>
    The assignment is to get this working which I did. The challenge is you can't use any instance variables whatsoever. There can't be any in the outer class or the inner class.

    The assignment is to make a JButton and a JLabel. When the button is clicked the text in the JLabel will increment by 1. The initial text of the JLabel is 0. But the challenge is you can't use any instance variables anywhere.

    Thanks

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

    Default Re: Jlabel and Jbutton class with no instance variable-help

    Edit, just realized this is an only posting that was hacked with a junk comment. Please don't reply.
    Last edited by camickr; 12-16-2013 at 05:38 PM.

Similar Threads

  1. Jlabel,Jbutton,If, Trying to connect them all??
    By 2scoopdelux in forum New To Java
    Replies: 5
    Last Post: 12-11-2009, 06:15 PM
  2. Replies: 20
    Last Post: 06-03-2009, 05:33 PM
  3. Replies: 2
    Last Post: 04-19-2009, 05:39 AM
  4. create new instance of variable class
    By Fedor in forum New To Java
    Replies: 5
    Last Post: 04-12-2009, 08:13 PM
  5. Naming a class instance with a variable
    By pikalex88 in forum New To Java
    Replies: 3
    Last Post: 09-30-2008, 06:27 PM

Posting Permissions

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