保持顏色模型(以及文本,如果'Red JPanel'是用戶所看到的一部分)將會簡單得多&只需更改適合作爲索引的計數器的現有面板的BG顏色到這些顏色(/文本)組合的數組。
正如@MadProgrammer所提到的,Swing Timer
會更合適,因爲Swing計時器確保在EDT上進行更新。或者兩個定時器。第一個將是一個單次計時器,延遲兩秒鐘。第二個將循環顏色。
這樣的(調整顏色和數字,需要):
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class ColorCycler {
private JComponent ui = null;
Color[] colors = {
Color.RED,
Color.ORANGE,
Color.YELLOW,
Color.GREEN,
Color.CYAN.darker(),
Color.MAGENTA.darker(),
Color.MAGENTA.darker().darker()
};
int counter = 0;
JPanel[] panels = new JPanel[colors.length];
ColorCycler() {
initUI();
}
public void initUI() {
if (ui!=null) return;
ui = new JPanel(new BorderLayout(4,4));
ui.setBorder(new EmptyBorder(4,4,4,4));
ui.setBackground(Color.CYAN);
ui.add(new JLabel(
"Clock", SwingConstants.CENTER), BorderLayout.PAGE_START);
ui.add(new JLabel(
"Progress Bars", SwingConstants.CENTER), BorderLayout.PAGE_END);
JPanel colorPanel = new JPanel(new GridLayout(0, 1));
Border border = new EmptyBorder(new Insets(10, 200, 10, 200));
for (int ii=0; ii<colors.length; ii++) {
JPanel p = new JPanel();
p.setBorder(border);
panels[ii] = p;
colorPanel.add(p);
}
ui.add(colorPanel, BorderLayout.CENTER);
ActionListener colorListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
counter++;
setColors();
}
};
final Timer colorCycleTimer = new Timer(50, colorListener);
ActionListener delayListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
colorCycleTimer.start();
}
};
Timer delayTimer = new Timer(2000, delayListener);
delayTimer.setRepeats(false);
delayTimer.start();
setColors();
}
private void setColors() {
for (int ii=0; ii<colors.length; ii++) {
panels[(counter+ii)%colors.length].setBackground(colors[ii]);
}
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
ColorCycler o = new ColorCycler();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
*「做一些閱讀後,我想了Java的TimerTask類將是一個非常適合這種」 * - 其實, Swing'Timer'會更安全 – MadProgrammer
@MadProgrammer,好的。我想我可以做這件事,因爲我知道Timer類的一些基本知識。但是,您對於如何實際移動JPanels有任何想法嗎?我從未嘗試過移動它們。我現在已經把他們全部坐在一個帶有Box佈局的主面板中。我應該改變它到GridLayout並改變他們在移動它們的列嗎?還是有更簡單的方法來做到這一點,我失蹤了? –
這可能會非常複雜,你可以使用「代理」佈局,演示[here](http://stackoverflow.com/questions/14540080/animations-when-using-gridbag-layout/14541651#14541651),[here ](http://stackoverflow.com/questions/32368190/too-many-jpanels-inside-a-jpanel-with-gridbaglayout/32372506#32372506)和[這裏](http://stackoverflow.com/questions/27463989/java-moving-jlabel-twice-using-timer/27466801#27466801)或者你可以使用[這裏]提到的可用lirabries之一(http://stackoverflow.com/questions/17625475/sliding-any-jpanel/17625831 #17625831) – MadProgrammer