Swing计时器的基本功能 [英] Swing timers basic functionality

查看:161
本文介绍了Swing计时器的基本功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是java平面设计的新手,如果可能的话,我希望你帮我一个简单的例子来帮助我理解JFrame,Timers,SwingControllers和所有这些东西的基本功能。你将如何实现以下情况:

I'm new at java graphic design, and I would like you, if possible, to help me with an easy example to help me to get to understand the basic functionality of JFrames, Timers, SwingControllers, and all this stuff. How would you implement the following case:

我们有一个内置JPanel的JFrame。
当执行开始时,JPanel是白色的,但我们希望它每两秒更改一次颜色:

We have a JFrame with a JPanel inside. When the execution starts, the JPanel is white, but we want it to change it's colour every two seconds:

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对象?

At first, I used the sleep method of a thread object between the setBackgroud() methods but it doesn't work, as it only shows the last change. How would you use here a Timer object?

推荐答案

首先,每当你需要改变所说的东西的颜色时,对于所说的东西,总是将不透明属性设置为true。就像你的情况一样,它是 JPanel 所以首先你必须使用 panelObject.setOpaque(true),对于某些外观和感觉调用此方法是必须使背景颜色更改生效。

First of all, whenever you need to change the colour of the said thingy, always set Opaque property to true for the said thingy. Like in your case it's the JPanel so first of all you must use panelObject.setOpaque(true), for some Look And Feels calling this method is a must for background colour changes to take effect.

尝试此代码示例,关于其余部分:

Do try this code example, regarding the rest :

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

这篇关于Swing计时器的基本功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆