我创建了一个GUI,每次按一个键都会打印出一个列表。或者我认为......但是不是运行我在按键上做的功能,而是GUI一直都是这样做的。
这是我的代码:
package main;
import java.util.ArrayList;
import javax.swing.*;
import java.awt.*;
public class Elements{
static ArrayList<String> composition = new ArrayList<String>();
static Boolean x = true;
static JLabel gLabel = GUI();
public static void main(String[] args){
while(x == true){
gLabel.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("Q"), "water");
gLabel.getActionMap().put("water", water());
gLabel.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("W"), "life");
gLabel.getActionMap().put("life", life());
}
}
public static Action water(){
if(composition.contains("Fire")){
composition.remove("Fire");
composition.add("Steam");
} else if(composition.contains("Cold")){
composition.remove("Cold");
composition.add("Ice");
} else if(composition.contains("Arcane")){
composition.remove("Arcane");
composition.add("Poison");
} else if(composition.contains("Lightning")){
composition.remove("Lightning");
} else {
composition.add("Water");
}
int y = 0;
while(y < 100){
System.out.println("");
y++;
}
System.out.println(composition);
x = true;
return null;
}
public static Action life(){
if(composition.contains("Arcane")){
composition.remove("Arcane");
} else {
composition.add("Life");
}
int y = 0;
while(y < 100){
System.out.println("");
y++;
}
System.out.println(composition);
x = true;
return null;
}
private static JLabel GUI(){
JFrame frame = new JFrame("KeyPress Handler");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("This window listens for key presses and does stuff with them. Isn't that cool?");
label.setPreferredSize(new Dimension(175, 100));
frame.getContentPane().add(label, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
return label;
}
}
不是在按键上运行life()
和water()
,而是一直运行 。
我得到了
的输出 [Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life, Water, Life]
没有触及任何事情!
我不知道为什么会这样,但我认为这与以下情况有关:
gLabel.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("Q"), "water");
和:
gLabel.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("W"), "life");
如果有人能告诉我问题是什么以及如何解决问题,那就太棒了。
谢谢, 海伦
P.S。:我添加了2个新行:
gLabel.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("Enter"), "stop");
gLabel.getActionMap().put("stop", stop());
函数stop()
如下:
public static Action stop(){
x = false;
return null;
据我了解,这是为了添加stop()
到Space
的新键绑定。
此外,进行这些更改后,输出减少到[Water, Life]
。
(另外,修复了while循环)
答案 0 :(得分:0)
while(x = true){
- &gt;不知何故,你必须在while条件中设置x布尔变量false。你的while循环中没有别的东西。
答案 1 :(得分:0)
while ( x = true )
必须更改为while ( x == true )
并且您必须在代码中(在x
是静态变量的情况下,尤其是在您的主要代码中)将x
的值更改为false。
那就是说,我不确定你想要实现什么,我认为你的方向是错误的。