简体   繁体   English

如何使用JProgressBar显示我的应用程序延迟?

[英]How show my application delay with JProgressBar?

I wrote a simple application and I want show delay of it with JProgressBar Plese help me ; 我写了一个简单的应用程序,我想用JProgressBar Plese显示它的延迟;

I want show JProgressBar with Joptionpane , with a cancel button and it should be modal 我想用Joptionpane显示JProgressBar,并带有一个取消按钮,它应该是模态的

this is my source code : 这是我的源代码:

class CustomFrame extends JFrame {

  private JProgressBar progressBar;

  public CustomFrame() {
    long start = System.currentTimeMillis();
    myMethod();
    this.getContentPane().setLayout(null);
    this.setSize(200, 200);

    //JOptionPane. ?????

    this.setTitle("JFrame");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
       long end = System.currentTimeMillis();
        System.out.print("\nTime: " + (end - start));
  }
    public void myMethod(){
        try {
                     java.io.File file = new java.io.File("i://m.txt");
                     BufferedReader input =
                       new BufferedReader(new FileReader(file));
                     String line;
                     while ((line = input.readLine()) != null) {
                         if (line.indexOf("CREATE KGCGI=") != -1 ){
                             System.out.println(line);
                         }
                     }
                     input.close();
                   }
                   catch(Exception e){
                       e.printStackTrace();
                   }
    } 

Thanks ... 谢谢 ...

There are a couple things that you will need to do to get this to work: 要使它正常工作,您需要做几件事:

  1. You should be aware of threading issues in Swing. 您应该注意Swing中的线程问题。 Your GUI painting should be done on the EventDispatchThread and disk I/O should be done in a worker thread. GUI绘制应在EventDispatchThread上完成,而磁盘I / O应在辅助线程中完成。 See this tutorial , the SwingWorker JavaDoc , and SwingUtilities.invokeLater for more detail 有关更多详细信息,请参见本教程SwingWorker JavaDocSwingUtilities.invokeLater
  2. You will then want to get the size of your file ( file.length() )to determine how to scope your progress bar ( myProgressBar.setMaximum(length) ) 然后,您将需要获取文件的大小( file.length() ),以确定如何调整进度条的范围( myProgressBar.setMaximum(length)
  3. When you iterate over the lines in your file, you will want to trigger an update to your progress bar ( myProgressBar.setValue(myProgressBar.getValue()+lineLength) ). 遍历文件中的各myProgressBar.setValue(myProgressBar.getValue()+lineLength) ,将需要触发对进度条的更新( myProgressBar.setValue(myProgressBar.getValue()+lineLength) )。

A couple points by way of critique: 批评的几点:

  • your constructor shouldn't go off and do all of your work (ie load your file and pop up an option pane with the ability to cancel. the constructor should just do the work needed to create the object. you might want to consider having your constructor create your class, and then have the work that needs to be done to be called separately, or within something like an init() method. 您的构造函数不应执行所有工作(即加载文件并弹出具有取消功能的选项窗格。构造函数应仅执行创建对象所需的工作。您可能要考虑让您的构造函数创建您的类,然后将需要完成的工作分别调用,或者在类似init()方法的调用中进行。
  • It isn't clear what you are doing with the JFrame as superclass. 不清楚您将JFrame作为超类正在做什么。 JOptionPane is a class that will pop up a very basic modal dialog with some text, maybe an icon or input field. JOptionPane是一个类,它将弹出一个非常基本的模态对话框,其中包含一些文本,可能是图标或输入字段。 It isnt a panel that is embedded in a dialog. 它不是嵌入在对话框中的面板。
  • As JOptionPane is a very basic construct for creating a basic message dialog, it might be easier to use a JDialog , which can also be made modal. 由于JOptionPane是用于创建基本消息对话框的非常基本的构造,因此使用JDialog (也可以使其成为模式)可能会更容易。 JDialog will allow you to add buttons as you please, where as a standalone JOptionPane will require you to use Yes/No, or Yes/No/Cancel or OK/Cancel etc. JDialog允许您JDialog添加按钮,而作为独立JOptionPane则要求您使用“是/否”或“是/否/取消”或“确定/取消”等。
  • If you still want to use JOptionPane , and only show a cancel button, you can instantiate a JOptionPane (as opposed to using the utility show* methods), with the progressbar as the message , and the JOptionPane.CANCEL_OPTION as the optionType param. 如果仍要使用JOptionPane ,并且仅显示取消按钮,则可以实例化JOptionPane (与使用实用程序show*方法相对),并使用进度条作为message ,并将JOptionPane.CANCEL_OPTION用作optionType参数。 You will still need to put this into a JDialog to make it visible. 您仍然需要将其放入JDialog以使其可见。 See this tutorial for more details: 有关更多详细信息,请参见本教程

JOptionPane (constructor) JOptionPane(构造函数)

Creates a JOptionPane with the specified buttons, icons, message, title, and so on. 使用指定的按钮,图标,消息,标题等创建JOptionPane。 You must then add the option pane to a JDialog, register a property-change listener on the option pane, and show the dialog. 然后,您必须将选项窗格添加到JDialog,在选项窗格上注册属性更改侦听器,然后显示对话框。 See Stopping Automatic Dialog Closing for details. 有关详细信息,请参见停止自动关闭对话框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM