A
A
Alexey R2015-02-20 21:02:27
Java
Alexey R, 2015-02-20 21:02:27

What is wrong with ActionListener?

I apologize in advance, I returned to learning Java, and immediately got stuck on a simple example and I can’t figure out what this error is. I understand that it’s not nice to write code and ask to understand it, but in this situation, it seems to me there is no other way out. Tell me what to read about this and how to avoid such a mistake.

public class OOP {
  public static void main(String[] args) {
    
    
    JFrame frame = new JFrame();
    frame.setSize(400, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setLayout(new GridBagLayout());

    JTextField textf = new JTextField();
    JTextField textf2 = new JTextField();
    JButton Mybutton = new JButton("Start");
    
    
    
    frame.add(textf, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.9,
        GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
        new Insets(2, 2, 2, 2), 0, 0));

    frame.add(textf2, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.9,
        GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
        new Insets(2, 2, 2, 2), 0, 0));

    frame.add(Mybutton, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.9,
        GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
        new Insets(1, 2, 2, 2), 0, 0));
    
    Mybutton.addActionListener(new ButtonActionListener()); // Ошибка

    frame.setVisible(true);
    frame.pack();
    
    
  }
  
  
  public class ButtonActionListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
          JOptionPane.showMessageDialog(null, "Messege box");
      
    }
    
  }
}

As a result, the listener does not work.
fec2a82e8ae64d13a3ba07d094395b74.JPG
Apparently I'm making mistakes in the OOP concept, but I can't figure out which one. I take an example from video lessons, and just there everything is fine

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yuri Vitkovsky, 2015-02-20
@Axeles

The problem is that static code that runs in a public static void main method cannot access a non-static inner class. Either make the class public static class ButtonActionListener implements ActionListener or move the main code into a non-static method

A
asd111, 2015-02-21
@asd111

Alternatively, you can make ButtonActionListener package-private a class

public class OOP {
  public static void main(String[] args) {
    
    JFrame frame = new JFrame();
    frame.setSize(400, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setLayout(new GridBagLayout());

    JTextField textf = new JTextField();
    JTextField textf2 = new JTextField();
    JButton Mybutton = new JButton("Start");
    
    frame.add(textf, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.9,
        GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
        new Insets(2, 2, 2, 2), 0, 0));

    frame.add(textf2, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.9,
        GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
        new Insets(2, 2, 2, 2), 0, 0));

    frame.add(Mybutton, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.9,
        GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL,
        new Insets(1, 2, 2, 2), 0, 0));
    
    Mybutton.addActionListener(new ButtonActionListener()); // Ошибка

    frame.setVisible(true);
    frame.pack();   
  } 
}

class ButtonActionListener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
          JOptionPane.showMessageDialog(null, "Messege box");      
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question