如何阻止我的 GUI 无限期地打印文本? - Java

How to stop my GUI from printing text indefinitely? - Java

我创建了一个 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 循环)

while(x = true){ --> 您必须在 while 条件中以某种方式将 x 布尔变量设置为 false。您的 while 循环中没有其他内容。

while ( x = true ) 必须改为 while while ( x == true )

并且您必须在您的代码中(无论在何处,尤其是在您的主代码中,因为 x 是一个静态变量)将 x 的值更改为 false。

就是说,我不确定您要实现的目标是什么,我认为您走错了方向。