Java JFrame无法正常打开

时间:2017-11-09 22:55:06

标签: java swing

我有一个JFrame,其中包含一个按钮,该按钮应调用另一个类,在该类中打开另一个JFrame并向其添加许多元素。但是,由于某种原因,这不起作用,只是打开JFrame时没有任何元素。我确定这是一个非常简单的修复,但我太愚蠢了,无法弄明白。代码也非常混乱,编写得很糟糕,但我稍后会对所有这些进行排序(我知道我在其中使用的是静态的)。

以下是包含按钮和操作列表器的类中的代码:

public static void server(){

    JButton server = new JButton();
    server.setText("Server");

    server.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

            String name = nameChoice;
            String ip = ipChoice;
            String port = portChoice;
            try {
                server2(port);
            } catch (IOException e1) {
                e1.printStackTrace();
            }


        }
    });

    panel.add(server);

}

public static void server2(String port) throws IOException {

    gui.frame();
    sockets.serverSetup(port);

    while (true) {

        sockets.receiveMsg();

    }

}

这是它所调用的类:

 package com.company;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.Vector;

public class chatWindowScreen {

    static JFrame frame = new JFrame();
    static JPanel panel = new JPanel();
    static JTextArea sent;
    static JTextArea received;
    static JTextArea newMsg;
    static JButton sendButton;
    public static String msg;

    static network sockets = new network();

    public static boolean close = false;

    public void frame() throws IOException {

        System.out.println("GUI opened");

        frame.setTitle("Messaging Application");
        frame.setSize(500, 500);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        sentMsg();
        receivedMsg();
        msgBox();
        sendBtn();
        save();

        frame.add(panel);
        frame.setVisible(true);

    }

    public static void sentMsg() {

        sent = new JTextArea(15, 20);
        sent.setText("Sent" + "\n");
        sent.setLineWrap(true);
        sent.setEditable(false);
        JScrollPane pane = new JScrollPane(sent, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        panel.add(pane);

    }

    public static void receivedMsg() {

        received = new JTextArea(15, 20);
        received.setText("Received" + "\n");
        received.setEditable(false);
        received.setLineWrap(true);
        JScrollPane pane1 = new JScrollPane(received, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        panel.add(pane1);

    }

    public static void msgBox() {

        newMsg = new JTextArea(1, 15);
        newMsg.setText("");
        newMsg.setLineWrap(true);
        JScrollPane pane2 = new JScrollPane(newMsg, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        panel.add(pane2);

    }

    public static void sendBtn() {

        sendButton = new JButton();
        sendButton.setText("Send!");

        sendButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                msg = newMsg.getText();
                newMsg.setText(null);
                try {
                    sockets.sendMsg(msg);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                msg = null;

            }
        });


        panel.add(sendButton);

    }

    public static void save(){

        JButton save = new JButton();
        save.setText("Save Conversation");
        save.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                saveToFile save = new saveToFile();

            }

        });

        panel.add(save);

    }


}

感谢任何帮助。

0 个答案:

没有答案