M
Mmm_moo_cows
Hi,
Pretty new to java and I have hit a small problem with event
listeners, so any help would be very much appreciated.
I wrote a simple program with a jframe and button. When the button is
clicked, the event listener displays a simple confirmation message in
the console via the System.out.print command.
My problem is I have created an anonymouse inner class (I think its
called that), which is giving me alot of strife when I want to use
integers/objects from my main program.
The simplest example I would like to program is a simple button click
counter. i.e. every time you click a button a message could be
displayed in the console - "Number of clicks = " + counter .
This is what I have so far:-
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class gui
{
public static void main (String[] args)
{
JFrame frame = new JFrame("Frame");
frame.setSize(150, 70);
Container content = frame.getContentPane();
content.setBackground(Color.white);
content.setLayout(new FlowLayout());
JButton clickme = new JButton("Click Me!");
content.add(clickme);
frame.setVisible(true);
clickme.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("You clicked me");
}
}
);
}
}
Every time I try and include a variable from the main gui class as a
counter I get an error message:
'local variable a is accessed from within inner class; needs to be
declared final'
The real use/application I am looking for is if I had some text boxes
or combo boxes etc (i.e. user input) and a submit button or something.
So, when you click the button it goes into the action event listener
and pulls the input data from those input boxes on the first form, so
it can work something out.
An example may be having 2 text boxes for 2 numbers and a calculate
button which will add them us and display the result in the system
console. (keeping it simple for now!). I'm not too sure how I would
get data from input boxes from within an inner class as well, i.e.
like getting ahold of those two numbers whilst in the inner class from
that example above.
Any help would be much appreciated. I have been searching on the
internet and can only find information about setting the event
lisenter up to do an event, but nothing about actually using
objects/variables in the event.
Or am I approaching this a completely wrong way? if so could you
please point me in the right direction.
Thanks for any help in advance, it will be greatly appreciated.
Cheers, Jon
Pretty new to java and I have hit a small problem with event
listeners, so any help would be very much appreciated.
I wrote a simple program with a jframe and button. When the button is
clicked, the event listener displays a simple confirmation message in
the console via the System.out.print command.
My problem is I have created an anonymouse inner class (I think its
called that), which is giving me alot of strife when I want to use
integers/objects from my main program.
The simplest example I would like to program is a simple button click
counter. i.e. every time you click a button a message could be
displayed in the console - "Number of clicks = " + counter .
This is what I have so far:-
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class gui
{
public static void main (String[] args)
{
JFrame frame = new JFrame("Frame");
frame.setSize(150, 70);
Container content = frame.getContentPane();
content.setBackground(Color.white);
content.setLayout(new FlowLayout());
JButton clickme = new JButton("Click Me!");
content.add(clickme);
frame.setVisible(true);
clickme.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("You clicked me");
}
}
);
}
}
Every time I try and include a variable from the main gui class as a
counter I get an error message:
'local variable a is accessed from within inner class; needs to be
declared final'
The real use/application I am looking for is if I had some text boxes
or combo boxes etc (i.e. user input) and a submit button or something.
So, when you click the button it goes into the action event listener
and pulls the input data from those input boxes on the first form, so
it can work something out.
An example may be having 2 text boxes for 2 numbers and a calculate
button which will add them us and display the result in the system
console. (keeping it simple for now!). I'm not too sure how I would
get data from input boxes from within an inner class as well, i.e.
like getting ahold of those two numbers whilst in the inner class from
that example above.
Any help would be much appreciated. I have been searching on the
internet and can only find information about setting the event
lisenter up to do an event, but nothing about actually using
objects/variables in the event.
Or am I approaching this a completely wrong way? if so could you
please point me in the right direction.
Thanks for any help in advance, it will be greatly appreciated.
Cheers, Jon