所以我一直在關閉這個程序,並在幾天內開始工作,它是一個基本的客戶端服務器聊天室。我試圖讓它在啓動客戶端時彈出的gui文本在portnumber,servername和textbox JTextFields
中。昨天是這種情況,但我已經改變了一些東西,現在gui在文本字段中沒有文字。該代碼位於try catch塊開始處運行的displaysettings方法中。任何人都知道它爲什麼不起作用?在我的GUI中沒有出現在Jframes中的文本
import java.net.*;
import java.util.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class clienttry2 extends JFrame implements ActionListener {
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JButton send = new JButton("Send");
JButton connect = new JButton("Connect");
JLabel server = new JLabel("Server");
JLabel port = new JLabel("Port");
JButton disconnect = new JButton("Disconnect");
static JTextField servername = new JTextField(10);
static JTextField portnumber = new JTextField(10);
static JTextField textbox = new JTextField(40);
JTextArea chatbox = new JTextArea(20,45);
static Boolean isconnected = false;
static Boolean sending = false;
static Socket server1;
static ObjectInputStream in;
static ObjectOutputStream out;
public clienttry2(){
setTitle("Chat Room");
setLayout(new java.awt.FlowLayout());
setSize(600,500);
panel1.add(chatbox); //has all the chats
panel2.add(textbox); //area to type new messages into
panel2.add(send); send.addActionListener(this);//send button
panel3.add(server);
panel3.add(servername);
panel3.add(port);
panel3.add(portnumber);
panel3.add(connect); connect.addActionListener(this);
panel3.add(disconnect); disconnect.addActionListener(this); disconnect.setEnabled(false);
add(panel1);
add(panel2);
add(panel3);
}
public static void main(String[] args)throws Exception {
Client display = new Client();
display.setVisible(true);
try{
displaysettings();
connect();
setup();
String message;
message = (String) in.readObject();
System.out.println(message);
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
private static void displaysettings() {
portnumber.setText("3333");
servername.setText("localhost");
textbox.setText("This is where you type your messages to send to others on the server!");
}
private static void connect() throws UnknownHostException, IOException {
server1 = new Socket("localhost", 3333);
}
private static void setup() throws IOException {
out = new ObjectOutputStream(server1.getOutputStream());
out.flush();
in = new ObjectInputStream(server1.getInputStream());
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==connect)
{
System.out.println("connected");
isconnected = true;
connect.setEnabled(false);
disconnect.setEnabled(true);
}
if(e.getSource()==send)
{
System.out.println("sending chat");
sending = true;
}
if(e.getSource()==disconnect)
{
try {
server1.close();
out.close();
isconnected = false;
connect.setEnabled(true);
disconnect.setEnabled(false);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
你使用過調試器嗎? – ControlAltDel
你不會像你應該的那樣發佈任何你的swing代碼到Event Dispatch Thread。不知道這是否是真正的問題,因爲我沒有檢查整個事情。創建一個[MCVE](http://stackoverflow.com/help/mcve),誰知道,你可能會自己偶然發現問題。如果你沒有找到它,編輯你的MCVE到你的文章。我強烈建議多學習一下Swing,並研究一下代碼設計,這樣你就不會混淆所有東西,從而無法孤立地測試東西。 –
我建議將主方法中的所有代碼封裝到傳遞給Runnable的Runnable中SwingUtilities.invokeAndWait使UI在EDT上構建。請參閱https://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html – copeg