Swing计时器的基本功能

use*_*456 -1 java swing timer awt jframe

我是java平面设计的新手,如果可能的话,我希望你帮我一个简单的例子来帮助我理解JFrames,Timers,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
}
Run Code Online (Sandbox Code Playgroud)

首先,我在setBackgroud()方法之间使用了线程对象的sleep方法,但它不起作用,因为它只显示最后一次更改.你如何在这里使用Timer对象?

nIc*_*cOw 6

首先,每当你需要改变所述东西的颜色时,总是将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();
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)