显示操作进度条

3
我想在某些操作后显示进度条。实际代码很复杂,但我准备了一个样例。
我希望进度条填充框架的宽度,但我希望它有特定/正常的高度并且不填充框架。我该如何做到这一点?
最好不要改变框架的布局(BorderLayout),因为这可能会导致我的实际代码出现问题。如果无法避免,则请更改,并查看结果。
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.Timer;

public class SwingControlDemo
{

    public static int progress = 0;
    private JFrame mainFrame;

    public SwingControlDemo()
    {
        prepareGUI();
    }

    public static void main(String[] args)
    throws Exception
    {
        //BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;
       // org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
        new SwingControlDemo();
    }

    private void prepareGUI()
    {
        mainFrame = new JFrame("Java Swing Examples");
        mainFrame.setSize(400, 400);
        mainFrame.setLayout(new BorderLayout());
        mainFrame.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent windowEvent)
            {
                System.exit(0);
            }
        });
        JButton startButton = new JButton("Start");
        startButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                showProgressBarDemo();
            }
        });

        mainFrame.add(new JLabel("mplaaaaaaaaaaaaaaaaaaa"), BorderLayout.CENTER);
        mainFrame.add(startButton, BorderLayout.SOUTH);
        mainFrame.setVisible(true);
    }

    private void showProgressBarDemo()
    {
        final JProgressBar progressBar = new JProgressBar(0, 100);
        progressBar.setValue(0);
        progressBar.setStringPainted(true);

        JPanel controlPanel = new JPanel();
        controlPanel.setLayout(new BorderLayout());
        controlPanel.add(progressBar, BorderLayout.CENTER);

        mainFrame.add(controlPanel);
        mainFrame.revalidate();
        mainFrame.repaint();

        final Timer timer = new Timer(1000, new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {   
                progress++;
                System.out.println(String.format("Completed %d%% of task.\n", progress));
                progressBar.setValue(progress);
                progressBar.setString(String.format("Completed %d%% of task.\n", progress));
            }
        });

        timer.start();
    }
}
2个回答

3
实际的代码很复杂,但我已经准备了一个示例。我想让进度条填充整个框架的宽度,但是希望它有一个特定/普通的高度,而不是填充整个框架。
例如,您可以使用 GridBagLayout。
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
controlPanel.add(progressBar, gbc);

更多详细信息,请参阅如何使用GridBagLayout

您可以考虑利用框架的“玻璃板”,这将允许您将内容放置在contentPane的顶部,但不会直接影响它。如果您向其注册一个MouseListener,甚至可以阻止鼠标事件传递到contentPane本身。

有关更多详细信息,请参阅如何使用根窗格

此外,根据您的需求,您可能能够使用ProgressMonitor,例如请参见此处


3
如果你想让它显示为特定/正常高度,请将其添加到使用FlowLayout的JPanel中,然后将该JPanel添加到使用BorderLayout的JPanel中(如果您仍想保留BorderLayout)。请注意,我建议使用CardLayout来交换组件。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接