There is a class that adds Button and TextField to the panel. But, when in ColorAction I try to register actions when I press a button, the compiler does not see the panel, Button, Textfield. Where is the mistake?

class BPanel extends JPanel { public BPanel() { JButton Button = new JButton("Button_"); JTextField Text = new JTextField(15); add(Button); add(Text); ActionListener actionListener = new ColorAction(); Button.addActionListener(actionListener); } protected class ColorAction implements ActionListener { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(panel, "ppppp"); //ошибка, не видит panel } } } class ButtonFrame extends JFrame { public ButtonFrame() { setTitle("Test"); setSize(500, 200); BPanel panel = new BPanel(); Container contentPane = getContentPane(); contentPane.add(panel) ; } } 
  • Um, I also do not see such a field / variable. Where did you get the panel at all? - falstaf
  • @falstaf, I added the class in which the panel is created - roman_ya111
  • @ roman_ya111 and what makes you think that a local variable from the constructor of a completely different class ( ButtonFrame ) should be visible in another class ( BPanel )? Read more about the scope of variables - DreamChild
  • @DreamChild, but for the same reason I can’t access Button and Text? - roman_ya111
  • Button and Tex are local variables of the BPanel class constructor . And, of course, you can access these variables only from this constructor. That is, you can not use them even in other methods of this class. Once again, I strongly recommend that you read about the scope of variables, this will answer all similar questions - DreamChild

0