- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我按照教程创建了一个信使。我正确地输入了代码,并且我对所有内容的作用有相当基本的了解。
尽管如此,我最终并没有得到相同的结果。代码如下:
<小时/>public class Server extends JFrame{
private JTextField userText;
private JTextArea chatWindow;
private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
private Socket connection;
//constructor
public Server(){
super("Coffee Messenger");
userText = new JTextField();
userText.setEditable(false);
userText.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
sendMessage(event.getActionCommand());
userText.setText("");
}
}
);
add(userText, BorderLayout.SOUTH);
chatWindow = new JTextArea();
add(new JScrollPane());
setSize(300,150);
setVisible(true);
}
//set up and run the server
public void startRunning(){
try{
server = new ServerSocket(6789, 100);
while(true){
try{
//connect and have conversation
waitForConnection();
setupStreams();
whileChatting();
}catch(EOFException eofException){
showMessage("\n Server ended the connection!");
}finally{
closeCrap();
}
}
}catch(IOException ioException){
ioException.printStackTrace();
}
}
//wait for connection, then display connection information
private void waitForConnection() throws IOException{
showMessage("Waiting for someone to connect...\n");
connection = server.accept();
showMessage("Now connected to " + connection.getInetAddress().getHostName());
}
//get stream to send and receive data
private void setupStreams() throws IOException{
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
showMessage("\n Streams are now setup! \n");
}
//during the chat conversation
private void whileChatting() throws IOException{
String message = "You are now connected!";
sendMessage(message);
ableToType(true);
do{
//have conversation
try{
message = (String) input.readObject();
showMessage("\n " + message);
}catch(ClassNotFoundException classNotFoundException){
showMessage("\n idk wtf that user sent");
}
}while(!message.equals("CLIENT - END"));
}
//close streams and sockets (application)
private void closeCrap(){
showMessage("\n Closing connections...\n");
ableToType(false);
try{
output.close();
input.close();
connection.close();
}catch(IOException ioException){
ioException.printStackTrace();
}
}
//send message to client
private void sendMessage(String message){
try{
output.writeObject("SERVER - " + message);
output.flush();
showMessage("\nSERVER - " + message);
}catch(IOException ioException){
chatWindow.append("\n ERROR: Cannot send message.");
}
}
//updates chatWindow
private void showMessage(final String text){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
chatWindow.append(text);
}
}
);
}
//sets the ability to edit the textfield
private void ableToType(final boolean tof){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
userText.setEditable(tof);
}
}
);
}
}
__
<小时/>
当我从主方法启动应用程序时,字符串“等待某人连接”(来自 waitForConnection 方法)不会显示。我相信问题出在 showMessage 方法中。难道是我用错了?如果我用简单的 system.out.println(); 替换 invokeLater 方法,项目将完全按计划运行。
抱歉,我有点缺乏经验,所以这可能非常简单。预先非常感谢您。
(感谢 thenewboston 制作这些教程)
最佳答案
这里:
chatWindow = new JTextArea();
add(new JScrollPane());
您的 GUI 正在创建一个 JTextArea、chatWindow,但将其添加到没有显示它的内容中,而您的 GUI 显示了一个空的 JScrollPane。如果有就更好了
chatWindow = new JTextArea();
add(new JScrollPane(chatWindow));
这样发送到 JTextArea 的文本就有机会被显示。
关于Java - invokeLater 将不会运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24717853/
有人可以强调这两者之间的区别以及两者都是必需的实例吗?! 我有一个可以互换使用的应用程序,但想知道一个是否比另一个更好。显然它们都接受 Runnable object,所以对我来说 - 我想我可以使用
EventQueue.invokeLater() 和 SwingUtilities.invokeLater() 有什么不同吗? 或者是后者只是为了设计而建立在前者之上(没有修改)? 最佳答案 没有没有
在我的方法 doWork() 中,我调用 SwingUtilities.invokeLater(new Runnable(){ public void run() { button.setBackgr
我怎么感觉SwingUtilities.invokeLater()在任何swing应用中的必要性。请给出一些代码示例。 最佳答案 每当您需要更新 GUI 中的某些内容时,您应该通过 AWT 事件线程
.addActionListener(new ActionListener(){ public void actionPerformed (ActionEvent e){
我按照教程创建了一个信使。我正确地输入了代码,并且我对所有内容的作用有相当基本的了解。 尽管如此,我最终并没有得到相同的结果。代码如下: public class Server extends JFr
是否有更优雅的方式来完成我在下面所做的事情?也就是说,是否有比轮询和 hibernate 、轮询和 hibernate 等更优雅的方式来知道何时 Runnable.run()方法已通过 invokeL
我在使用 invokeLater 更新 JLabel 值时遇到问题。我在运行 invokeLater 的主函数之外有一个单独的方法,但是当我单击搜索按钮更新值时,它不会在同一个 gui 实例中更新。我
我使用推荐的代码启动我的交互式程序,它使用 Swing。 : public static void main(String[] args) { javax.swing.Swin
EventQueue javadoc 声明要求排队的事件按顺序和顺序分派(dispatch)。 与 EventQueue.invokeLater 一起排队的 Runnable 保证在后续用户事件(例如
有时我觉得使用 AsyncTask 对任务来说太过分了,我正在寻找类似的功能,如 Android 中的 SwingUtilities.invokeLater。因为我只想执行一行代码,没有必要为此创建一
我的问题与 SwingUtilities.invokeLater 有关。我应该什么时候使用它?每次需要更新 GUI 组件时都必须使用吗?它究竟是做什么的?是否有替代方案,因为它听起来不直观并且添加了看
我有一个问题,关于使用 SwingUtilities 的 invokeLater 方法的正确做法是什么。 因此,首先,我想确认我的理解是否正确。 据我所知,必须在 EDT 上更改 GUI,因为 Swi
我有一个线程使用 SwingUtilities.invokeLater 在我的 JFrame 上显示更新。线程的速度是可调的,当它设置为全速(更新之间没有 sleep )时,我的程序会严重减慢。我想问
我从来不理解SwingUtilities.invokeLater,这就是我一直避免使用它直到现在的原因。但是确保 Swing Thread 安全非常重要。好吧,请原谅,因为这是我第一次这样做。我试图在
这个问题已经有答案了: What does SwingUtilities.invokeLater do? [duplicate] (5 个回答) SwingUtilities.invokeLater
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 6 年前。 Improve th
我正在使用 invokeLater() 在 JEditPane 中加载 html 页面,该 JEditPane 位于 JPanel 中,而 JPanel 位于 JTabbedPane 中。 我的所有方
I had a problem earlier我试图将对象添加到 Canvas 中,但 SwingUtilities.invokeLater 尚未真正完成其工作。我确实需要能够可靠地向此 JPanel
我正在尝试使用多线程模拟一个简单的恒温器。恒温器应提高温度以达到用户请求的值,即下面代码中的“最大值”。我有两个线程,一个负责提高温度,另一个负责降低温度。但减少的条件是必须在关气时才运行。但我在实现
我是一名优秀的程序员,十分优秀!