2017-02-20 167 views
-1

我正在聊天客戶端(我沒有做到)。我正在製作一個誰在線列表,它不會顯示。這裏是代碼JList不在jframe中顯示

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

該守則仍然需要工作。有誰知道它爲什麼列表不顯示?

+0

它可能是,它只是小給你看,試圖在一個'JScrollPane'包裝'listDisplay'並將其添加到該幀,而不是 – MadProgrammer

+0

您還需要看看[併發在Swing中](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/),因爲你違反了Swing的單線程規則 – MadProgrammer

回答

2
names.add("Online People"); 
listDisplay.equals(names); 

上面的代碼什麼都不做。您將一個字符串添加到列表。但是,您絕不會將List的內容添加到JList。 equals(..)方法用於比較對象,以查看一個對象是否等於另一對象。

當您添加到JList時,您將數據添加到JListListModel。最簡單的方法是創建一個DefaultListModel並將該模型添加到JList。然後你就可以將數據直接添加到模型(也沒有必要爲List):

DefaultListModel model = new DefaultListModel(); 
model.addElement("Online People"); 
JList list = new JList(model); 

然後當你添加新的人,你只是調用的DefaultListModeladdElement(...)方法。

閱讀Swing教程中關於How to Use Lists的部分以獲取工作示例。 ListDemo中的「Hire」按鈕顯示如何添加項目。另請注意,本教程中的示例將如何添加JScrollPane`,以便在添加更多用戶時顯示滾動條。

frame.getContentPane().add(listDisplay, "East"); 

請勿使用魔術變量。人們不知道「東方」是什麼意思。該API將具有可用作約束的變量。例如:

frame.getContentPane().add(listDisplay, BorderLayout.EAST); 
+0

謝謝,它看起來像它的工作,但我不縫能夠從列表中刪除某人 –

+0

'我不縫以能夠從列表中刪除某人' - 您是否閱讀了教程並下載了演示代碼? 「Hire」和「Fire」按鈕完全符合你的要求。 – camickr