Swing - 更新标签

发布于 2024-12-27 20:18:07 字数 418 浏览 1 评论 0原文

我有一个消息标签和一个提交按钮。提交按钮将被按下多次,每次按下的操作最多可能需要一分钟。

当按下按钮时,我想将消息设置为空,任务完成后,我想将消息设置为“完成”。

private void submitActionPerformed(java.awt.event.ActionEvent evt) {
   message = "";
   updateMessageLabel();

   doTheTask();

   /* this update is apply to the label after completion */
   message = "Complete";
}

是否可以在运行 submitActionPerformed() 方法之前(或在方法中)但单击按钮之后更新该消息标签?

I have a message label and a submit button. The submit button will be pressed multiple times, and the action for the each press can take up to a minute.

When the button is pressed, I want to set the message to empty, and after the task is complete, I want to set the message to "Complete".

private void submitActionPerformed(java.awt.event.ActionEvent evt) {
   message = "";
   updateMessageLabel();

   doTheTask();

   /* this update is apply to the label after completion */
   message = "Complete";
}

Is it possible to update that message label before the submitActionPerformed() method is run (or in the method), but after the the button is clicked?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尐籹人 2025-01-03 20:18:07

尽管 Swing 并发教程 已经包含了一些非常好的示例如何处理 Swing 中的并发性,请在下面找到一个示例,其中

  • 包含一个复选框,以证明 UI 仍然存在。
  • 有一个进度条,从 SwingWorker 更新
  • 有一个标签,它获取更新一次SwingWorker 已完成

    导入javax.swing.JCheckBox;
    导入 javax.swing.JFrame;
    导入 javax.swing.JLabel;
    导入 javax.swing.JProgressBar;
    导入 javax.swing.SwingWorker;
    导入 java.awt.BorderLayout;
    导入 java.awt.EventQueue;
    导入 java.lang.reflect.InitationTargetException;
    导入java.util.List;
    导入 java.util.concurrent.ExecutionException;
    
    公共类 SwingWorkerExample {
      私有静态 JProgressBar PROGRESS_BAR;
      私有静态 JLabel OUTPUT_LABEL;
      私有静态 JFrame createGUI(){
        JFrame testFrame = new JFrame( "TestFrame" );
    
        PROGRESS_BAR = new JProgressBar( );
        PROGRESS_BAR.setMinimum( 0 );
        PROGRESS_BAR.setMaximum( 100 );
    
        OUTPUT_LABEL = new JLabel( "处理中" );
    
        testFrame.getContentPane().add( PROGRESS_BAR, BorderLayout.CENTER );
        testFrame.getContentPane().add( OUTPUT_LABEL, BorderLayout.SOUTH );
    
        // 添加一个复选框以证明 UI 仍然具有响应能力
        testFrame.getContentPane().add( new JCheckBox( "点击我来证明 UI 是响应式的" ), BorderLayout.NORTH );
    
    
    
        testFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        返回测试帧;
      }
    
      public static void main( String[] args ) 抛出 InspirationTargetException, InterruptedException {
        EventQueue.invokeAndWait( new Runnable() {
          @覆盖
          公共无效运行(){
            JFrame 框架 = createGUI();
    
            框架.pack();
            框架.setVisible( true );
          }
        } );
        //在 EDT 之外启动 SwingWorker
        MySwingWorker 工人 = new MySwingWorker( PROGRESS_BAR, OUTPUT_LABEL );
        执行();
      }
      私有静态类 MySwingWorker 扩展了 SwingWorker{
        私有最终 JProgressBar fProgressBar;
        私有最终 JLabel fLabel;
        私有 MySwingWorker( JProgressBar aProgressBar, JLabel aLabel ) {
          fProgressBar = aProgressBar;
          fLabel = aLabel;
        }
    
        @覆盖
        protected String doInBackground() 抛出异常 {
          int 最大数量 = 10;
          for( int i = 0; i < maxNumber; i++ ){
            Thread.sleep( 2000 );//模拟长时间运行的进程
            双因子 = ((double)(i+1) / maxNumber);
            System.out.println("中间结果就绪");
            publish(factor);//发布进度
          }
          返回“完成”;
        }
    
        @覆盖
        protected void process( List aDoubles ) {
          //更新进度条完成的百分比
          int amount = fProgressBar.getMaximum() - fProgressBar.getMinimum();
          fProgressBar.setValue( ( int ) (fProgressBar.getMinimum() + ( amount * aDoubles.get( aDoubles.size() - 1 ))) );
        }
    
        @覆盖
        受保护的无效完成(){
          尝试 {
            fLabel.setText( get() );
          } catch (InterruptedException e) {
            e.printStackTrace();
          } catch ( ExecutionException e ) {
            e.printStackTrace();
          }
        }
      }
    }
    

Although the Swing concurrency tutorial already contains some very good samples on how to deal with concurrency in Swing, find below an example which

  • contains a checkbox to prove the UI is still alive
  • has a progress bar, which gets updated from the SwingWorker
  • has a label, which gets updated once the SwingWorker is finished

    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JProgressBar;
    import javax.swing.SwingWorker;
    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.lang.reflect.InvocationTargetException;
    import java.util.List;
    import java.util.concurrent.ExecutionException;
    
    public class SwingWorkerExample {
      private static JProgressBar PROGRESS_BAR;
      private static JLabel OUTPUT_LABEL;
      private static JFrame createGUI(){
        JFrame testFrame = new JFrame( "TestFrame" );
    
        PROGRESS_BAR = new JProgressBar(  );
        PROGRESS_BAR.setMinimum( 0 );
        PROGRESS_BAR.setMaximum( 100 );
    
        OUTPUT_LABEL = new JLabel( "Processing" );
    
        testFrame.getContentPane().add( PROGRESS_BAR, BorderLayout.CENTER );
        testFrame.getContentPane().add( OUTPUT_LABEL, BorderLayout.SOUTH );
    
        //add a checkbox as well to proof the UI is still responsive
        testFrame.getContentPane().add( new JCheckBox( "Click me to proof UI is responsive" ), BorderLayout.NORTH );
    
    
    
        testFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        return testFrame;
      }
    
      public static void main( String[] args ) throws InvocationTargetException, InterruptedException {
        EventQueue.invokeAndWait( new Runnable() {
          @Override
          public void run() {
            JFrame frame = createGUI();
    
            frame.pack();
            frame.setVisible( true );
          }
        } );
        //start the SwingWorker outside the EDT
        MySwingWorker worker = new MySwingWorker( PROGRESS_BAR, OUTPUT_LABEL );
        worker.execute();
      }
      private static class MySwingWorker extends SwingWorker<String, Double>{
        private final JProgressBar fProgressBar;
        private final JLabel fLabel;
        private MySwingWorker( JProgressBar aProgressBar, JLabel aLabel ) {
          fProgressBar = aProgressBar;
          fLabel = aLabel;
        }
    
        @Override
        protected String doInBackground() throws Exception {
          int maxNumber = 10;
          for( int i = 0; i < maxNumber; i++ ){
            Thread.sleep( 2000 );//simulate long running process
            double factor = ((double)(i+1) / maxNumber);
            System.out.println("Intermediate results ready");
            publish( factor );//publish the progress
          }
          return "Finished";
        }
    
        @Override
        protected void process( List<Double> aDoubles ) {
          //update the percentage of the progress bar that is done
          int amount = fProgressBar.getMaximum() - fProgressBar.getMinimum();
          fProgressBar.setValue( ( int ) (fProgressBar.getMinimum() + ( amount * aDoubles.get( aDoubles.size() - 1 ))) );
        }
    
        @Override
        protected void done() {
          try {
            fLabel.setText( get() );
          } catch ( InterruptedException e ) {
            e.printStackTrace();
          } catch ( ExecutionException e ) {
            e.printStackTrace();
          }
        }
      }
    }
    
新一帅帅 2025-01-03 20:18:07

是的,您可以使用 SwingWorker 线程执行此操作,在 execute() 方法中执行所有预 submitActionPerformed() 活动,例如更新标签currentThread 并使用工作线程 Thread 调用 doTheTask() 作为后台作业。

我建议您阅读此文档以参考有关 SwingWorker 线程

Yes you can do this using SwingWorker thread, do all the pre submitActionPerformed() activities like updating the label, in the execute() method of the currentThread and call doTheTask() as a background job using worker Thread.

I suggest you to go through this documentation for reference about SwingWorker Thread

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文