Java AWT setBackground Color by ActionEvent

时间:2015-06-15 11:05:06

标签: java awt actionevent

我想更改包含Frame的backgroundcolor,但它似乎不起作用。我添加了debug- Messages并检查了控制台输出,交换机正在工作并设置后台 MainFrame.setBackground()方法。

    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 个答案:

答案 0 :(得分:0)

使用面板并将其添加到框架中并更改面板的背景。请检查以下代码。确保将子组件添加到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();

        }
    }