振荡定时器基本functionallity [英] Swing timers basic functionallity
问题描述
我在Java的图形设计新的,我想你,如果可能的话,帮我一个简单的例子,以帮助我去了解JFrames,定时器,SwingControllers基本functionallity,而这一切的东西。你将如何实现以下情况:
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 functionallity 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之间的线程对象的睡眠方法()方法,但它不工作,因为它仅显示最近change.How你会在这里使用一个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(真)
,对于一些的外观和感觉
打来的这个方法是必须的背景颜色更改生效。
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 Feel
s calling this method is a must for background colour changes to take effect.
不要尝试这个code例如,对于其余的:
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();
}
});
}
}
这篇关于振荡定时器基本functionallity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!