Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么当我按下按钮时,应用程序中JLabel中的文本没有相应更新?_Java - Fatal编程技术网

Java 为什么当我按下按钮时,应用程序中JLabel中的文本没有相应更新?

Java 为什么当我按下按钮时,应用程序中JLabel中的文本没有相应更新?,java,Java,字段lblNewLabel_1已更改为服务器初始化尝试检查控制台的状态!在按钮的ActionListener中;为什么我按下按钮时它没有改变 这可能是由startServer方法调用whiletrue循环引起的吗 package server; import java.awt.Font; public class StudyItServerGUI { private JFrame frame; public JLabel lblNewLabel_1; /**

字段lblNewLabel_1已更改为服务器初始化尝试检查控制台的状态!在按钮的ActionListener中;为什么我按下按钮时它没有改变

这可能是由startServer方法调用whiletrue循环引起的吗

package server;

import java.awt.Font;

public class StudyItServerGUI {

    private JFrame frame;
    public JLabel lblNewLabel_1;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
                try {
                    StudyItServerGUI window = new StudyItServerGUI();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }

    }

    /**
     * Create the application.
     */
    public StudyItServerGUI() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 378, 251);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JLabel lblNewLabel = new JLabel("StudyIt Server Creation Application!");
        lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
        lblNewLabel.setFont(new Font("MT Extra", Font.PLAIN, 15));
        lblNewLabel.setBounds(0, 0, 372, 16);
        frame.getContentPane().add(lblNewLabel);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(241, 68, 121, 93);
        frame.getContentPane().add(scrollPane);

        JList list = new JList();
        scrollPane.setColumnHeaderView(list);

        JButton btnStart = new JButton("START!");
        btnStart.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                new Thread(new Runnable() {
                    public void run() {
                        ServerApplication.startServer();
                        lblNewLabel_1.setText("Server Init Attempted (Check Console for Status)!");

                        //y u no change
                    }
                }).start();
            }
        });
        btnStart.setBounds(241, 159, 117, 29);
        frame.getContentPane().add(btnStart);

        JButton btnCreateQuestionset = new JButton("Create QuestionSet");
        btnCreateQuestionset.setBounds(32, 68, 146, 98);
        frame.getContentPane().add(btnCreateQuestionset);

        lblNewLabel_1 = new JLabel();
        lblNewLabel_1.setText("yes");
        lblNewLabel_1.setFont(new Font("Charcoal CY", Font.BOLD, 16));
        lblNewLabel_1.setBounds(6, 197, 366, 26);
        frame.getContentPane().add(lblNewLabel_1);
    }
    static class ServerApplication{

            private static ServerSocket serverSocket;
            private static Socket clientSocket;
            private static InputStreamReader inputStreamReader;
            private static BufferedReader bufferedReader;
            private static String message;
            private static PrintWriter printWriter;

            static void startServer() {
                try {
                    serverSocket = new ServerSocket(4444);  //Server socket
                } catch (IOException e) {
                    System.out.println("Could not listen on port: 4444");
                }

                System.out.println("Server started. Listening to the port 4444");

                while (true) {
                    try {
                        clientSocket = serverSocket.accept();   //accept the client connection
                        inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
                        bufferedReader = new BufferedReader(inputStreamReader); //get the client message
                        message = bufferedReader.readLine();

                        System.out.println(message);
                        //StudyIt

                        inputStreamReader.close();
                        clientSocket.close();

                    } catch (IOException ex) {
                        System.out.println("Problem in message reading");
                    }
                }
            }
    }
}

有什么乱七八糟的?是不是碰巧全部打印为单独的行,而您希望在同一行上打印?这可能是因为您使用System.out.println而不是System.out.print。

将whiletrue循环检查任何新消息移动到线程中,而不是启动服务器的按钮中的操作。这将阻止帧更新为无法访问的代码。

您发布的输出是否为所需的输出?或者这就是你收到的输出?请发布两个未发布的内容,并指定哪一个是哪一个。谢谢你可能的副本