Java JList未显示在jframe中

Java JList未显示在jframe中,java,swing,arraylist,jframe,jlist,Java,Swing,Arraylist,Jframe,Jlist,我正在开发一个聊天客户端(我没有成功)。我正在制作一个“谁是在线的”列表,但它不会出现。这是密码 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import

我正在开发一个聊天客户端(我没有成功)。我正在制作一个“谁是在线的”列表,但它不会出现。这是密码

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 * A simple Swing-based client for the chat server.  Graphically
 * it is a frame with a text field for entering messages and a
 * textarea to see the whole dialog.
 *
 * The client follows the Chat Protocol which is as follows.
 * When the server sends "SUBMITNAME" the client replies with the
 * desired screen name.  The server will keep sending "SUBMITNAME"
 * requests as long as the client submits screen names that are
 * already in use.  When the server sends a line beginning
 * with "NAMEACCEPTED" the client is now allowed to start
 * sending the server arbitrary strings to be broadcast to all
 * chatters connected to the server.  When the server sends a
 * line beginning with "MESSAGE " then all characters following
 * this string should be displayed in its message area.
 */
public class ChatClient {
    List<String> names = new ArrayList<String>();
    BufferedReader in;
    PrintWriter out;
    JFrame frame = new JFrame("Chatter");

    JTextArea messageArea = new JTextArea(8, 40);
JTextField textField = new JTextField(40);
JList listDisplay = new JList();

/**
 * Constructs the client by laying out the GUI and registering a
 * listener with the textfield so that pressing Return in the
 * listener sends the textfield contents to the server.  Note
 * however that the textfield is initially NOT editable, and
 * only becomes editable AFTER the client receives the NAMEACCEPTED
 * message from the server.
 */
public ChatClient() {

    // Layout GUI
    names.add("Online People");
    listDisplay.equals(names);

    textField.setEditable(false);
    messageArea.setEditable(false);
    frame.getContentPane().add(listDisplay, "East");
    frame.getContentPane().add(textField, "South");
    frame.getContentPane().add(new JScrollPane(messageArea), "North");

    frame.pack();
    // Add Listeners
    textField.addActionListener(new ActionListener() {
        /**
         * Responds to pressing the enter key in the textfield by sending
         * the contents of the text field to the server.    Then clear
         * the text area in preparation for the next message.
         */
        public void actionPerformed(ActionEvent e) {
            out.println(textField.getText());
            textField.setText("");
        }
    });
}

/**
 * Prompt for and return the address of the server.
 */
private String getServerAddress() {
    return JOptionPane.showInputDialog(
        frame,
        "Enter IP Address of the Server:",
        "Welcome to the Chatter",
        JOptionPane.QUESTION_MESSAGE);
}

/**
 * Prompt for and return the desired screen name.
 */
String getName() {
    String name = JOptionPane.showInputDialog(
            frame,
            "Choose a screen name:",
            "Screen name selection",
            JOptionPane.PLAIN_MESSAGE);
    return name;
}

/**
 * Connects to the server then enters the processing loop.
 */
private void run() throws IOException {

    // Make connection and initialize streams
    String serverAddress = getServerAddress();
    @SuppressWarnings("resource")
    Socket socket = new Socket(serverAddress, 25565);
    in = new BufferedReader(new InputStreamReader(
        socket.getInputStream()));
    out = new PrintWriter(socket.getOutputStream(), true);
    String name = getName();

    // Process all messages from server, according to the protocol.
    while (true) {
        String line = in.readLine();
        if (line.startsWith("SUBMITNAME")) {

            out.println(name);
        } else if (line.equals("NAMEACCEPTED " + name)){
            textField.setEditable(true);
            names.add(name);
            listDisplay.equals(names);
        }

        else if (line.startsWith("NAMEACCEPTED ")) {
            line.replaceAll("NAMEACCEPTED ", "");
            names.add(name);
            listDisplay.equals(names);
        } 

        else if (line.startsWith("MESSAGE")) {
            messageArea.append(line.substring(8) + "\n");
                 messageArea.setCaretPosition(messageArea.getDocument().getLength());
        } else if (line.startsWith("KICK " + name) ){
            JOptionPane.showMessageDialog(null, "YOU HAVE BEEN KICKED \nRestart the program to join again", "KICK", JOptionPane.WARNING_MESSAGE);
            textField.setEditable(false);
        } else if (line.startsWith("SILENT")){
            textField.setEditable(false);
        }
    }

}

/**
 * Runs the client as an application with a closeable frame.
 */
public static void main(String[] args) throws Exception {
    ChatClient client = new ChatClient();
    client.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    client.frame.setVisible(true);
    client.run();
}
}
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.io.PrintWriter;
导入java.net.Socket;
导入java.util.ArrayList;
导入java.util.array;
导入java.util.List;
导入javax.swing.JFrame;
导入javax.swing.JList;
导入javax.swing.JOptionPane;
导入javax.swing.JScrollPane;
导入javax.swing.JTextArea;
导入javax.swing.JTextField;
/**
*一个简单的基于Swing的聊天服务器客户端。生动地
*它是一个框架,带有用于输入消息的文本字段和
*textarea以查看整个对话框。
*
*客户端遵循聊天协议,如下所示。
*当服务器发送“SUBMITNAME”时,客户端用
*所需的屏幕名称。服务器将继续发送“SUBMITNAME”
*只要客户端提交的屏幕名称是
*已经在使用中。当服务器发送一行开始时
*使用“NAMEACCEPTED”,现在允许启动客户端
*向所有服务器发送要广播的任意字符串
*连接到服务器的聊天程序。当服务器发送
*以“MESSAGE”开头的行,然后是后面的所有字符
*此字符串应显示在其消息区域中。
*/
公共类聊天客户端{
列表名称=新的ArrayList();
缓冲读取器;
打印输出;
JFrame=新JFrame(“颤振”);
JTextArea messageArea=新的JTextArea(8,40);
JTextField textField=新的JTextField(40);
JList listDisplay=新建JList();
/**
*通过布局GUI和注册
*具有文本字段的侦听器,以便在
*侦听器将textfield内容发送到服务器。注意
*但是,文本字段最初不可编辑,并且
*仅在客户端收到NAMEACCEPTED后才可编辑
*来自服务器的消息。
*/
公共聊天室客户端(){
//布局图形用户界面
姓名。添加(“在线人士”);
listDisplay.equals(名称);
textField.setEditable(false);
messageArea.setEditable(false);
frame.getContentPane();
frame.getContentPane().add(textField,“South”);
frame.getContentPane();
frame.pack();
//添加侦听器
addActionListener(新ActionListener(){
/**
*通过发送消息来响应按文本字段中的enter键
*将文本字段的内容发送到服务器。然后清除
*准备下一条消息的文本区域。
*/
已执行的公共无效操作(操作事件e){
out.println(textField.getText());
textField.setText(“”);
}
});
}
/**
*提示输入并返回服务器的地址。
*/
私有字符串getServerAddress(){
return JOptionPane.showInputDialog(
框架
“输入服务器的IP地址:”,
“欢迎来到聊天室”,
JOptionPane.QUESTION_消息);
}
/**
*提示输入并返回所需的屏幕名称。
*/
字符串getName(){
String name=JOptionPane.showInputDialog(
框架
“选择一个屏幕名称:”,
“屏幕名称选择”,
JOptionPane.普通消息);
返回名称;
}
/**
*连接到服务器,然后进入处理循环。
*/
私有void run()引发IOException{
//建立连接并初始化流
字符串serverAddress=getServerAddress();
@抑制警告(“资源”)
套接字=新的套接字(服务器地址,25565);
in=新的BufferedReader(新的InputStreamReader(
getInputStream());
out=新的PrintWriter(socket.getOutputStream(),true);
字符串名称=getName();
//根据协议处理来自服务器的所有消息。
while(true){
String line=in.readLine();
if(第行开始使用(“SUBMITNAME”)){
out.println(名称);
}else if(行等于(“接受的名称”+名称)){
textField.setEditable(true);
名称。添加(名称);
listDisplay.equals(名称);
}
else if(第行开始使用(“接受名称”)){
行.replaceAll(“NAMEACCEPTED”,”);
名称。添加(名称);
listDisplay.equals(名称);
} 
else if(行.startsWith(“消息”)){
messageArea.append(行.子字符串(8)+“\n”);
messageArea.setCaretPosition(messageArea.getDocument().getLength());
}else if(第行开始(“KICK”+名称)){
JOptionPane.showMessageDialog(null,“您已被踢出\n重新启动程序以再次加入”,“踢出”,JOptionPane.WARNING\u消息);
textField.setEditable(false);
}else if(行开始时使用(“无声”)){
textField.setEditable(false);
}
}
}
/**
*将客户端作为具有可关闭框架的应用程序运行。
*/
公共静态void main(字符串[]args)引发异常{
ChatClient=new ChatClient();
client.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
client.frame.setVisible(true);
client.run();
}
}
代码仍然需要改进。有人知道为什么它的列表没有出现吗

names.add("Online People");
listDisplay.equals(names);
上面的代码不起任何作用。将字符串添加到列表中。但是您从不将
列表的内容添加到
JList
。equals(..)方法用于比较对象,以查看一个对象是否等于另一个对象

添加到
JList
时,将数据添加到
JList
ListModel
。最简单的方法是创建一个
DefaultListModel
,并将模型添加到JList中。然后,您可以直接将数据添加到模型中(不需要
列表
):

然后,当您添加新人员时,只需调用
addEleme
DefaultListModel model = new DefaultListModel();
model.addElement("Online People");
JList list = new JList( model );
frame.getContentPane().add(listDisplay, "East");
frame.getContentPane().add(listDisplay, BorderLayout.EAST);