繁体   English   中英

jprogressbar可见并可以单击按钮

[英]jprogressbar visible and working on button click

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%)后可以看到进度条,并且我也不想在buttonclick之前显示进度条。

  1. 使用单独的线程来运行任务。 进度条没有机会在GUI线程上绘制。 您需要像SwingWorker这样的工具来帮助您。
  2. 单击按钮后,将进度条添加到GUI。
  3. 请参阅Java教程以获取有关如何进行设置的逐步指导。
//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: - akbrightfuture@gmail.com
*/

暂无
暂无

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

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