jprogressbar 可见并且正在单击按钮

发布于 2024-10-12 06:53:56 字数 812 浏览 6 评论 0原文

public NewJDialog(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
  jProgressBar1.setVisible(false);

}  

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                     
    jButton1.setEnabled(false);
    jProgressBar1.setVisible(true);        
   repaint();            
   for(int i=0;i<=100;i+=5){
          jProgressBar1.setValue(i);
         // jProgressBar1.setIndeterminate(false);              
          try{
              jProgressBar1.paintImmediately(0, 0, 100, 100);//0, 1, 100, 10
          Thread.sleep(100);
          jProgressBar1.setStringPainted(true);

     }catch(Exception e){}
    }

我使用上面的代码在 JDialog 中使用 Jprogressbar。如果我使用这个,我可以在完成其过程(100%)后看到一个进度条,而且我不想在按钮单击之前显示进度条。

public NewJDialog(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
  jProgressBar1.setVisible(false);

}  

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                     
    jButton1.setEnabled(false);
    jProgressBar1.setVisible(true);        
   repaint();            
   for(int i=0;i<=100;i+=5){
          jProgressBar1.setValue(i);
         // jProgressBar1.setIndeterminate(false);              
          try{
              jProgressBar1.paintImmediately(0, 0, 100, 100);//0, 1, 100, 10
          Thread.sleep(100);
          jProgressBar1.setStringPainted(true);

     }catch(Exception e){}
    }

I use above code for using a Jprogressbar in a JDialog. If I use this I can see a progressbar after completing its process(100 %) and also I don't want to show the progressbar upto buttonclick.

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

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

发布评论

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

评论(2

不疑不惑不回忆 2024-10-19 06:53:56
  1. 使用单独的线程来运行您的任务。进度条没有机会在 GUI 线程上绘制。您需要像 SwingWorker 这样的东西来帮助您。
  2. 单击按钮后将进度条添加到 GUI。
  3. 请参阅 Java 教程,了解如何设置起来吧。
  1. Use a separate Thread for running your task. The progress bar doesn't have a chance to get painted on the GUI thread. You need something like SwingWorker to help you out.
  2. Add the progress bar to the GUI after the button click.
  3. Refer to the Java tutorial for a walk through on how to set it up.
空城缀染半城烟沙 2024-10-19 06:53:56
//Simplest way of using JProgressBar

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
class logindemo extends JFrame implements ActionListener
   {
      JProgressBar pb;
      JButton b1;
          logindemo()
          {
             super("LOGIN FORM");
             setLayout(null);
             b1=new JButton("LOGIN");
             b1.setBackground(Color.yellow);             
             pb=new JProgressBar(1,100);
             pb.setValue(0);
             pb.setStringPainted(true);
             pb.setForeground(Color.red);   
             pb.setBackground(Color.white); 
             b1.setBounds(20,20,80,25);pb.setBounds(110,20,200,25);
             pb.setVisible(false);
             add(b1);
             add(pb);             
             b1.addActionListener(this);
             setResizable(false);
             setDefaultCloseOperation(EXIT_ON_CLOSE);
          }
           public void actionPerformed(ActionEvent e)
           {
              int i=0;
              if(e.getSource()==b1)
                 {
                   pb.setVisible(true);
                try
                {
                   while(i<=100)
                   {
                    Thread.sleep(50);
    pb.paintImmediately(0, 0, 200, 25);
        pb.setValue(i);
                     i++;
    }
                 }
                 catch(Exception e1)
                 {
    System.out.print("Caughted exception is ="+e1);
                 }
                }
           }
          public static void main(String arg[])
          {
          logindemo m=new logindemo();
          m.setSize(330,100);
          m.setVisible(true);
          Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
          int x = (int) ((dimension.getWidth() - m.getWidth()) / 2);
          int y = (int) ((dimension.getHeight() - m.getHeight()) / 2);
          m.setLocation(x, y); 
          }
   }

/* 
By 
Dr. Amit Kumar Kapoor
Assistant Professor, Maharaja Agrasen Institute of Management & Technology, Jagadhri
E-mail ID: - [email protected]
*/
//Simplest way of using JProgressBar

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
class logindemo extends JFrame implements ActionListener
   {
      JProgressBar pb;
      JButton b1;
          logindemo()
          {
             super("LOGIN FORM");
             setLayout(null);
             b1=new JButton("LOGIN");
             b1.setBackground(Color.yellow);             
             pb=new JProgressBar(1,100);
             pb.setValue(0);
             pb.setStringPainted(true);
             pb.setForeground(Color.red);   
             pb.setBackground(Color.white); 
             b1.setBounds(20,20,80,25);pb.setBounds(110,20,200,25);
             pb.setVisible(false);
             add(b1);
             add(pb);             
             b1.addActionListener(this);
             setResizable(false);
             setDefaultCloseOperation(EXIT_ON_CLOSE);
          }
           public void actionPerformed(ActionEvent e)
           {
              int i=0;
              if(e.getSource()==b1)
                 {
                   pb.setVisible(true);
                try
                {
                   while(i<=100)
                   {
                    Thread.sleep(50);
    pb.paintImmediately(0, 0, 200, 25);
        pb.setValue(i);
                     i++;
    }
                 }
                 catch(Exception e1)
                 {
    System.out.print("Caughted exception is ="+e1);
                 }
                }
           }
          public static void main(String arg[])
          {
          logindemo m=new logindemo();
          m.setSize(330,100);
          m.setVisible(true);
          Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
          int x = (int) ((dimension.getWidth() - m.getWidth()) / 2);
          int y = (int) ((dimension.getHeight() - m.getHeight()) / 2);
          m.setLocation(x, y); 
          }
   }

/* 
By 
Dr. Amit Kumar Kapoor
Assistant Professor, Maharaja Agrasen Institute of Management & Technology, Jagadhri
E-mail ID: - [email protected]
*/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文