JButtons AWT控制java

JButtons AWT控制java,java,swing,awt,jbutton,Java,Swing,Awt,Jbutton,我有两个基本问题 我有一个带有JavaSwing的GUI项目。当我在框架上放置按钮并双击它们时,我执行了操作的代码,但它被阻止了 如何在那里放置一个按钮,然后在actionListener上使用它 我的项目是关于服务器客户端(多线程和套接字) 我调用一个方法来重新接收一个字符串,我们可以在JtextField上写入该字符串,它会在PrintWriter和getOutputStream上停留一段时间 比如: 所以。。当我写东西并按下按钮发送时,它会停留在cicle上,按钮会阻塞。我怎样才能解

我有两个基本问题

  • 我有一个带有JavaSwing的GUI项目。当我在框架上放置按钮并双击它们时,我执行了
    操作的代码,但它被阻止了

    如何在那里放置一个按钮,然后在
    actionListener
    上使用它

  • 我的项目是关于服务器客户端(多线程和套接字)

    我调用一个方法来重新接收一个字符串,我们可以在JtextField上写入该字符串,它会在PrintWriter和getOutputStream上停留一段时间

    比如:


所以。。当我写东西并按下按钮发送时,它会停留在cicle上,按钮会阻塞。我怎样才能解锁该按钮以写入其他内容?
编辑:
我理解EDT的问题,但我不能解决它

我尝试使用计时器,但没有成功,类似于:

  int delay = 1000; //milliseconds
  ActionListener listener = new ActionListener() {
  public void actionPerformed(ActionEvent e) {
  //My action calling the Thread class with the while cicle that has the PrintWriter
       }

  };
new Timer(delay, listener).start();
当我按下按钮时,我如何处理这个来计时

每次有一个用户在文本字段中输入内容时,我如何保持该循环(阅读注释行)以通过OutputStream发送信息?
我知道,例如,对于控制台应用程序,我使用BufferedReader,然后使用ReadLine()等待从控制台发送的任何内容,但是对于GUI界面,它会一直冻结。

Java GUI开发中有一个基本概念,即开发人员在哪个线程中实现用户交互处理,例如按钮单击

简而言之,您需要在调用操作处理方法的线程之外执行处理。这个线程称为事件调度线程(EDT),如果您的逻辑运行时间远远超过几毫秒,它将阻止UI继续绘制按钮释放等内容

您需要将长期运行的套接字代码从EDT中移出。这样做将允许按钮释放,并允许用户与其他控件(甚至同一按钮)交互

为了避免重复关于这个主题的其他讨论,我指示大家。此外,还简要概述了Swing中的线程概念

问候,


ScottH

Java GUI开发中有一个基本概念,即开发人员在哪个线程中实现用户交互处理,如按钮单击

简而言之,您需要在调用操作处理方法的线程之外执行处理。这个线程称为事件调度线程(EDT),如果您的逻辑运行时间远远超过几毫秒,它将阻止UI继续绘制按钮释放等内容

您需要将长期运行的套接字代码从EDT中移出。这样做将允许按钮释放,并允许用户与其他控件(甚至同一按钮)交互

为了避免重复关于这个主题的其他讨论,我指示大家。此外,还简要概述了Swing中的线程概念

问候,


ScottH

根据您的评论,您有一些命名问题。您需要一个实现
ActionListener
-接口的类,如:
类YourListenerClass实现ActionListener
,但您也可以通过一个匿名类,如new ActionListener来实现{ 已执行的公共无效操作(操作事件e){ //你的按钮代码在这里 } }); 当您设置
ActionListener

关键的是,您需要以正确的方式命名您的方法。它必须是
public void actionPerformed(ActionEvent e)
,并且您必须实现
ActionListener
-接口。 接下来,您必须在按钮中注册侦听器,如:

yourButton.addActionListener(new YourListenerClass);
或 插入一个匿名类,就像我之前向您展示的那样

第二件事听起来像我在评论中提到的多线程问题。我没有关注scotth的链接,但根据他的描述,这可能是您想要阅读以解决任何进一步阻塞问题的来源。
编辑: 好的,起初我不想解释它,因为它是一个相当大的代码块,但由于问题仍然存在,我想在我的回答中添加一些关于SwingWorker的内容。
如果您有长时间运行的代码,则使用
计时器将无济于事,因为它调用的代码也将在EDT上,因为它是由事件触发的。
相反,您可以使用
SwingWorker
来解决此问题。不过,这需要一些额外的代码。 这里有一个简单的方法可以遵循:

public class WorkingHard{
  SwingWorker<String, String> worker;
  JButton yourButton = ...;
  ...
  //do some cool stuff, as register those listeners!
  ...

  public void actionPerformed(ActionEvent evt){
      if(evt.getSource().equals(yourButton);
      // Construct a new SwingWorker
      worker = new SwingWorker<String, Void>(){


    @Override
    protected String doInBackground(){
      //do your reading in this method, it will be executed in an own thread
      String readText = "i will be read".
      /*your reading algorithm, you could also call publish(...) to post your results,
      e.g. likewise), then you also have to override process(...). this process will be
      thread save, too*/
      readText += ... ;

      ...
      return readText;
    }

    @Override
    protected void done(){
      try {
        //do sth. with your result, now thread safe.
        someGuiElement.setText(get());
      } catch (InterruptedException e) {
        e.printStackTrace();
      } catch (ExecutionException e) {
        e.printStackTrace();
      }
    }
  };
  // Execute the SwingWorker; the GUI will not freeze
  worker.execute();
}

}
公共类努力工作{
摇摆工人;
JButton yourButton=。。。;
...
//做一些很酷的事情,就像注册那些听众一样!
...
已执行的公共无效操作(操作事件evt){
如果(evt.getSource().equals)(yourButton);
//构造一个新的SwingWorker
工人=新的SwingWorker(){
@凌驾
受保护字符串doInBackground(){
//如果您阅读此方法,它将在自己的线程中执行
String readText=“我将被读取”。
/*您的阅读算法,您也可以调用publish(…)发布您的结果,
e、 同样地),然后您还必须重写进程(…)。此进程将
线程保存*/
readText+=;
...
返回readText;
}
@凌驾
受保护的void done(){
试一试{
//用你的结果做某事,现在线程安全了。
setText(get());
}捕捉(中断异常e){
e、 printStackTrace();
}捕获(执行例外){
e、 printStackTrace();
}
}
};
//执行SwingWorker;GUI不会冻结
worker.execute();
}
}

如果你想更多地了解那些工人。。。有几个线程处理它,例如…

根据您的评论,您有一些命名问题。您需要一个实现
ActionListenerpublic class WorkingHard{
  SwingWorker<String, String> worker;
  JButton yourButton = ...;
  ...
  //do some cool stuff, as register those listeners!
  ...

  public void actionPerformed(ActionEvent evt){
      if(evt.getSource().equals(yourButton);
      // Construct a new SwingWorker
      worker = new SwingWorker<String, Void>(){


    @Override
    protected String doInBackground(){
      //do your reading in this method, it will be executed in an own thread
      String readText = "i will be read".
      /*your reading algorithm, you could also call publish(...) to post your results,
      e.g. likewise), then you also have to override process(...). this process will be
      thread save, too*/
      readText += ... ;

      ...
      return readText;
    }

    @Override
    protected void done(){
      try {
        //do sth. with your result, now thread safe.
        someGuiElement.setText(get());
      } catch (InterruptedException e) {
        e.printStackTrace();
      } catch (ExecutionException e) {
        e.printStackTrace();
      }
    }
  };
  // Execute the SwingWorker; the GUI will not freeze
  worker.execute();
}

}