0

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();

    }
}
Ben
  • 1
  • 2
  • maybe you have to repaint the frame? – Tator Jun 15 '15 at 11:06
  • just repaint it buddy – CoderNeji Jun 15 '15 at 11:06
  • Change `MainFrame.setLayout(new BorderLayout());` to `MainFrame.setLayout(new BorderLayout(20,20));` to see the app. work (in a lesser sense than you expected). ;) The label and panel containing the button are both opaque, the only mystery is why they ever showed a blue BG.. But why use AWT at all? It is a GUI toolkit that many Java GUI programmers have never used, and the ones that used to code it have *forgotten* a lot of the differences between AWT and Swing. .. – Andrew Thompson Jun 16 '15 at 11:09
  • .. See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Jun 16 '15 at 11:09

1 Answers1

0

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();

        }
    }
sam_haz
  • 189
  • 3
  • 15
  • @AndrewThompson, I have tested the code and it is changing the background color. Can you please elaborate a bit more. – sam_haz Jun 17 '15 at 06:46