首页 > 解决方案 > Java JFrame 中的所有东西都是不可见的

问题描述

我制作了一个消息传递程序,其中客户端有一个 GUI。在启动客户端之前,客户端程序需要有关用户和服务器的信息。我使用 args 数组通过命令行输入了信息。

但是现在我让程序工作了,我制作了一个用于输入信息的 GUI。我在另一个调用客户端类并将信息传递给客户端类的类中实现了它。信息 GUI 工作正常,但是当我输入信息并调用客户端类时,框架会显示,但框架中没有任何组件可见。

当我恢复到旧方法时,客户端 GUI 工作正常。我不知道我还应该做什么。

调用客户端类的代码:

btn.addActionListener(new ActionListener() {
    ...
    username = Username.getText();
    hostName = sa.getText();
    portNr = Integer.parseInt(spnr.getText());
    f.dispose();
    System.out.println("frame disposed of");
    try {
        Client client = new Client();
        f.dispose();
        Client.llc(username, hostName, portNr);
    } catch (IOException e) {
        e.printStackTrace();
    }
    }
});

客户端代码:

public static void llc(String iun, String ihn, int ipnr) throws IOException {
        username = iun;
        hostName = ihn;
        portNr = ipnr;
        launchClient();
    }

    public static void launchClient() throws IOException {
        try (
            //socket setup
            Socket socket = new Socket(hostName, portNr);
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        ) {
            System.out.println("opened streams");

            //frame setup
            JFrame frame = new JFrame("frame");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            //text field
            JTextField MSGText = new JTextField(5);

            //text area
            JTextArea MSGD = new JTextArea(20, 30);
            MSGD.setEditable(false);
            JScrollPane scrollPane = new JScrollPane(MSGD);
            System.out.println("opened streams");

            //button
            JButton b = new JButton("Send message");
            b.setPreferredSize(new Dimension(60, 30));
            b.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent event) {
                    sendMSG = MSGText.getText();
                    MSGText.setText("");
                    out.println("<" + username + ">: " + sendMSG);
                }
            });

            //panel
            JPanel p = new JPanel();
            p.setLayout(new BoxLayout(p, BoxLayout.PAGE_AXIS));
            p.add(Box.createVerticalStrut(5));
            p.add(scrollPane);
            p.add(Box.createVerticalStrut(5));
            p.add(MSGText);
            p.add(Box.createVerticalStrut(5));
            p.add(b);
            p.add(Box.createVerticalStrut(5));

            JPanel pMain = new JPanel();
            pMain.setLayout(new BoxLayout(pMain, BoxLayout.LINE_AXIS));
            pMain.add(Box.createHorizontalStrut(5));
            pMain.add(p);
            pMain.add(Box.createHorizontalStrut(5));
            frame.getContentPane().add(pMain);

            //frame visiblity
            frame.pack();
            frame.setVisible(true);
            System.out.println("opened streams");

            while((getMSG = in.readLine()) != null) {
                MSGD.append(getMSG + "\n");
                System.out.println("opened streams");
            }
        }


    }

标签: javaswinguser-interfaceevent-dispatch-thread

解决方案


如果您的客户端代码是扩展 JFrame 的客户端类,则 Frame 可能在组件之前初始化。要解决此问题,您只需在客户端类中将 JFrame 成员设置为公共全局变量,然后在调用 llc 后调用 client.frame.repaint()。

public class Client extends JFrame {
    public JFrame frame;
    //everything else the same 
}

-----------------------------------

btn.addActionListener(new ActionListener() {
    ...
    username = Username.getText();
    hostName = sa.getText();
    portNr = Integer.parseInt(spnr.getText());
    f.dispose();
    System.out.println("frame disposed of");
    try {
        Client client = new Client();
        f.dispose();
        Client.llc(username, hostName, portNr);
        client.frame.repaint();
    } catch (IOException e) {
        e.printStackTrace();
    }
    }
});

推荐阅读