简体   繁体   English

JList无法显示

[英]JList not Displayable

I have been working on this problem for a couple of days and I cannot understand where I went wrong. 我已经在这个问题上工作了几天,但我不明白哪里出了问题。 I have created a Server-Client Chat Program and on the Server GUI there is a tab that shows a list of users. 我创建了一个服务器-客户端聊天程序,并且在服务器GUI上有一个显示用户列表的选项卡。 This list works in every way that I want. 该列表可以按照我想要的任何方式工作。 I wanted to add a UserList to the Client GUI and the JList is there, but when I update the DefaultListModel , the JList only updates on the ServerGUI . 我想将UserList添加到客户端GUI,并且JList在其中,但是当我更新DefaultListModelJList仅在ServerGUI上更新。 I tried to debug and found that the JList on the ChatGUI is not displayable and I don't know why, or how to fix it. 我尝试调试,发现ChatGUI上的JList无法显示,我也不知道为什么或如何修复它。

Here is my (relevant) code: 这是我的(相关)代码:

Client Class 客户类别

public class Client {
    String username;
    Socket socket;
    PrintWriter out;
    Scanner in;

    public Client (String username, Socket socket, PrintWriter out, Scanner in) {
        this.username = username;
        this.socket = socket;
        this.out = out;
        this.in = in;
    }
}

ServerGUI Class - (the register method is called later in the program when a Client joins) ServerGUI类-(当客户端加入时,稍后在程序中调用register方法)

public class ServerGUI {
    public volatile static ArrayList<Client> users;
    public static DefaultListModel<String> model = new DefaultListModel<String>();
    static Client clientReg;

    public static void register(Client client) {
        clientReg = client;
        users.add(clientReg);
        model.addElement(clientReg.username);
         ServerView.userList.setModel(model);
        ChatView.userList.setModel(model);
    }
}

ChatView Class ChatView类别

public class ChatView extends JFrame {
    public JPanel contentPane;
    public static JList<String> userList = new JList<String>();
    public static JTextArea chatOutput;
    private JTextField inputField;

    public ChatView() {
        setResizable(false);
        setTitle("Chat GUI");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 550, 475);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JTabbedPane tabs = new JTabbedPane(JTabbedPane.TOP);
        tabs.setBounds(0, 0, 535, 435);
        contentPane.add(tabs);

        JPanel chatViewer = new JPanel();
        tabs.addTab("Chat", null, chatViewer, null);
        chatViewer.setLayout(null);

        // Code that makes up the chatViewer JPanel

        JPanel userListPane = new JPanel();
        tabs.addTab("User List", null, userListPane, null);
        userListPane.setLayout(null);

        JLabel label = new JLabel("User List:");
        label.setBounds(10, 10, 510, 20);
        userListPane.add(label);

        userList.setModel(new AbstractListModel<String>() {
            public String getElementAt(int index) {
                return ServerGUI.model.get(index);
            }

            public int getSize() {
                return ServerGUI.model.size();
            }
        });
        userList.setValueIsAdjusting(true);
        userList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
        userList.setBounds(10, 40, 510, 395);

        userListPane.add(userList);
    }
}

Most of my programming has been self taught so if there is any format that is incorrect please let me know so that I may be able to correct it. 我的大多数编程都是自学的,因此,如果有任何格式不正确,请告诉我,以便我可以对其进行更正。

OK, I see how you're trying to "share" models via static fields and methods: 好的,我知道您如何尝试通过静态字段和方法“共享”模型:

userList.setModel(new AbstractListModel<String>() {
    public String getElementAt(int index) {
        return ServerGUI.model.get(index);
    }

    public int getSize() {
        return ServerGUI.model.size();
    }
});

and this will never work, and is not how clients and servers transfer information. 这将永远无法工作,而客户端和服务器将不会传输信息。 For one, while yes you have the server static fields and methods available to the client, the actual server class and instance are running in a completely different JVM, usually on a different machine entirely, and so the data that you're getting does not reflect the true state of the actual Server object and static class state on the server itself, but is a mere shadow of the correct object/class. 一方面,虽然是的,但您拥有客户端可用的服务器静态字段和方法,但是实际的服务器类和实例在完全不同的JVM中运行,通常完全在另一台计算机上运行,​​因此获取的数据不会反映服务器上实际Server对象的真实状态和静态类状态,但这仅仅是正确的对象/类的影子。

Suggestions: 意见建议:

  • Use a standard list model for your client, and delete the code you're using, it won't work 为您的客户使用标准列表模型,然后删除您正在使用的代码,它将无法正常工作
  • If this is true client/server code where you transmit information over a socket, then the client must get updates from the server through the socket. 如果这是真实的客户机/服务器代码,您可以在其中通过套接字传输信息,则客户机必须通过套接字从服务器获取更新。 We have no idea how you're wiring this so, I cannot give specific recommendations other then -- get the data from the socket 我们不知道您如何进行布线,因此,我只能提出其他建议-从套接字获取数据
  • And do so in a background thread -- ie, the client should receive socket data off of the Swing event thread 并在后台线程中执行此操作-即,客户端应从Swing事件线程中接收套接字数据
  • But then use this information received to update the client's list model on the event thread. 但是,然后使用接收到的此信息在事件线程更新客户端的列表模型。 One way to do this is via a SwingWorker<Void, String> using its process / publish method pair. 一种方法是通过SwingWorker<Void, String>使用其process / publish方法对。

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

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