[英]Java executing switch with delay
我正在制作迷宮游戲,並且想要實現非常原始的自動播放選項。 綠色方塊必須到達迷宮或線條末端的粉紅色方塊。
在下面的代碼中,有2個按鈕,“右鍵”每次單擊將變為1個圖塊,而“操作”按鈕每次將變為多個圖塊。
我對“操作”按鈕有疑問。 我希望它一次變一格,然后休息一秒鍾,再變一格。 我將switch
語句與Thread.sleep()
一起使用,但是與其結合使用1個圖塊並進行休息,它結合了休息時間,並在休眠時間結束后立即轉到了最后一個case #
圖塊。
如何逐步進行?
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;
public class Line extends JFrame implements ActionListener {
private JPanel jPBoard;
private JPanel jPControl;
private JButton jBFill[] = new JButton[16];
private JButton jBAct;
private JButton jBRight;
private Border empty;
private int fillCounter;
private int position;
public static void main(String[] args) {
Line frame = new Line();
frame.setSize(700, 100);
frame.createGUI();
frame.setVisible(true);
}
private void createGUI()
{
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new BorderLayout() );
jPBoard = new JPanel();
jPBoard.setPreferredSize(new Dimension(700, 60));
jPBoard.setBackground(Color.GRAY);
window.add(jPBoard, BorderLayout.CENTER);
jPBoard.setLayout(new GridLayout(1, 16));
empty = BorderFactory.createEmptyBorder();
for (fillCounter = 1; fillCounter < 16; fillCounter++) {
jBFill[fillCounter] = new JButton(""+fillCounter);
jBFill[fillCounter].setBorder(empty);
position = 1;
jPBoard.add(jBFill[fillCounter]);
jBFill[fillCounter].setBackground(Color.YELLOW);
if (fillCounter == 15)
{
jBFill[fillCounter].setBackground(Color.PINK);
}
if (position == fillCounter)
{
jBFill[fillCounter].setBackground(Color.GREEN);
}
jPControl = new JPanel();
jPControl.setLayout(new GridLayout(1,2) );
jPControl.setPreferredSize(new Dimension(700, 40));
jPControl.setBackground(Color.RED);
window.add(jPControl, BorderLayout.SOUTH);
jBRight = new JButton("Right");
jPControl.add(jBRight);
jBRight.addActionListener(this);
jBAct = new JButton("Act");
jPControl.add(jBAct);
jBAct.addActionListener(this);
}
}
@Override
public void actionPerformed(ActionEvent grid) {
Object source = grid.getSource();
if (source == jBRight){
jBFill[position+1].setBackground(Color.GREEN);
jBFill[position].setBackground(Color.YELLOW);
position=position+1;
}
if (source == jBAct) {
switch (position) {
case 1: jBRight.doClick();
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
e.printStackTrace();};
case 2: jBRight.doClick();
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
e.printStackTrace();};
case 3: jBRight.doClick();
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
e.printStackTrace();};
case 4: jBRight.doClick();
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
e.printStackTrace();};
case 5: jBRight.doClick();
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
e.printStackTrace();};
case 6: jBRight.doClick();
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
e.printStackTrace();};
case 7: jBRight.doClick();
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
e.printStackTrace();};
case 8: jBRight.doClick();
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
e.printStackTrace();};
case 9: jBRight.doClick();
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
e.printStackTrace();};
default: break;
}
}
}
}
同樣,您的代碼正在做的是
Thread.sleep
,這將阻止該線程並使您的程序凍結,凍結,直到完成所有睡眠為止。 取而代之的是,您將希望使用Swing計時器通過List<JButton>
進行“偽”循環,該List<JButton>
是一個ArrayList,按希望遍歷按鈕的順序保存按鈕。
例如,編譯並運行此命令以查看我的意思:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
@SuppressWarnings("serial")
public class Line2 extends JPanel {
private static final int BTN_COUNT = 256;
private static final Dimension BTN_SZ = new Dimension(40, 40);
private List<JButton> buttonList = new ArrayList<>();
private JButton actButton;
private int index = 0;
public Line2() {
JPanel buttonPanel = new JPanel(new GridLayout(16, 0));
for (int i = 0; i < BTN_COUNT; i++) {
JButton btn = new JButton("" + (i + 1));
btn.setBorder(BorderFactory.createEmptyBorder());
btn.setPreferredSize(BTN_SZ);
buttonPanel.add(btn);
buttonList.add(btn);
}
JButton actionButton = new JButton("Act");
actionButton.addActionListener(e -> doAction());
resetColors();
buttonList.get(0).setBackground(Color.GREEN);
setLayout(new BorderLayout());
add(buttonPanel);
add(actionButton, BorderLayout.PAGE_END);
}
public void resetColors() {
for (int i = 0; i < buttonList.size(); i++) {
Color color = Color.YELLOW;
if (i == BTN_COUNT - 1) {
color = Color.PINK;
}
buttonList.get(i).setBackground(color);
}
}
private void doAction() {
int timerDelay = 100;
index = 0;
new Timer(timerDelay, e -> {
resetColors();
if (index == BTN_COUNT) {
((Timer) e.getSource()).stop();
} else {
// buttonList.get(index).doClick();
buttonList.get(index).setBackground(Color.GREEN);
index++;
}
}).start();
}
private static void createAndShowGui() {
Line2 mainPanel = new Line2();
JFrame frame = new JFrame("Line 2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.