我的java程序一直在冻结

时间:2012-04-30 22:09:32

标签: java swing freeze

我试图将其缩小一点,但我的程序仍然冻结了代码:

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"));
    }
   }

1 个答案:

答案 0 :(得分:1)

String MainInput = input.getText();遮蔽对象实例变量MainInput,因此未在MainMenu.start()中使用。此外,如果MainMenu.Advance未设置为Y以外的值,则会无限循环。