C:\jexp>javac gn.java
gn.java:40: non-static variable this cannot be referenced from a static context
okButton.addActionListener(this);
^
gn.java:58: cannot find symbol
symbol : variable okButton
location: class GN
if(e.getSource() == okButton){
^
2 errors
1 — You cannot use the "this" keyword in static context, ie, inside a method whose signature includes the
static modifier or with a field/variable that is declared
static. You can use a reference variable:
public class GN implements ActionListener {
static JButton okButton; // member variable has class scope
public static void main(String[] args) {
GN gn = new GN(); // reference to the enclosing class
// JButton okButton; // local variable
...
okButton.addActionListener(gn);
2 — the JButton "okButton" is declared inside a method (as a local variable) and thus cannot be seen inside the
actionPerformed method. To be seen inside
actionPerformed "okButton" must be in class scope, ie, declared as a member variable.
When making guis it is more elegant to put them together outside the main method.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.*;
import java.awt.*;
public class GN implements ActionListener{
JButton okButton;
private JPanel getContent() {
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
JLabel msgLabel = new JLabel(askForNum, JLabel.CENTER);
msgLabel.setSize(400, 150);
msgLabel.setFont(new Font("Times New Roman", Font.BOLD, 14));
JTextField answerTextField = new JTextField();
// The designers recommend that we specify a columns argument
// to help establish the size of a JTextField, eg, new JTextField(16)
// The setSize method is of little value until after realization.
// Swing components are made to work well with preferredSize.
// So another option is:
Dimension d = answerTextField.getPreferredSize();
d.width = 75;
answerTextField.setPreferredSize(d);
// Or answerTextField.setPreferredSize(new Dimension(75, 30));
// Choice of parent layout manager has a lot to do with how
// this will be shown/handled.
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); // "this" is okay now
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(msgLabel);
panel.add(answerTextField);
panel.add(okButton);
return panel;
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == okButton){
}
}
public static void main(String[] args) {
GN gn = new GN();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(gn.getContent());
f.setSize(400, 200);
f.setVisible(true);
}
}