简体   繁体   English

Java JFrame 中的所有内容均不可见

[英]Java every thing in JFrame isn't visible

I made a messaging program where the client has a GUI.我制作了一个消息传递程序,其中客户端有一个 GUI。 Before starting the client the client program needs info about the user and the server.在启动客户端之前,客户端程序需要有关用户和服务器的信息。 I entered the info through the command line using the args array.我使用 args 数组通过命令行输入了信息。

But now that I got the program working I made a GUI for entering the info.但是现在我让程序工作了,我制作了一个用于输入信息的 GUI。 I made it in another class that calls the client class and passes the info to the client class.我在另一个调用客户端 class 并将信息传递给客户端 class 的 class 中创建了它。 The info GUI works fine but when I enter the info and call the client class the frame shows up but none of the components are visible in the frame.信息 GUI 工作正常,但是当我输入信息并调用客户端 class 时,会显示框架,但框架中看不到任何组件。

When I revert to back to the old method the client GUI works fine.当我恢复到旧方法时,客户端 GUI 工作正常。 I don't know what else I should do.我不知道我还应该做什么。

The code that calls the client class:调用客户端 class 的代码:

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

The client code:客户端代码:

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


    }

If your client code is a Client class that extends JFrame, then it's possible that the Frame is initializing before the components.如果您的客户端代码是扩展 JFrame 的客户端 class,则框架可能在组件之前初始化。 To fix this, you can just set your JFrame member to public global variable in your client class and then call client.frame.repaint() after you call llc.要解决此问题,您只需将 JFrame 成员设置为客户端 class 中的公共全局变量,然后在调用 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();
    }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM