Java 应用程序未显示GUI和WIN';不要退出爪哇

Java 应用程序未显示GUI和WIN';不要退出爪哇,java,Java,所以我正在用Java创建一个游戏,我遇到了一个小问题。无论何时运行应用程序,GUI都不会显示在屏幕上,关闭应用程序的唯一方法是使用Windows任务管理器。在应用程序中,您选择一个用户名并单击enter按钮,该按钮将创建一个新窗口,在其中玩游戏。但是,单击按钮后,将不会加载任何GUI,并且无法关闭应用程序 基本上,当您单击按钮时,会调用一个方法来处理当前窗口并启动游戏窗口。该方法的代码如下所示: public void login(String name) { String[] args

所以我正在用Java创建一个游戏,我遇到了一个小问题。无论何时运行应用程序,GUI都不会显示在屏幕上,关闭应用程序的唯一方法是使用Windows任务管理器。在应用程序中,您选择一个用户名并单击enter按钮,该按钮将创建一个新窗口,在其中玩游戏。但是,单击按钮后,将不会加载任何GUI,并且无法关闭应用程序

基本上,当您单击按钮时,会调用一个方法来处理当前窗口并启动游戏窗口。该方法的代码如下所示:

public void login(String name) {
    String[] args={};
    dispose();
    SingleplayerGUI.main(args);
}
下面是SingleplayerGUI类的代码:

public SingleplayerGUI(String userName, Singleplayer sp) {
    this.userName = userName;
    setResizable(false);
    setTitle("Console Clash Singleplayer");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(880, 550);
    setLocationRelativeTo(null);

    System.setIn(inPipe); 
    try {
        System.setOut(new PrintStream(new PipedOutputStream(outPipe), true));
        inWriter = new PrintWriter(new PipedOutputStream(inPipe), true); 
    } catch (IOException e1) {
        e1.printStackTrace();
    } 

    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);

    GridBagLayout gbl_contentPane = new GridBagLayout();
    gbl_contentPane.columnWidths = new int[] { 28, 815, 30, 7 }; // SUM = 880
    gbl_contentPane.rowHeights = new int[] { 25, 485, 40 }; // SUM = 550
    contentPane.setLayout(gbl_contentPane);

    history = new JTextPane();
    history.setEditable(false);
    JScrollPane scroll = new JScrollPane(history);
    caret = (DefaultCaret) history.getCaret();
    caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
    GridBagConstraints scrollConstraints = new GridBagConstraints();
    scrollConstraints.insets = new Insets(0, 0, 5, 5);
    scrollConstraints.fill = GridBagConstraints.BOTH;
    scrollConstraints.gridx = 0;
    scrollConstraints.gridy = 0;
    scrollConstraints.gridwidth = 2;
    scrollConstraints.gridheight = 2;
    scrollConstraints.weightx = 1;
    scrollConstraints.weighty = 1;
    scrollConstraints.insets = new Insets(0, 0, 5, -64);
    contentPane.add(scroll, scrollConstraints);

    txtMessage = new JTextField();
    txtMessage.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                Color color = new Color(92, 219, 86);
                String text = txtMessage.getText();
                inWriter.println(text); 
                console(txtMessage.getText(), color);
                command = txtMessage.getText();
            }
        }
    });
    GridBagConstraints gbc_txtMessage = new GridBagConstraints();
    gbc_txtMessage.insets = new Insets(0, 0, 0, 25);
    gbc_txtMessage.fill = GridBagConstraints.HORIZONTAL;
    gbc_txtMessage.gridx = 0;
    gbc_txtMessage.gridy = 2;
    gbc_txtMessage.gridwidth = 2;
    gbc_txtMessage.weightx = 1;
    gbc_txtMessage.weighty = 0;
    txtMessage.setColumns(5);
    contentPane.add(txtMessage, gbc_txtMessage);

    chat = new JTextPane();
    chat.setEditable(false);
    JScrollPane chatscroll = new JScrollPane(chat);
    caret = (DefaultCaret) chat.getCaret();
    caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
    GridBagConstraints chatscrollConstraints = new GridBagConstraints();
    chatscrollConstraints.insets = new Insets(0, 0, 5, 5);
    chatscrollConstraints.fill = GridBagConstraints.BOTH;
    chatscrollConstraints.gridx = 0;
    chatscrollConstraints.gridy = 0;
    chatscrollConstraints.gridwidth = 2;
    chatscrollConstraints.gridheight = 2;
    chatscrollConstraints.weightx = 1;
    chatscrollConstraints.weighty = 1;
    chatscrollConstraints.insets = new Insets(150, 600, 5, -330);
    contentPane.add(chatscroll, chatscrollConstraints);

    chatMessage = new JTextField();
    final String name = this.userName;
    chatMessage.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                chatconsole(name + ": " + chatMessage.getText());
            }
        }
    });
    GridBagConstraints gbc_chatMessage = new GridBagConstraints();
    gbc_txtMessage.insets = new Insets(000, 600, 000, -330);
    gbc_txtMessage.fill = GridBagConstraints.HORIZONTAL;
    gbc_txtMessage.gridx = 0;
    gbc_txtMessage.gridy = 2;
    gbc_txtMessage.gridwidth = 2;
    gbc_txtMessage.weightx = 1;
    gbc_txtMessage.weighty = 0;
    txtMessage.setColumns(5);
    contentPane.add(chatMessage, gbc_txtMessage);

    JButton btnSend = new JButton("Send");
    btnSend.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Color color = new Color(92, 219, 86);
            String text = txtMessage.getText();
            inWriter.println(text); 
            console(txtMessage.getText(), color);
            command = txtMessage.getText();
        }
    });
    GridBagConstraints gbc_btnSend = new GridBagConstraints();
    gbc_btnSend.insets = new Insets(0, 0, 0, 275);
    gbc_btnSend.gridx = 2;
    gbc_btnSend.gridy = 2;
    gbc_btnSend.weightx = 0;
    gbc_btnSend.weighty = 0;
    contentPane.add(btnSend, gbc_btnSend);

    list = new JList();
    GridBagConstraints gbc_list = new GridBagConstraints();
    gbc_list.insets = new Insets(0, 600, 330, -330);
    gbc_list.fill = GridBagConstraints.BOTH;
    gbc_list.gridx = 0;
    gbc_list.gridy = 0;
    gbc_list.gridwidth = 2;
    gbc_list.gridheight = 2;
    JScrollPane p = new JScrollPane();
    p.setViewportView(list);
    contentPane.add(p, gbc_list);
    list.setFont(new Font("Verdana", 0, 24));

    System.out.println("HEY");

    setVisible(true);

    new SwingWorker<Void, String>() { 
         protected Void doInBackground() throws Exception { 
            Scanner s = new Scanner(outPipe);
            while (s.hasNextLine()) {
                     String line = s.nextLine();
                     publish(line);
            }
            return null; 
        } 
         @Override protected void process(java.util.List<String> chunks) { 
             for (String line : chunks) {
                try {
                    Document doc = history.getDocument();
                    doc.insertString(doc.getLength(), line + "\r\n", null);
                } catch(BadLocationException exc) {
                    exc.printStackTrace();
                }
             }
         } 

    }.execute(); 

    Singleplayer spp = new Singleplayer();
    Singleplayer.startOfGame();

    setVisible(true);
}

public static void console(String message, Color color) {
    txtMessage.setText("");
    try {
        StyledDocument doc = history.getStyledDocument();
        Style style = history.addStyle("", null);
        StyleConstants.setForeground(style, color);
        doc.insertString(doc.getLength(), message + "\r\n", style);
    } catch(BadLocationException exc) {
        exc.printStackTrace();
    }
}

public static void console(String message) {
    txtMessage.setText("");
    try {
        Document doc = history.getDocument();
        doc.insertString(doc.getLength(), message + "\r\n", null);
    } catch(BadLocationException exc) {
        exc.printStackTrace();
    }
}

public void chatconsole(String message) {
    chatMessage.setText("");
    try {
          Document doc = chat.getDocument();
          doc.insertString(doc.getLength(), message + "\r\n", null);
       } catch(BadLocationException exc) {
          exc.printStackTrace();
       }
}

public static void single() {
    Singleplayer sp = new Singleplayer();
    new SingleplayerGUI("", sp);
}

public static void main(String[] args) {
    Singleplayer sp = new Singleplayer();
    new SingleplayerGUI("", sp);
}
到目前为止,我已经发现在将输出管道添加到控制台时最有可能出现问题。我是否能够修复此问题,还是无法将输出管用于此应用程序


提前感谢您的帮助。

setVisible()方法会被调用两次。。删除Singleplayer.startOfGame()之后的第二个setVisible()调用;SingleplayerGUI构造函数中的行。

当您提出问题时,应该尝试并努力将显示的代码量减少到说明问题所需的最低限度。您的GUI代码充斥着用户输入逻辑和游戏逻辑,这一事实使得您很难准确指出可能出现的错误。我假设JVM控制台中没有报告错误,这对吗?您也没有显示一些非常重要的代码部分。例如,看起来您使
SinglePlayerGUI
扩展了另一个类,但没有显示代码的该部分。我假设它是一个
JFrame
JPanel
?是的,对所有的代码感到抱歉。你是对的;没有错误。哦,我忘了告诉你那件事了。SingleplayerGUI类扩展了JFrame。@ArnaudDenoyelle我想我可以切换到JavaFX,但这需要花很多时间来学习,我想继续使用我熟悉的东西。谢谢你的帮助,但即使在我删除第二个setVisible调用后,同样的问题也会发生。
public static void startOfGame(){
    System.out.println("HEY");
    SingleplayerGUI.console("Welcome to Console Clash Pre Alpha Version 1!");
    SingleplayerGUI.console("Created by Drift");
    SingleplayerGUI.console("Published by Boring Games");
    SingleplayerGUI.console("");
    SingleplayerGUI.console("");
    menuScreen();
}

static void menuScreen() {
    Scanner scan = new Scanner(System.in);
    System.out.println("To play the game, type 'start'.  To quit, type 'quit'.");
        String menu = scan.nextLine();
        if (menu.equals("start")) {
            start();
        } else if (menu.equals("quit")) {
            quit();
        } else {
            menuScreen();
        }
}

private static void quit() {
    SingleplayerGUI.console("You quit the game.");
}

private static void start() {
    SingleplayerGUI.console("You started the game.");
}