简体   繁体   English

如何在按钮单击上使用JProgressBar

[英]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. 在gui中,有两个字段在单击按钮时添加了两个数字,并且有代码逻辑来检查字段是否为空,但是我想要的是当用户单击按钮时,它应根据毫秒显示进度条,进度应从1开始到100,然后完成任务并自动重置进度栏。

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);

    }

}

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

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