gpt4 book ai didi

java - 在操作后显示进度条

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:42:34 26 4
gpt4 key购买 nike

我想在一些操作后显示一个进度条。实际代码很复杂,但我准备了一个示例我希望进度条填满框架的宽度,但我希望它具有特定/正常的高度而不填满框架。我怎样才能做到这一点?如果框架的布局没有改变 (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();
}
}

最佳答案

The actual code is complex, but I've prepared a sample I want the progress bar to fill the width of the frame, but I want it to have a specific/normal height and not fill the frame.

例如,您可以使用 GridBagLayout

JPanel controlPanel = new JPanel();
controlPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
controlPanel.add(progressBar, gbc);

参见 How to Use GridBagLayout了解更多详情

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

看看How to Use Root Panes了解更多详情

此外,根据您的需要,您可以使用 ProgressMonitorfor example

关于java - 在操作后显示进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33280119/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com