簡體   English   中英

Java AWT setBackground顏色(通過ActionEvent)

[英]Java AWT setBackground Color by 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();

    }
}

使用面板並將其添加到框架中並更改面板的背景。 請檢查以下代碼。 確保將子組件添加到面板中。

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

        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM