【问题标题】:JButtons AWT control javaJButtons AWT 控件 java
【发布时间】:2012-12-15 20:06:29
【问题描述】:

我有两个基本问题。

  • 我有一个带有 Java Swing 的 GUI 项目。当我将按钮放在框架上并双击它们时,我有actionPerformed的代码,但它被阻止了。

    我怎样才能放一个按钮然后在actionListener上使用它?

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

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

    类似:


do{
...
}while(thisstring!=null || thisstring!="exit")

所以.. 当我写东西并按下按钮发送它时,它停留在 cicle 和按钮块上。如何解锁按钮以编写其他内容?
编辑:
我了解 EDT 问题,但我无法解决。

我尝试使用 Timer 但没有成功,类似这样:

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

当我按下按钮时如何处理这个来做计时器?

每当一个用户在文本字段中输入内容时,我如何才能留在那个 cicle(阅读评论行)以通过 OutputStream 发送信息? 我知道,例如对于控制台应用程序,我使用 BufferedReader,然后使用 ReadLine() 等待从控制台发送的任何内容,但使用 GUI 界面时,它会一直冻结..

【问题讨论】:

  • “被阻止”和“阻止”是什么意思?没有光学反馈,您按下它并“挂起”?这可能有几个问题,例如通过在 EDT(Event-Dispatcher-Thread)中处理大数据。我们可能需要看一些代码。
  • private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { } 所以..如果我想将它更改为公共,例如,我不能编辑它...在这段代码之后生成了如何将按钮与 actionlister 一起使用?第2题,界面被锁定,因为等待跳出cicle。
  • 1) do/while 似乎阻塞了 EDT。不要阻塞 EDT(事件调度线程)——当这种情况发生时,GUI 将“冻结”。而是实现一个 Swing Timer 用于重复任务或一个 SwingWorker 用于长时间运行的任务。有关详细信息,请参阅Concurrency in Swing。 2) 为了尽快获得更好的帮助,请发布SSCCE。 3) 请停止将代码放入 cmets。这是不可读的。而是将其编辑到问题中。
  • 最后一个添加解决了你的问题还是……离开了吗?

标签: java swing awt jbutton


【解决方案1】:

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

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

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

为避免重复有关该主题的其他讨论,我指示您to this pretty good one。此外,this article 简要概述了 Swing 中的线程概念。

问候,

史考特

【讨论】:

    【解决方案2】:

    根据您的评论,您有一些命名问题。您需要一个实现ActionListener-interface 的类,如下所示:class YourListenerClass implements ActionListener,但您也可以通过像 new ActionListener { 这样的匿名类来实现 公共无效actionPerformed(ActionEvent e){ //你的按钮代码在这里 } }); 当您设置 ActionListener.
    关键是您需要以正确的方式命名您的方法。它必须是public void actionPerformed(ActionEvent e),并且你必须实现ActionListener-interface。 接下来是你必须在你的按钮中注册你的监听器,比如:

    yourButton.addActionListener(new YourListenerClass);
    

    或 插入一个像我之前展示给你的匿名类。

    第二件事听起来像是我在评论中提到的多线程问题。我没有关注 scott 的链接,但根据他的描述,这可能是您想要阅读以解决任何进一步的阻塞问题的来源。
    编辑: 好吧,起初我不想解释它,因为它是一大块代码,但由于问题仍然存在,我想在我的答案中添加一些关于 SwingWorkers 的内容。
    如果您有长时间运行的代码,则使用 Timer 将无济于事,因为它调用的代码也将在 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();
    }
    
    }
    

    如果您想了解有关这些工人的更多信息...有几个线程在处理它,例如this one.

    【讨论】:

      猜你喜欢
      • 2010-11-17
      • 2012-01-21
      • 1970-01-01
      • 1970-01-01
      • 2013-05-30
      • 2017-05-15
      • 1970-01-01
      • 1970-01-01
      • 2011-01-11
      相关资源
      最近更新 更多