代码之家  ›  专栏  ›  技术社区  ›  Jennifer Oclaray Hast

我试图在每次按下按钮时更改JPanel的背景色

  •  0
  • Jennifer Oclaray Hast  · 技术社区  · 7 年前

    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class TimeFrame extends JFrame {
            private static final int FRAME_WIDTH = 250;
            private static final int FRAME_HEIGHT = 200;
            private JButton CurrentTime;
            private JLabel CurrentTimeLabel;
            private JTextField CurrentTimeField;
            private int click;
            private JTextField clicktest;
    
            public TimeFrame() {
                CurrentTimeLabel = new JLabel("The Current Time is: ");
    
                createTextField();
                createButton();
                createPanel();
    
                setSize(FRAME_WIDTH, FRAME_HEIGHT);
                setSize(250, 200);
                initialize();
    
    
            }
            private void initialize()
            {
            click = 0;
            }
    
            private void createTextField()
            {
               final int FIELD_WIDTH = 10;
               CurrentTimeField = new JTextField(FIELD_WIDTH);
               Date now = new Date();
               DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
               CurrentTimeField.setText("" + dateFormat.format(now));
    
               clicktest = new JTextField(FIELD_WIDTH);
               clicktest.setText(" " + click);
    
            }
            private void createButton()
            {
                CurrentTime = new JButton("Display Current Time");
    
                ActionListener listener = new AddCurrentTimeListener();
                CurrentTime.addActionListener(listener);
    
            }
            private void createPanel()
            {
               JPanel panel = new JPanel();
               //panel.addActionListener(backgroundListener);
               panel.add(CurrentTimeLabel);
               panel.add(CurrentTimeField);
               panel.add(CurrentTime);
               panel.add(clicktest);
               //int click = 0;
    
               if (click == 0){
                    panel.setBackground(Color.ORANGE);
                } 
               else if (click == 1) {
                    panel.setBackground(Color.YELLOW);
                } 
               else if (click == 2) {
                    panel.setBackground(Color.BLUE);
                } 
               else if (click == 3) {
                    panel.setBackground(Color.GREEN);
                } 
               else if (click == 4) {
                    panel.setBackground(Color.PINK);
                }
    
               add(panel);
    
            }
    
            class AddCurrentTimeListener implements ActionListener
            {
               public void actionPerformed(ActionEvent event){
    
                   new java.util.Date();
                   Date now = new Date();
                   DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
                   CurrentTimeField.setText("" + dateFormat.format(now));getClass();                                   
                   if (click < 4){
                       click++;
                   } else  {
                       click = click - 4;}
    
                   clicktest.setText("" + click);getClass();
    
                   /**
                   if (click == 0){
                        panel.setBackground(Color.ORANGE);
                    } 
                   else if (click == 1) {
                        panel.setBackground(Color.YELLOW);
                    } 
                   else if (click == 2) {
                        panel.setBackground(Color.BLUE);
                    } 
                   else if (click == 3) {
                        panel.setBackground(Color.GREEN);
                    } 
                   else if (click == 4) {
                        panel.setBackground(Color.PINK);
                    }
                   */
    
                   repaint();
                   }
    
    
                }
    
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   MadProgrammer    7 年前

    通过添加 JPanel 对于帧,您正在有效地阻止帧 contentPane ActionListener ,您正在尝试设置帧本身的背景色。

    现在,这不起作用,因为框架是由许多重叠的组件组成的。

    The layers of a JFrame

    内容窗格

    相反,您应该更改 的背景。

    好的,但这让我们回到第一个问题(即 面板 监听器 内容窗格 ,你选择哪一个取决于你自己

    对于我过于简化的示例,我选择了简单地使用框架的 内容窗格

    import java.awt.Color;
    import java.awt.EventQueue;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    
    public class TimeFrame extends JFrame {
    
        public static void main(String args[]) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new TimeFrame().setVisible(true);
                }
            });
        }
    
        private JButton currentTime;
        private JLabel currentTimeLabel;
        private JTextField currentTimeField;
        private int click;
        private JTextField clicktest;
    
        public TimeFrame() {
            currentTimeLabel = new JLabel("The Current Time is: ");
    
            createTextField();
            createButton();
            createPanel();
    
            pack();
            setLocationRelativeTo(null);
            initialize();
    
        }
    
        private void initialize() {
            click = 0;
        }
    
        private void createTextField() {
            final int FIELD_WIDTH = 10;
            currentTimeField = new JTextField(FIELD_WIDTH);
            Date now = new Date();
            DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
            currentTimeField.setText("" + dateFormat.format(now));
    
            clicktest = new JTextField(FIELD_WIDTH);
            clicktest.setText(" " + click);
    
        }
    
        private void createButton() {
            currentTime = new JButton("Display Current Time");
    
            ActionListener listener = new AddCurrentTimeListener();
            currentTime.addActionListener(listener);
    
        }
    
        private void createPanel() {
            setLayout(new FlowLayout());
            //panel.addActionListener(backgroundListener);
            add(currentTimeLabel);
            add(currentTimeField);
            add(currentTime);
            add(clicktest);
        }
    
        class AddCurrentTimeListener implements ActionListener {
    
            public void actionPerformed(ActionEvent event) {
    
                new java.util.Date();
                Date now = new Date();
                DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
                currentTimeField.setText("" + dateFormat.format(now));
                getClass();
                if (click < 4) {
                    click++;
                } else {
                    click = click - 4;
                }
    
                clicktest.setText("" + click);
    
                if (click == 0) {
                    getContentPane().setBackground(Color.ORANGE);
                } else if (click == 1) {
                    getContentPane().setBackground(Color.YELLOW);
                } else if (click == 2) {
                    getContentPane().setBackground(Color.BLUE);
                } else if (click == 3) {
                    getContentPane().setBackground(Color.GREEN);
                } else if (click == 4) {
                    getContentPane().setBackground(Color.PINK);
                }
                repaint();
            }
    
        }
    
    }
    

    免责声明:

    就我个人而言,我会避免直接从 JFrame getContentPane )

    就我个人而言,我会从一个 面板 然后把它添加到我想要的每个容器中