简体   繁体   中英

I need help on a few things on Java JFrame

I have created this Progress Code and now I want it to close automatically after going to 100% and also open a new Frame with Text in the frame. So how do I do this?

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
class Progress extends JFrame implements ActionListener {

    JProgressBar pb;
    JButton b1;
    Progress() {
        super("Progress");
        setLayout(null);
        b1 = new JButton("Start");
        b1.setBackground(Color.LIGHT_GRAY);             
        pb = new JProgressBar(1,100);
        pb.setValue(0);
        pb.setStringPainted(true);
        pb.setForeground(Color.green);   
        pb.setBackground(Color.pink); 
        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(400,250);
        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); 
    }
}

This is the code for my Progress bar.

So a thing before the answer.

ProgressBar

In your code while progress is increasing rest of the application is frozen. If you want to avoid it, it's good to have progress bar code in another Thread. But remember to use SwingWorker instead of classic Threads.

Also read the thing what Andrew Thompson mentioned in his comments.

To "close" or rather hide main frame u can call one of these methods

setVisible(false);

or

dispose();

To open new Window/Frame or whatever, you have to put it after the while loop. You can check it with code below, just add this method to your class and call it after the loop.

    private void dialogMessage() {
    Object[] options = {"OK"};
    int result = JOptionPane.showOptionDialog(this,
            "Done!", "",
            JOptionPane.OK_OPTION, JOptionPane.QUESTION_MESSAGE,
            null,
            options,
            options[0]);
    if (result == JOptionPane.OK_OPTION) {
        System.exit(0);
    }
}

To read more about JOptionPane check out this link How to Make Dialogs

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