尝试制作类似的东西
https://www.youtube.com/watch?v=mPU6mnM2i-k
按下“停止”按钮可以选择不同的面板(我稍后会在其中放置字母并可能将它们更改为JButtons)。我通过添加一个计算它被按下次数的变量来改变它的动作。不是理想的事情,但有效。我想清理一下代码,但我不知道该怎么做。不确定我是否应该将SwingWorker放在某处并尝试使用一个Timer而不是三个。所有面板都保存在JPanels阵列中。它应该使代码更清晰,但显然它完全相反。该计划有效,但看起来很糟糕。请帮忙。
private final JPanel[] firstLevelPanels = new JPanel[2];
private final JPanel[] secondLevelPanels1 = new JPanel[3];
private final JPanel[] secondLevelPanels2 = new JPanel[3];
private final JPanel[] rowPanels1 = new JPanel[7];
private final JPanel[] rowPanels2 = new JPanel[7];
private final JPanel[] rowPanels3 = new JPanel[7];
private final JPanel[] rowPanels4 = new JPanel[7];
private final JPanel[] rowPanels5 = new JPanel[7];
private final JPanel[] rowPanels6 = new JPanel[7];
private final JPanel[][] allPanels = new JPanel[9][];
private int currentlyActivePanel = 0;
private int activeAncestorPanel = 0;
private int stopCounter = 0;
private int counter = 0;
class FirstLevelAction implements ActionListener{
JPanel[] panel;
@Override
public void actionPerformed(ActionEvent e) {
changeBackground(allPanels[activeAncestorPanel], counter);
counter = (counter+1)%2;
}
public FirstLevelAction(JPanel[][] panels, int panelNumber) {
this.panel = panels[panelNumber];
}
}
class SecondLevelAction implements java.awt.event.ActionListener{
JPanel[] tempPanel;
@Override
public void actionPerformed(ActionEvent e) {
changeBackground(allPanels[activeAncestorPanel], counter);
counter = (counter+1)%3;
}
public SecondLevelAction(JPanel[][] panels, int panelNumber) {
this.tempPanel = panels[panelNumber];
}
}
class ThirdLevelAction implements java.awt.event.ActionListener{
JPanel[] tempPanel;
@Override
public void actionPerformed(ActionEvent e) {
changeBackground(allPanels[activeAncestorPanel], counter);
counter = (counter+1)%7;
}
public ThirdLevelAction(JPanel[][] panels, int panelNumber) {
this.tempPanel = panels[panelNumber];
}
}
private void changeBackground(JPanel[] panels, int panelIndex){
for(JPanel panel : panels){
panel.setBackground(Color.GRAY);
}
panels[panelIndex].setBackground(Color.YELLOW);
currentlyActivePanel = panelIndex;
}
private Timer timer1 = new Timer(500, new FirstLevelAction(allPanels, activeAncestorPanel));
private Timer timer2;
private Timer timer3;
private void stopButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
stopCounter++;
counter = 0;
if(stopCounter == 1){
timer1.stop();
activeAncestorPanel = currentlyActivePanel + 1;
allPanels[0][currentlyActivePanel].setBackground(Color.GRAY);
currentlyActivePanel = 0;
timer2 = new Timer(500, new SecondLevelAction(allPanels, activeAncestorPanel));
timer2.start();
}else if(stopCounter == 2){
timer2.stop();
allPanels[activeAncestorPanel][currentlyActivePanel].setBackground(Color.GRAY);
activeAncestorPanel = activeAncestorPanel + activeAncestorPanel*2 + currentlyActivePanel;
currentlyActivePanel = 0;
timer3 = new Timer(500, new ThirdLevelAction(allPanels, activeAncestorPanel));
timer3.start();
}else{
timer3.stop();
stopCounter = 0;
allPanels[activeAncestorPanel][currentlyActivePanel].setBackground(Color.GRAY);
activeAncestorPanel = 0;
timer1 = new Timer(500, new FirstLevelAction(allPanels, activeAncestorPanel));
timer1.start();
}
}
答案 0 :(得分:0)
您可以通过使用大小写切换和状态列表来使用状态机结构。请查看oracle文档以获取更多信息:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
基本上,您希望在初始化时将每个活动面板设置为相应的数字,如下所示:
private static final int activePanelState = activePanel1;//whatever is default
private static final int activePanel1 = 1;
private static final int activePanel2 = 2;
private static final int activePanel3 = 3;
然后在这些状态之间切换并在每种情况下调用正确的jPanel函数以及设置正确的转换:
例如。
switch(activePanelState){
case activePanel1:
jPanel1.setBackground(Color.YELLOW);
jPanel2.setBackground(Color.GRAY);
activePanelState = activePanel2;
break;
等等。