java 如何每秒移动jlabel?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13226164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to move jlabel every second?
提问by mopr mopr
i try to move it to the right(x++) every seconds
我尝试每秒钟将它向右移动(x++)
i try to move it with thread..
我试着用线移动它..
- how to do it? (and can see it move every second)
- there are another way to do it without use thread?
- what layout manager that i should use?
- 怎么做?(并且可以看到它每秒移动一次)
- 还有另一种方法可以在不使用线程的情况下做到这一点吗?
- 我应该使用什么布局管理器?
heres i try..
继承人我尝试..
public class help {
JFrame frame = new JFrame();
JLabel label = new JLabel("target");
public help() {
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(800,600);
frame.setLayout(new GridLayout());
frame.add(label);
label.setPreferredSize(new Dimension(100,100));
label.setLocation(400, 300);
frame.getContentPane().validate();
frame.repaint();
frame.setVisible(true);
mysterious();
}
void mysterious(){
////////////////////////////////
// part of edit responding David kroukamp
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try{
for (int z=0; z<10; z++){
label.setLocation((label.getLocationOnScreen().x+10), label.getLocationOnScreen().y);
Thread.sleep(1000);
}
}catch(Exception ae){
}
}
});
t.start();
//////////////////////////////
}
public static void main(String[]args){
new help();
}
}
thanks a lot for any kind of help
非常感谢任何形式的帮助
回答by David Kroukamp
- Class names begin with capital letters i.e
Help
- Swing components should be created and modified on
Event Dispatch Thread
A new
Thread
is created like this:Thread t = new Thread(new Runnable() { @Override public void run() { //work here } }); t.start();//start thread
- 类名以大写字母开头,即
Help
- 应在以下位置创建和修改 Swing 组件
Event Dispatch Thread
一个新
Thread
的创建是这样的:Thread t = new Thread(new Runnable() { @Override public void run() { //work here } }); t.start();//start thread
however I'd suggest a Swing Timer
as it runs on EDT
:
但是我建议在Timer
运行时使用 Swing EDT
:
EDIT:
编辑:
As per your questions I suggest using a Timer
the creating thread point was for general knowledge.
根据您的问题,我建议使用Timer
创建线程点作为一般知识。
The probelm is the Thread is not run on EDT Thread of your swing GUI where as a Timer
does:
问题是线程未在您的摆动 GUI 的 EDT 线程上运行,而 aTimer
是:
int delay = 1000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
int count=0;
public void actionPerformed(ActionEvent evt) {
if(count==10) {//we did the task 10 times
((Timer)evt.getSource()).stop();
}
label.setLocation((label.getLocationOnScreen().x+10), label.getLocationOnScreen().y);
System.out.println(SwingUtilities.isEventDispatchThread());
count++;
}
};
new Timer(delay, taskPerformer).start();
Reference:
参考:
回答by vels4j
Here is an Swing
example of a simple puzzle game.
这是Swing
一个简单的益智游戏的例子。
When you press Pause
button the title will get animate until you release the pause. Similarly you can use it for JLabel
. Source code is also attached.
当您按下Pause
按钮时,标题将变为动画,直到您松开暂停。同样,您可以将其用于JLabel
. 还附上了源代码。
Hope that can help you much.
希望对你有很大帮助。
回答by Scott Loftin
If you put that part of the constructor in a thread, then you can call thread.sleep(1000);
(1000 milliseconds for a 1 second delay) and then refresh, which should move the target across the screen.
如果你把构造函数的那部分放在一个线程中,那么你可以调用thread.sleep(1000);
(1000 毫秒,延迟 1 秒)然后刷新,这应该在屏幕上移动目标。