⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 progressbardemo.java

📁 java netbeans 学习程序合集
💻 JAVA
字号:
/*
 * ProgressBarDemo.java
 *
 * Created on 2008年1月10日, 下午3:54
 */

package org.Adam;

/**
 *
 * @author  Administrator
 */
public class ProgressBarDemo extends javax.swing.JFrame implements Runnable{
    Thread t;
    boolean a=false;
    int textLocation=0;
    String tt="abcabcabcabc";
    /** Creates new form ProgressBarDemo */
    public ProgressBarDemo() {
        initComponents();
        this.setLocationRelativeTo(this);
       // init();
    }
    /*
    private void init(){
        while(true){
            if(this.jProgressBar1.getValue()<this.jProgressBar1.getMaximum()){
            this.jProgressBar1.setValue(this.jProgressBar1.getValue()+1);
            }
        }
        
    }*/
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jProgressBar1 = new javax.swing.JProgressBar();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("Start");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(77, 77, 77)
                        .add(jButton1))
                    .add(layout.createSequentialGroup()
                        .add(41, 41, 41)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(jScrollPane1)
                            .add(jProgressBar1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE))))
                .addContainerGap(55, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 249, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(61, 61, 61)
                .add(jProgressBar1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 24, Short.MAX_VALUE)
                .add(jButton1)
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO 将在此处添加您的处理代码:
     if(a==false){
         a=true;
         this.jProgressBar1.setValue(0);
         this.jTextArea1.setText("");
         this.jButton1.setText("Stop");
     }else{
         a=false;
         this.jButton1.setText("Start");
     }
     
    t=new Thread(this);
        t.start();
    }//GEN-LAST:event_jButton1ActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ProgressBarDemo().setVisible(true);
            }
        });
    }

    public void run() {
        while(true){
            if(a==true){
                if(this.jProgressBar1.getValue()<this.jProgressBar1.getMaximum()){
                    this.jProgressBar1.setValue(this.jProgressBar1.getValue()+1);
                    this.jTextArea1.append(tt.substring(textLocation,textLocation+1));
                     textLocation++;
                     if(textLocation==tt.length()){
                         textLocation=0;
                     }                    
                }else{
                    a=false;
                }try {
                t.sleep(50);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            }
           
        }
    }
    
    // 变量声明 - 不进行修改//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    // 变量声明结束//GEN-END:variables
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -