你会如何关闭一个jpanel?

时间:2014-08-11 20:51:21

标签: java swing

我有以下代码:

public static void main(String[] args) throws MalformedURLException {

    // ask player for his username, this is used for login.
    Random rand = new Random();
    int r = rand.nextInt(1000 - 1) + 1;

    Object[] options1 = {"Login", "Random Username",
            "Quit"};

    JPanel panel = new JPanel();
    panel.add(new JLabel("Enter your desired username:"));
    JTextField textField = new JTextField(16);
    panel.add(textField);

    //random usernames
    String words = "username:noob:player:slayer:foreveruser:forevernoob:foreverplayer:lol:wrectlikeaboos:darkalien:gold_in_silver:zeebra:zebra:maddox:canada:canadian:richtard";
    String[] wordsAsArray = words.split(":");
    int index = new Random().nextInt(wordsAsArray.length);
    String randomWord = wordsAsArray[index];
    Random num = new Random();
    int numr = num.nextInt(1000 - 1) + 1;

    int result = JOptionPane.showOptionDialog(null, panel, "Forever: Login",
            JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
            null, options1, null);
    if (result == JOptionPane.YES_OPTION) {
        JOptionPane.showMessageDialog(null, "Logged in as " + textField.getText());
    }
    //randomusername = no
    //quit = cancel
    if (result == JOptionPane.CANCEL_OPTION) {
        System.exit(1)
    }
    if (result == JOptionPane.NO_OPTION) {
        JOptionPane.showMessageDialog(null, "Logged in as " + randomWord + numr);
    }
    //String user = JOptionPane.showInputDialog("Enter your username: ", "User"+r);
    Login client = new Login("hi");
}

public Login(String userName) throws MalformedURLException {
    Launcher launcher = new Launcher();
    launcher.init();
    this.username = userName;
}

基本上,当您运行程序时,会弹出一个询问您用户名的弹出窗口。如果你按退出,它无论如何都会运行游戏而没有任何用户名(不应该发生),如果你按X也会这样做。我怎么做到这样它只是结束程序而不是没有任何用户名运行。

谢谢。

3 个答案:

答案 0 :(得分:1)

在您处理按退出操作的地方,您可以致电System.exit(int)(假设您没有任何清理或保存,而shutdown hook没有这样做。)

答案 1 :(得分:1)

首先,您需要了解JOptionPane告诉您的内容......

来自JavaDocs for JOptionPane.showOptionDialog(Component, Object, String, int, int, Icon, Object[], Object)

  

返回:   一个整数,表示用户选择的选项,如果用户关闭了对话框,则为CLOSED_OPTION

这可能是一个小小的流浪汉,但是,基本上它将做的是返回用户选择的Object[]选项的索引。这意味着正常的意图没有任何意义(CLOSED_OPTION除外)。

因此,您需要将NO_OPTION用于CANCEL_OPTION0用于Login和{{1},而不是使用1Random Username } {} {}为2,例如......

Quit

然后,如果选择了有效的回复,您也应该只创建if (result != 2 && result != JOptionPane.CLOSED_OPTION) { if (result == 0) { JOptionPane.showMessageDialog(null, "Logged in as " + textField.getText()); } else { JOptionPane.showMessageDialog(null, "Logged in as " + randomWord + numr); } //String user = JOptionPane.showInputDialog("Enter your username: ", "User"+r); Login client = new Login("hi"); }

我老了,我相信程序的一个入口点和一个出口点,用Login乱丢你的代码只会使将来调试变得更加困难 - 恕我直言

答案 2 :(得分:0)

您需要检查一个可能的结果:JOptionPane.CLOSED_OPTION。这是因为当用户点击X或以任何其他方式关闭它时。

在问题中,你说它对于CANCEL_OPTION也没有用,但我看到你编辑了你的问题并修复了(你添加了System.exit(1)

另外,我建议用if / else语句编写它,这样你只需要担心你关心的行为:

String username;
if (result == JOptionPane.YES_OPTION) {
    username = textField.getText();
} else if (result == JOptionPane.NO_OPTION) {
    username = randomWord + numr;
} else { //Or add if's for CANCEL and CLOSED if you want to do something else
    System.exit(0);
}
JOptionPane.showMessageDialog(null, "Logged in as " + username);
Login client = new Login(username);