Results 1 to 3 of 3
- 02-16-2011, 11:11 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
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.
-
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.
- 02-17-2011, 08:02 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
Oops sorry.
Code:
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.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 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
Similar Threads
-
Jlabel,Jbutton,If, Trying to connect them all??
By 2scoopdelux in forum New To JavaReplies: 5Last Post: 12-11-2009, 06:15 PM -
Help me finish an instance method which references a class variable?
By trueblue in forum New To JavaReplies: 20Last Post: 06-03-2009, 05:33 PM -
[SOLVED] JButton actionlistener, if statement, JLabel visibility
By JonoF in forum New To JavaReplies: 2Last Post: 04-19-2009, 05:39 AM -
create new instance of variable class
By Fedor in forum New To JavaReplies: 5Last Post: 04-12-2009, 08:13 PM -
Naming a class instance with a variable
By pikalex88 in forum New To JavaReplies: 3Last Post: 09-30-2008, 06:27 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks