jprogressbar可见并处理按钮单击

时间:2011-01-14 05:48:16

标签: java swing jprogressbar

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%),而且我也不想在按钮上显示进度条。

2 个答案:

答案 0 :(得分:1)

  1. 使用单独的线程运行任务。进度条没有机会在GUI线程上绘制。你需要像SwingWorker这样的东西来帮助你。
  2. 点击按钮后,将进度条添加到GUI。
  3. 有关如何设置的详细信息,请参阅Java tutorial

答案 1 :(得分:0)

//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
*/