0
    class MyPanel extends JPanel{
        MyPanel(){
            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if(getBackground() == Color.white)
                        setBackground(Color.black);
                    else
                        setBackground(Color.white);
                    System.out.println("Clicked");
                }
            });
        }
    }

I want to change not panel_background but frame_background. Would you tell me how to do this? thank you.

hhyuw
  • 1
  • If you know that the current JPanel is held by a JFrame (and not say a JDialog), then do `JFrame currentFrame = (JFrame) SwingUtilities.getWindowAncestor(MyPanel.this);` as per the duplicate answer – Hovercraft Full Of Eels Dec 21 '19 at 02:46
  • I heartily thank you. – hhyuw Dec 21 '19 at 04:32
  • 1
    I couldn't tell by searching alone. Thank for your contribute, now I know about function, getWindowAncestor. It is first activity in asking. Remembering your good deed, I also promise to do good for others. :D – hhyuw Dec 21 '19 at 04:37

0 Answers0