如何在JFrame打开时附加进度条?

时间:2014-05-19 19:42:42

标签: java swing

我想将进度条附加到Frame而不是我当前的测试开始按钮。进度条有效,但我希望它在暂时打开窗口时运行,然后我可以将它附加到我以后想要的任何内容上。

代码:

import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.JProgressBar;
import javax.swing.JButton;

public class ProgressBarWindow extends JFrame {
    private static final long serialVersionUID = 1L;
    private JPanel contentPane;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ProgressBarWindow frame = new ProgressBarWindow();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public ProgressBarWindow() {
        try {
             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
        } catch(Exception e) { }

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 183);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JProgressBar progressBar = new JProgressBar();
        progressBar.setBounds(22, 77, 386, 27);
        contentPane.add(progressBar);

        JButton btnNewButton = new JButton("Cancel");
        btnNewButton.setBounds(319, 111, 89, 23);
        btnNewButton.addMouseListener(new myMouseListener2());
        contentPane.add(btnNewButton);

        JButton btnStart = new JButton("Start");
        btnStart.addActionListener(new btnDoAction(progressBar));
        btnStart.setBounds(220, 111, 89, 23);
        contentPane.add(btnStart);
    }
}

class myClose implements MouseListener {
    myClose() { }

    @Override
    public void mouseClicked(MouseEvent e) {
         final Component source = e.getComponent();
         final JFrame frame = (JFrame) SwingUtilities.getRoot(source);
         frame.dispose();
    }

    @Override
    public void mousePressed(MouseEvent e) {
         final Component source = e.getComponent();
         final JFrame frame = (JFrame) SwingUtilities.getRoot(source);
         frame.dispose();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }
}

class btnDoAction implements ActionListener{
    JProgressBar temp = new JProgressBar();

    btnDoAction(JProgressBar p) {
        this.temp = p;
    }

    public void actionPerformed (ActionEvent e){
        new Thread(new thread1(temp)).start(); //Start the thread
    }
}

class thread1 implements Runnable{
    JProgressBar pBar = new JProgressBar();

    thread1(JProgressBar u) {
        this.pBar = u;
    }

    public void run(){
        for (int i=0; i<=100; i++){ //Progressively increment variable i
            pBar.setValue(i); //Set value
            pBar.repaint(); //Refresh graphics
            try{Thread.sleep(50);} //Sleep 50 milliseconds
            catch (InterruptedException err){}
        }
    }
}

enter image description here

1 个答案:

答案 0 :(得分:3)

如果我理解正确,不要使用JFrame作为基础组件,而是从JPanel扩展您的课程,这样您就可以将其添加到您想要的任何内容中。

然后你可以提供setter和getter方法来调整进度条,但是我可以通过某种接口契约来实现。

  • 不要使用null布局。您无法控制字体,字体指标,渲染管道和其他属性等方面,这些属性会影响空间组件所需的空间量,以便在多个系统(甚至那些运行相同操作系统的系统)中正确呈现。
  • 不要在按钮上使用MouseListener,而是使用ActionListene rs。在使用时点击按钮并按空格/齐纳
  • 时要考虑到
  • 不要从事件调度线程以外的任何线程更新UI。有关详细信息,请参阅Concurrency in Swing。考虑使用SwingWorker,它具有将后台线程与EDT同步并在内置进度支持中的功能

<强>更新

enter image description here

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class LayoutExample {

    public static void main(String[] args) {
        new LayoutExample();
    }

    public LayoutExample() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new ProgressPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ProgressPane extends JPanel {

        private JProgressBar pb;
        private JButton start;
        private JButton cancel;

        public ProgressPane() {

            pb = new JProgressBar();
            start = new JButton("Start");
            cancel = new JButton("Cacnel");

            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();

            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(50, 10, 5, 10);
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.weightx = 1;
            gbc.gridwidth = 2;
            add(pb, gbc);

            gbc.gridx = 0;
            gbc.gridy = 1;
            gbc.insets = new Insets(0, 0, 0, 5);
            gbc.fill = GridBagConstraints.NONE;
            gbc.weightx = 1;
            gbc.gridwidth = 1;
            gbc.anchor = GridBagConstraints.EAST;
            add(start, gbc);

            gbc.weightx = 0;
            gbc.gridx = 1;
            gbc.insets = new Insets(0, 0, 0, 10);
            gbc.anchor = GridBagConstraints.WEST;
            add(cancel, gbc);

        }


    }
}