0

我试图缩小范围,但我的程序一直在代码中冻结:

try {
    MainMenu.start();
} catch (IOException e1) {
    System.out.print(e1.getCause());
}

我在 JTextField 中键入了几行随机行,按回车键,然后程序冻结。

如果您需要,这是整个代码。

package Main;

import java.awt.BorderLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class window extends JFrame implements KeyListener {
/**
 * 
 */
private static final long serialVersionUID = 7092006413113558324L;
private static int NewSize;
public static String MainInput;
public static JTextField input = new JTextField(10);
public static JTextArea output = new JTextArea(main.Winx, NewSize);

public window() {
    super("Satine");
    NewSize = main.Winy - 20;
    setLayout(new BorderLayout());
    output.setToolTipText("");
    add(input, BorderLayout.PAGE_END);
    add(output, BorderLayout.CENTER);
    input.addKeyListener(this);
    input.requestFocus();
}

@Override
public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();
    if (key == KeyEvent.VK_ENTER) {
        String MainInput = input.getText();
        try {
            MainMenu.start();
        } catch (IOException e1) {
            System.out.print(e1.getCause());
        }
        output.append(MainInput + "\n");
        input.setText("");
    }
}

@Override
public void keyReleased(KeyEvent e) {
}

@Override
public void keyTyped(KeyEvent e) {
}
}

感谢您抽出宝贵时间对此进行评估。

我也忘了这里是 MainMenu.java

package Main;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.util.StringTokenizer;

public class MainMenu {
public static String InputText = window.MainInput;
public static String CompleteInput, noCaseCompleteInput, A1, A2, A3, A4,
        A5; // Command
// Inputs
public static String Advance = "Y";
public static String Advance2 = "Y";
public String Command;
public static String CorrectP;
public static StringTokenizer st;

public static void start() throws IOException{
    window.output.append("Satine. /InDev-01/ " + "\n");
    do {
        CompleteInput = InputText;
        noCaseCompleteInput = CompleteInput.toLowerCase();
        st = new StringTokenizer(noCaseCompleteInput);
        switch (st.nextToken()) {
        case "help":
            helpData.HelpDefine();
            continue;
        case "end":
            System.exit(0);
        case "newworld":
            newWorldRun.NewWorld();
            break;
        case "save":
            Save.SaveFull();
            continue;
        case "load":
            Save.FileName = st.nextToken();
            Read.ReadFull();
            continue;
        case "profile":
            profileData.profile();
            break;
        case "dp":
            debugPrint.print();
            continue;
        case "move":
            Movement.MoveCommand();
            continue;
        case "equip":
            equip.equip();
            break;
        case "attack":
            MonsterEvents.attackStart();
            break;
        default:
            window.output.append("Please Enter A Valid Command" + "\n");
            System.out.println("Please Enter A Valid Command");
        }
        window.input.setText("");
        } while (Advance.equals("Y"));
    }
   }
4

1 回答 1

1

String MainInput = input.getText();阴影对象实例变量MainInput,因此不用于MainMenu.start(). 此外,如果MainMenu.Advance未设置为 以外的值Y,它将无限循环。

于 2012-04-30T22:54:20.230 回答