简体   繁体   中英

How to Use JProgressBar on Button Click

In gui there are two fields which add two numbers on button click and there is code logic to check if the fields are empty but what i want is when user click on the button it should display a progress bar according to milliseconds progress should go from 1 to 100 then it complete the task and auto reset the progress bar.

public class Main {

    public static void main(String[] args) {

        Progress pg = new Progress();


    }

}


public class Progress extends JFrame implements ActionListener {

    private JTextField t1;
    private JTextField t2;
    private JTextField t3;
    private JButton b1;
    private JProgressBar bar;
    Timer t;
    int interval = 1000;
    int i = 0;

    public Progress() {
        setLayout(new FlowLayout());

        t1 = new JTextField(20);
        t2 = new JTextField(20);
        t3 = new JTextField(20);

        b1 = new JButton("OK");
        b1.addActionListener(this);

        t = new Timer(interval, this);

        bar = new JProgressBar();
        bar.setStringPainted(true);
        bar.setValue(0);

        add(t1);
        add(t2);
        add(t3);
        add(b1);
        add(bar);

        setSize(600, 480);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == b1) {

            if (t1.getText().isEmpty() || t2.getText().isEmpty()) {

                JOptionPane.showMessageDialog(null, "Empty Fields");
            }

            else {
                int w = Integer.parseInt(t1.getText());
                int x = Integer.parseInt(t2.getText());
                int res = w + x;
                t3.setText("" + res);

            }

        }

    }

}
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class Progress extends JFrame implements ActionListener {

    private JTextField t1;
    private JTextField t2;
    private JTextField t3;
    private JButton b1;
    JProgressBar progressBar;
    int i = 0;

    public Progress() {

        setLayout(new FlowLayout());

        t1 = new JTextField(20);
        t2 = new JTextField(20);
        t3 = new JTextField(20);

        b1 = new JButton("OK");
        b1.addActionListener(this);

        progressBar = new JProgressBar(1, 100);
        progressBar.setValue(0);
        progressBar.setStringPainted(true);
        progressBar.setForeground(Color.GRAY);
        progressBar.setBackground(Color.white);

        add(t1);
        add(t2);
        add(t3);
        add(b1);
        add(progressBar);

        setSize(600, 480);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        int i = 0;
        if (e.getSource() == b1) {
            progressBar.setVisible(true);
            if (t1.getText().isEmpty() || t2.getText().isEmpty()) {
                JOptionPane.showMessageDialog(null, "Empty Fields");
                System.exit(0);
            } else {
                try {
                    while (i <= 100) {

                        int w = Integer.parseInt(t1.getText());
                        int x = Integer.parseInt(t2.getText());
                        int res = w + x;
                        t3.setText("" + res);

                        Thread.sleep(50);
                        progressBar.paintImmediately(0, 0, 200, 200);
                        progressBar.setValue(i);
                        i++;
                    }
                } catch (Exception e1) {
                    System.out.print("Exception =" + e1);
                }
                progressBar.setValue(0);
            }
        }

    }

    public static void main(String[] args) {
        Progress p = new Progress();
        p.setVisible(true);

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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