在JFrame上完成每个方法后写一个字符串

时间:2014-01-07 20:14:24

标签: java swing jframe jlabel

最近我制作了一个控制台程序,为我更新插件。我认为如果我将它从控制台程序转换为JFrame / UI会更好看。我几乎是一个初学者,但我似乎没有找到任何地方,我可以在每个方法完成后如何向JFrame / Panel添加文本。有点像进度条,但只是文字描述了“引擎盖下”正在发生的事情。没有什么花哨的东西,但至少比我运行我的罐子更具视觉效果,我所看到的只是一个窗口加载环旋转几秒钟。

public static void main(String[] args) {
    JFrame frame = new JFrame("Updater");

    JLabel label1=new JLabel("Testprogram 1",JLabel.LEFT);
    JLabel label2=new JLabel("Method 1..",JLabel.LEFT);
    JLabel label3=new JLabel("Method 3..",JLabel.LEFT);
    JLabel label4=new JLabel("Method 4...",JLabel.LEFT);
    JLabel label5=new JLabel("Done!",JLabel.LEFT);

    JPanel panel=new JPanel();

    panel.setLayout(new GridLayout(5,1));

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300,300);
    frame.setLocationRelativeTo(null);
    frame.add(panel,BorderLayout.CENTER);
    panel.setBackground(Color.WHITE);
    frame.setResizable(false);
    frame.setVisible(true);

    panel.add(label1);
    panel.add(label2);
    panel.add(label3);
    panel.add(label4);
    panel.add(label5);

    CreateFrame();
    Start();
    CreateDir();
    LinkList();
    Download();
    unzip();
    DeleteZip();

}

我尝试在每种方法之后添加标签,但我想它不会像那样工作,除非它实时“更新”。我希望有人理解我想要实现的目标。

1 个答案:

答案 0 :(得分:3)

我怀疑你有一堆长时间运行的代码,它们会阻止它绘制并与用户交互。如果是这样,您需要阅读Concurrency in Swing以了解为什么您的程序没有按照您的预期行事。


请注意,dani-h的示例会冒Swing线程问题,因为他在调用Swing代码的同一线程上调用Thread.sleep(...)。如果他正确地在Swing事件线程上启动了他的Swing GUI,或者如果你试图在任何其他Swing程序中使用他的代码,它将会锁定,你最终会遇到同样的问题。

以下是使用dani-h示例的更正代码,但使用SwingWorker纠正了其线程问题:

import java.awt.Dimension;
import java.awt.Window;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;

@SuppressWarnings("serial")
public class Example2 extends JPanel {
   private static final int PREF_W = 400;
   private static final int PREF_H = 200;
   private JLabel label = new JLabel();

   public Example2() {
      add(label);
      final MySwingWorker mySwingWorker = new MySwingWorker();
      mySwingWorker.addPropertyChangeListener(new PropertyChangeListener() {

         @Override
         public void propertyChange(PropertyChangeEvent pcEvt) {
            if (MySwingWorker.STEP_PERFORMING.equals(pcEvt.getPropertyName())) {
               label.setText(mySwingWorker.getStepPerforming());
            } else if (pcEvt.getNewValue() == SwingWorker.StateValue.DONE) {
               Window win = SwingUtilities.getWindowAncestor(Example2.this);
               win.dispose();
            }
         }
      });
      mySwingWorker.execute();
   }

   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   private static void createAndShowGUI() {
      Example2 example = new Example2();

      JFrame frame = new JFrame("Example 2");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(example);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGUI();
         }
      });
   }
}

class MySwingWorker extends SwingWorker<Void, Void> { 
   public static final String STEP_PERFORMING = "step performing";
   private static final long SLEEP_DELAY = 1500;
   private String stepPerforming = "";

   @Override
   protected Void doInBackground() throws Exception {
      start();
      createDir();
      linkList();
      download();
      unzip();
      deletezip();
      return null;
   }


   public String getStepPerforming() {
      return stepPerforming;
   }


   public void setStepPerforming(String stepPerforming) {
      String oldValue = this.stepPerforming;
      String newValue = stepPerforming;
      this.stepPerforming = stepPerforming;
      firePropertyChange(STEP_PERFORMING, oldValue, newValue);
   }

   private void start() {
      setStepPerforming("Starting...");
      // Start();
      try {
         Thread.sleep(SLEEP_DELAY);
      } catch (InterruptedException e) {}
   }

   private void createDir() {
      setStepPerforming("Executing Create Directory");
      // CreateDir();
      try {
         Thread.sleep(SLEEP_DELAY);
      } catch (InterruptedException e) {}
   }

   private void linkList() {
      setStepPerforming("Executing Link List");
      // LinkList();
      try {
         Thread.sleep(SLEEP_DELAY);
      } catch (InterruptedException e) {}
   }

   private void download() {
      setStepPerforming("Executing Download");
      // Download();
      try {
         Thread.sleep(SLEEP_DELAY);
      } catch (InterruptedException e) {}
   }

   private void unzip() {
      setStepPerforming("Executing Unzip");
      // unzip();
      try {
         Thread.sleep(SLEEP_DELAY);
      } catch (InterruptedException e) {}
   }

   private void deletezip() {
      setStepPerforming("Executing Delete zip");
      // DeleteZip();
      try {
         Thread.sleep(SLEEP_DELAY);
      } catch (InterruptedException e) {}
   }

}