我每次按下按钮时都试图改变JPanel的背景颜色

时间:2017-09-07 01:38:24

标签: java swing jframe jbutton actionlistener

我已经查看了几个类似的问题并且无法完成这项工作。我正在显示时间并添加了一个计数到4并重新开始的计数器,但我只能设置它来设置开始的背景颜色。我在这里错过了什么?我试过把我的if语句放在几个地方。除颜色变化之外的所有东西都运转正常。

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

答案 0 :(得分:1)

通过在框架中添加JPanel,您可以有效地阻止框架contentPane,这本身并不是特定问题,除了在ActionListener中,您正试图设置框架本身的背景颜色。

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

The layers of a JFrame

对框架背景的任何更改都不会显示,因为它隐藏在contentPane后面。

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

好的,但这会让我们回到第一个问题(JPanel)。现在,您可以将JPanel设置为实例字段,并在ActionListener中更改它的背景颜色,这样就行了,或者您可以摆脱它并简单地更改框架&#39;您选择的contentPane取决于您

对于我过于简化的示例,我选择简单地使用框架contentPane,因为它展示了您需要了解框架的重要概念...

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

就个人而言,我从JPanel开始,然后将其添加到我想要的每个容器中