|
-
Nov 18th, 2007, 04:37 PM
#1
Thread Starter
Junior Member
[RESOLVED] Help needed with button event
I'm trying to code an JButton and cannot get the code to compile correctly. When I add the action listener to the button I get an error that non-static variable this cannot be referenced from a static context. I'm also having problems with the method that is setup to handle the event. Here is my code:
Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.*;
import java.awt.*;
public class GuessNumber implements ActionListener{
public static void main(String[] args) {
JButton okButton;
JLabel msgLabel;
final JTextField answerTextField;
JPanel panel;
String askForNum = "Please enter what you think the random number is below.";
// generates randomNumber between 1 and 100
int randomNumber;
int max = 100;
Random random = new Random();
randomNumber = random.nextInt(max +1);
// output of random number for testing purposes remove prior to submission
System.out.println(randomNumber);
// creates JLabel that asks user to guess what the random number is and enter it below
msgLabel = new JLabel(askForNum, JLabel.CENTER);
msgLabel.setSize(400, 150);
msgLabel.setFont(new Font("Times New Roman", Font.BOLD, 14));
answerTextField = new JTextField();
answerTextField.setFont(new Font("Times New Roman", Font.PLAIN, 14));
answerTextField.setSize(75, 30);
answerTextField.setVisible(true);
okButton = new JButton("Ok");
okButton.setSize(30, 30);
okButton.setFont(new Font("Times New Roman", Font.PLAIN, 14));
okButton.addActionListener(this);
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(msgLabel);
panel.add(answerTextField);
panel.add(okButton);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(panel);
f.setSize(400, 200);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == okButton){
}
}
}
Thanks
Last edited by SilentCodingOne; Nov 18th, 2007 at 04:41 PM.
-
Nov 18th, 2007, 05:06 PM
#2
Re: Help needed with button event
The main method as you know is "statis" which means that it doesn't belong to a certain instance of the "GuessNumber" class. it belongs to the class itself and is shared among all instances. On the other hand, the "actionPerformed" method is not static. thus belongs to the current instance which means it cannot be accessed from the main method without creating a new instance.
Another mistake you're doing is comparing "e.getSource" to "okButton" where the latter is a local member to the main method and cannot be accessed from anywhere outside the main method. not even other static methods.
There are lots of ways to do this but the easiest would be to implement an anonymous class. like the following:
Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.*;
import java.awt.*;
public class GuessNumber {
public static void main(String[] args) {
final JButton okButton;
JLabel msgLabel;
final JTextField answerTextField;
JPanel panel;
String askForNum = "Please enter what you think the random number is below.";
// generates randomNumber between 1 and 100
int randomNumber;
int max = 100;
Random random = new Random();
randomNumber = random.nextInt(max + 1);
// output of random number for testing purposes remove prior to submission
System.out.println(randomNumber);
// creates JLabel that asks user to guess what the random number is and enter it below
msgLabel = new JLabel(askForNum, JLabel.CENTER);
msgLabel.setSize(400, 150);
msgLabel.setFont(new Font("Times New Roman", Font.BOLD, 14));
answerTextField = new JTextField();
answerTextField.setFont(new Font("Times New Roman", Font.PLAIN, 14));
answerTextField.setSize(75, 30);
answerTextField.setVisible(true);
okButton = new JButton("Ok");
okButton.setSize(30, 30);
okButton.setFont(new Font("Times New Roman", Font.PLAIN, 14));
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Hello World!");
}
});
panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(msgLabel);
panel.add(answerTextField);
panel.add(okButton);
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(panel);
f.setSize(400, 200);
f.setVisible(true);
}
}
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Nov 18th, 2007, 07:12 PM
#3
Thread Starter
Junior Member
Re: Help needed with button event
Thanks for the help. I'm relatively new to Java and button events are still giving me some issues.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|