Results 1 to 3 of 3
  1. #1
    adlb1300 is offline Member
    Join Date
    Jul 2007
    Posts
    46
    Rep Power
    0

    Default Need help with JButton 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:

    Java 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

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    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:
    Java Code:
    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.
    Java Code:
    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);
        }
    }
    Last edited by hardwired; 11-19-2007 at 12:30 AM. Reason: fix typo

  3. #3
    adlb1300 is offline Member
    Join Date
    Jul 2007
    Posts
    46
    Rep Power
    0

Similar Threads

  1. JButton onClick?
    By Joe2003 in forum AWT / Swing
    Replies: 2
    Last Post: 01-06-2008, 04:04 PM
  2. Help with JButton and layout
    By adlb1300 in forum AWT / Swing
    Replies: 1
    Last Post: 12-25-2007, 09:33 AM
  3. mouse over on JButton
    By gradon in forum Java Applets
    Replies: 1
    Last Post: 08-04-2007, 06:50 AM
  4. Few action in one Jbutton
    By kubiasty in forum New To Java
    Replies: 0
    Last Post: 07-25-2007, 11:19 AM
  5. Problem with JButton
    By Marcus in forum AWT / Swing
    Replies: 1
    Last Post: 07-05-2007, 06:56 AM

Posting Permissions

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