Answer the question
In order to leave comments, you need to log in
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");
}
}
}
Answer the question
In order to leave comments, you need to log in
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
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 questionAsk a Question
731 491 924 answers to any question