字段lblNewLabel_1更改为" Server Init Attempted(检查控制台状态)!"在按钮的ActionListener中;当我按下按钮时为什么不改变?
这可能是由调用while(true)循环的startServer方法引起的吗?
package server;
import java.awt.Font;
public class StudyItServerGUI {
private JFrame frame;
public JLabel lblNewLabel_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
StudyItServerGUI window = new StudyItServerGUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the application.
*/
public StudyItServerGUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 378, 251);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblNewLabel = new JLabel("StudyIt Server Creation Application!");
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setFont(new Font("MT Extra", Font.PLAIN, 15));
lblNewLabel.setBounds(0, 0, 372, 16);
frame.getContentPane().add(lblNewLabel);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(241, 68, 121, 93);
frame.getContentPane().add(scrollPane);
JList list = new JList();
scrollPane.setColumnHeaderView(list);
JButton btnStart = new JButton("START!");
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
new Thread(new Runnable() {
public void run() {
ServerApplication.startServer();
lblNewLabel_1.setText("Server Init Attempted (Check Console for Status)!");
//y u no change
}
}).start();
}
});
btnStart.setBounds(241, 159, 117, 29);
frame.getContentPane().add(btnStart);
JButton btnCreateQuestionset = new JButton("Create QuestionSet");
btnCreateQuestionset.setBounds(32, 68, 146, 98);
frame.getContentPane().add(btnCreateQuestionset);
lblNewLabel_1 = new JLabel();
lblNewLabel_1.setText("yes");
lblNewLabel_1.setFont(new Font("Charcoal CY", Font.BOLD, 16));
lblNewLabel_1.setBounds(6, 197, 366, 26);
frame.getContentPane().add(lblNewLabel_1);
}
static class ServerApplication{
private static ServerSocket serverSocket;
private static Socket clientSocket;
private static InputStreamReader inputStreamReader;
private static BufferedReader bufferedReader;
private static String message;
private static PrintWriter printWriter;
static void startServer() {
try {
serverSocket = new ServerSocket(4444); //Server socket
} catch (IOException e) {
System.out.println("Could not listen on port: 4444");
}
System.out.println("Server started. Listening to the port 4444");
while (true) {
try {
clientSocket = serverSocket.accept(); //accept the client connection
inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader); //get the client message
message = bufferedReader.readLine();
System.out.println(message);
//StudyIt
inputStreamReader.close();
clientSocket.close();
} catch (IOException ex) {
System.out.println("Problem in message reading");
}
}
}
}
}
答案 0 :(得分:0)
将while(true)循环检查任何新消息移动到线程中,而不是启动服务器的按钮内的操作。这将使帧停止更新为无法访问的代码。
答案 1 :(得分:-1)
什么是乱码?是否偶然将所有打印为单独的行并且您想要在同一行上打印?那是因为您使用的是System.out.println
而不是System.out.print
。