Swing是單線程,您需要將for-loop
從主線程移出。鞦韆也不是線程安全的,所以任何修改UI的狀態應的範圍內完成主(事件調度)線程 - 捕22 - 最簡單的解決方法是使用一個Swing Timer
爲見How to use Swing Timers更多細節
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JLabel("Hello"));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
move(frame, 100, 100);
}
});
}
});
}
public static void move(JFrame frame, int deltaX, int deltaY) {
int xMoveBy = deltaX > 0 ? 4 : -4;
int yMoveBy = deltaY > 0 ? 4 : -4;
int targetX = frame.getX() + deltaX;
int targetY = frame.getY() + deltaY;
Timer timer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int frameX = frame.getX();
int frameY = frame.getY();
if (deltaX > 0) {
frameX = Math.min(targetX, frameX + xMoveBy);
} else {
frameX = Math.max(targetX, frameX - xMoveBy);
}
if (deltaY > 0) {
frameY = Math.min(targetY, frameY + yMoveBy);
} else {
frameY = Math.max(targetY, frameY - yMoveBy);
}
frame.setLocation(frameX, frameY);
if (frameX == targetX && frameY == targetY) {
((Timer)e.getSource()).stop();
}
}
});
timer.start();
}
}
Swing是單線程,您需要將'for-loop'移出主線程。 Swing也不是線程安全的,所以任何對UI狀態的修改應該在主(Event Dispatching)線程的上下文中完成 - catch 22 - 最簡單的解決方案是使用Swing Timer。 – MadProgrammer
可能的[有沒有辦法在Java中動畫整個Jframe,以便它移動?](https://stackoverflow.com/questions/14950694/is-there-a-way-to-animate-an-entire-jframe-in- java-so-it-it) – BaSsGaz