Java AWT setBackground Color by ActionEvent

624 views Asked by At

I would like to change the backgroundcolor of the containing Frame, but it does not seem to work. I added debug- Messages and checked the console output, the switch is working and setting the background with MainFrame.setBackground() Method.

    import java.awt.*;
    import java.awt.event.*;

public class StateWindow {
    private Frame MainFrame;
    private int bgcolor;


    StateWindow() {
        GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        int scrwidth = gd.getDisplayMode().getWidth();
        int scrheight = gd.getDisplayMode().getHeight();
        MainFrame = new Frame("StateWindow");
        MainFrame.setSize(200, 200);
        MainFrame.setLayout(new BorderLayout());
        MainFrame.setLocation((scrwidth-250), (scrheight-450));
        MainFrame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent windowEvent) {
                System.exit(0);
            }
        });
        bgcolor = 1;


        Panel centerPanel = new Panel(new FlowLayout());
        Label titlelabel = new Label("StateWindow", Label.CENTER);
        Button changeBut = new Button("Change State");
        changeBut.setSize(60, 30);
        centerPanel.add(changeBut);

        MainFrame.add(titlelabel, BorderLayout.NORTH);
        MainFrame.add(centerPanel, BorderLayout.CENTER);
        MainFrame.setBackground(Color.BLUE);

        changeBut.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                switch(bgcolor) {
                    case 1: MainFrame.setBackground(Color.GREEN); MainFrame.repaint(); bgcolor = 2; break;
                    case 2: MainFrame.setBackground(Color.ORANGE); MainFrame.repaint(); bgcolor = 3; break;
                    case 3: MainFrame.setBackground(Color.RED);  MainFrame.repaint(); bgcolor = 1; break;
                }
            }
        });

        MainFrame.setVisible(true);
    }

    public static void main(String args[]) {
        StateWindow StateWindow = new StateWindow();

    }
}
1

There are 1 answers

1
sam_haz On

Use a Panel and add that into the Frame and change background of the Panel. Please check the below code. Make sure you add the child components to the Panel.

    import java.awt.*;
    import java.awt.event.*;

    public class StateWindow {
        private Frame MainFrame;
        private int bgcolor;

        StateWindow() {
            GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment()
                    .getDefaultScreenDevice();
            int scrwidth = gd.getDisplayMode().getWidth();
            int scrheight = gd.getDisplayMode().getHeight();
            MainFrame = new Frame("StateWindow");
            MainFrame.setSize(200, 200);
            MainFrame.setLayout(new BorderLayout());
            MainFrame.setLocation((scrwidth - 250), (scrheight - 450));
            MainFrame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent windowEvent) {
                    System.exit(0);
                }
            });
            bgcolor = 1;

            Panel basePanel = new Panel();
            MainFrame.add(basePanel);
            Panel centerPanel = new Panel(new FlowLayout());
            Label titlelabel = new Label("StateWindow", Label.CENTER);
            Button changeBut = new Button("Change State");
            changeBut.setSize(60, 30);
            centerPanel.add(changeBut);

            basePanel.add(titlelabel, BorderLayout.NORTH);
            basePanel.add(centerPanel, BorderLayout.CENTER);
            basePanel.setBackground(Color.BLUE);

            changeBut.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {

                    switch (bgcolor) {
                    case 1:
                        MainFrame.getComponent(0).setBackground(Color.GREEN);
                        MainFrame.repaint();
                        bgcolor = 2;
                        break;
                    case 2:
                        MainFrame.getComponent(0).setBackground(Color.ORANGE);
                        MainFrame.repaint();
                        bgcolor = 3;
                        break;
                    case 3:
                        MainFrame.getComponent(0).setBackground(Color.RED);
                        MainFrame.repaint();
                        bgcolor = 1;
                        break;
                    }
                }
            });

            MainFrame.setVisible(true);
        }

        public static void main(String args[]) {
            StateWindow StateWindow = new StateWindow();

        }
    }