如何解决使用计时器一个接一个地显示 JButton 数组 [JAVA SWING]
我正在制作一个颜色游戏,当我点击开始时,会显示一系列颜色。现在我必须点击我的 4 个颜色按钮并匹配显示的链。 如果我赢了,链条会增加一种颜色(升级),如果我输了,链条就会失去一种颜色(等级降低)。
我现在的问题:
- 如果我重叠显示的颜色以匹配,我只会看到数组的第一个索引。我需要让它像每 2 秒一样显示 JButton [] 的所有索引。我用计时器试过了,但它仍然只显示第一个。
- 如果我升级,我可以为需要匹配的数组添加颜色,但是如果我升级,我无法删除最后添加的颜色。我知道无法从数组中删除某些内容,但是否有解决方法。现在,如果玩家不符合计划,我会让他完全失败。
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.*;
public class Farbgedaechtnisspiel {
private static int startLevel = 1;
private static int gameLevel = 3;
private static List<Integer> gameArray = new ArrayList<>();
private static List<Integer> userArray = new ArrayList<>();
private static int save = 0;
private static int count = 0;
private static JButton [] b;
private static final Color red = new Color(255,0);
private static final Color green = new Color(0,255,0);
private static final Color blue = new Color(0,255);
private static final Color yellow = new Color(255,0);
private static JFrame frame = new JFrame("Farbgedächtnisspiel");
private static JLabel levelTxt;
public static void main(String[] args) {
// create mainframe
new Farbgedaechtnisspiel();
}
public Farbgedaechtnisspiel () {
createFrame(frame);
levelTxt = new JLabel("Level: " + startLevel);
frame.add(levelTxt);
levelTxt.setFont(new Font("Times New Roman",Font.ITALIC,40));
// create 4 color buttons + start/exit
b = new JButton[7];
for(int i = 0;i<b.length;i++){
b[i] = new JButton();
frame.add(b[i]);
}
// create button layout
createButtons();
// button listeners
b[4].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// start Game -> show taskArray
levelTxt.setBounds(550,200,200);
startGame(gameLevel);
}});
b[5].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
b[0].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
userArray.add(1);
save++;
}
}
);
b[1].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
userArray.add(2);
save++;
}
});
b[2].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
userArray.add(3);
save++;
}
});
b[3].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
userArray.add(4);
save++;
}
});
b[6].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
boolean result = compareArrays(userArray,gameArray);
if (result){
gameLeveL++;
save = 0;
startLeveL++;
levelTxt.setText("Level: " + startLevel);
} else {
JOptionPane.showMessageDialog(frame,"Sie haben verloren");
new Farbgedaechtnisspiel();
System.exit(1);
}
startGame(gameLevel);
}
});
// make frame visible
frame.setVisible(true);
}
public static void startGame(int gameLevel){
int x = 100;
JButton [] tmp = new JButton[gameLevel];
for(int i = 0;i<tmp.length;i++) {
tmp[i] = new JButton();
tmp[i].setBackground(randomColor());
tmp[i].setopaque(true);
tmp[i].setBorderPainted(false);
frame.add(tmp[i]);
if (tmp[i].getBackground() == red) {
gameArray.add(1);
} else if (tmp[i].getBackground() == blue) {
gameArray.add(2);
} else if (tmp[i].getBackground() == green) {
gameArray.add(3);
} else if (tmp[i].getBackground() == yellow) {
gameArray.add(4);
}
}
for (int i = 0; i < tmp.length; i++) {
tmp[i].setBounds(x,50,100,100);
x+=100;
}
// for (int i = 0; i < tmp.length; i++) {
// tmp[i].setBounds(450,300,299);
// x += 100;
// }
}
public static void createButtons() {
int x = 0;
for (int i = 0; i < b.length; i++) {
b[i].setBounds(x,200);
x += 200;
}
b[0].setBackground(red);
b[0].setopaque(true);
b[0].setBorderPainted(false);
b[1].setBackground(blue);
b[1].setopaque(true);
b[1].setBorderPainted(false);
b[2].setBackground(green);
b[2].setopaque(true);
b[2].setBorderPainted(false);
b[3].setBackground(yellow);
b[3].setopaque(true);
b[3].setBorderPainted(false);
b[4].setBounds(150,600,200);
b[4].setText("Start");
b[5].setBounds(450,200);
b[5].setText("Beenden");
b[6].setBounds(750,200);
b[6].setText("Continue");
b[0].setBounds(200,400,200);
b[1].setBounds(400,200);
b[2].setBounds(600,200);
b[3].setBounds(800,200);
}
public static boolean compareArrays(List<Integer> userArray,List<Integer> gameArray){
return userArray.equals(gameArray);
}
public static void createFrame(JFrame frame){
frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1200,800);
frame.setLocationRelativeto(null);
frame.setLayout(null);
frame.setResizable(false);
}
public static Color randomColor () {
Random rand = new Random();
int randomNum = rand.nextInt((4 - 1) + 1) + 1;
return switch (randomNum) {
case 1 -> red;
case 2 -> green;
case 3 -> blue;
case 4 -> yellow;
default -> null;
};
}
}```
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。