Swing计时器的基本功能

发布于 2021-01-29 19:56:59

我是Java图形设计的新手,如果可能的话,我希望您能通过一个简单的示例帮助我,以帮助我了解JFrames,Timer,SwingControllers以及所有这些东西的基本功能。您将如何实现以下情况:

我们有一个内部带有JPanel的JFrame。执行开始时,JPanel为白色,但我们希望它每两秒钟更改一次其颜色:

public class MiJFrame extends javax.swing.JFrame {

    public MiJFrame() {
        initComponents();
    }


    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MiJFrame().setVisible(true);
                jPanel1.setBackground(Color.yellow);
                jPanel1.setBackground(Color.RED);
            }
        });
    }

    // Variables declaration - do not modify
    private static javax.swing.JPanel jPanel1;
    // End of variables declaration
}

最初,我在setBackgroud()方法之间使用了线程对象的sleep方法,但是它不起作用,因为它仅显示最后的更改。在这里如何使用Timer对象?

关注者
0
被浏览
126
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    首先,每当需要更改表示内容的颜色时,请始终将Opaque属性设置为true。就像您的情况一样,JPanel首先必须使用panelObject.setOpaque(true),对于某些Look And Feel调用此方法是必须执行背景颜色更改的。

    关于其余部分,请尝试下面的代码示例:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    /*
     * @see
     * http://stackoverflow.com/q/11036830/1057230
     */
    
    public class ColourTimer
    {
        private JPanel contentPane;
        private Timer timer;
        private int counter;
        private Color[] colours = {
                                    Color.RED,
                                    Color.WHITE,
                                    Color.BLUE,
                                    Color.DARK_GRAY,
                                    Color.YELLOW,
                                    Color.LIGHT_GRAY,
                                    Color.BLACK,
                                    Color.MAGENTA,
                                    Color.PINK,
                                    Color.CYAN
                                  };
    
        private ActionListener timerAction = new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                if (counter == (colours.length - 1))
                    counter = 0;
                contentPane.setBackground(colours[counter++]);
            }    
        };
    
        public ColourTimer()
        {
            counter = 0;
        }
    
        private void displayGUI()
        {
            JFrame frame = new JFrame("Colour Timer");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    
            contentPane = new JPanel();
            contentPane.setOpaque(true);
    
            final JButton button = new JButton("STOP");
            button.addActionListener(new ActionListener()
            {
                @Override
                public void actionPerformed(ActionEvent ae)
                {
                    if (timer.isRunning())
                    {
                        button.setText("START");
                        timer.stop();
                    }
                    else
                    {
                        button.setText("STOP");
                        timer.start();
                    }
                }
            });
    
            frame.getContentPane().add(contentPane, BorderLayout.CENTER);
            frame.getContentPane().add(button, BorderLayout.PAGE_END);
            frame.setSize(300, 200);
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
            timer = new Timer(2000, timerAction);
            timer.start();
        }
    
        public static void main(String... args)
        {
            EventQueue.invokeLater(new Runnable()
            {
                @Override
                public void run()
                {
                    new ColourTimer().displayGUI();
                }
            });
        }
    }
    


推荐阅读
知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看