I can not hide the JPanel by clicking on the button. Throws an error on the line Menu.setVisible(false); . What needs to be fixed? (Window rendered using another class)

Also, if there are any tips for improving the code, I will be glad to hear.

 package p1; import java.awt.*; import javax.swing.*; public class Window extends JFrame { int vert,hor,ma,mb; Window(){ super("p1"); Dimension sSize = Toolkit.getDefaultToolkit().getScreenSize(); vert = sSize.height; hor = sSize.width; ma=300; mb=30; setResizable(false); setUndecorated(true); setDefaultCloseOperation(EXIT_ON_CLOSE); JPanel Menu = new JPanel(); JButton Back = new JButton("Назад"); JButton Settings = new JButton("Настройки"); JButton Exit = new JButton("Выход"); Menu.setLayout(null); Menu.setBackground(Color.red); Settings.setSize(ma, mb); Settings.setLocation((hor/2)-(ma/2),200); Settings.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { SettingsActionPerformed(evt); } }); Menu.add(Settings); private void SettingsActionPerformed(java.awt.event.ActionEvent evt) { Menu.setVisible(false); } } 
  • one
    What kind of mistake is that? - Vartlok
  • On setVisible swears for some reason - TrueASL
  • Judging by the code should swear that he (the compiler) does not know such a variable. If so, then you need to read about the scope of variables. - Vartlok

1 answer 1

Try closing the Menu add inside ActionListener :

 Settings.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { SettingsActionPerformed(evt); } private void SettingsActionPerformed(ActionEvent evt) { Menu.setVisible(false); } }); Menu.add(Settings);