如何重新创建这个基于 JFrame 的进度条?
How to recreate this progress bar based JFrame?
我想创建一个 JFrame
,我刚刚找到了完美的框架。
我想重新创建这个:
获取此帧的代码如下所示:
progressBar = new JProgressBar();
statusLbl = new JLabel();
statusLbl1 = new JLabel();
percentLbl = new JLabel();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setTitle("Auto-Updater");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
formWindowClosing(evt);
}
});
statusLbl.setText("Status:");
statusLbl1.setText("N/A");
percentLbl.setText("0%");
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(statusLbl).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(statusLbl1).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 304, 32767)
.addComponent(percentLbl)).addComponent(progressBar, GroupLayout.Alignment.TRAILING, -1, 380, 32767))
.addContainerGap()));
layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(statusLbl1, -1, -1, 32767)
.addComponent(percentLbl))
.addComponent(statusLbl, -1, -1, 32767)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(progressBar, -2, 30, -2).addContainerGap(-1, 32767)));
pack();
但我认为这看起来很难看并且可读性为零,所以我问你:如何使用不同的布局重新创建此框架,或者如何以不同的方式使用此布局以使其可读?
如果进度字符串可以包含在进度条中,则可以使用单个 JPanel
和 BorderLayout
来完成。将标签放在 PAGE_START
中,将进度条放在 CENTER
中(如果没有指定约束,这是默认设置)。
注意: 我倾向于在 JDialog
或 JOptionPane
中显示该面板而不是 JFrame
,但在这里是基于框架的版本。
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class ProgressBarPanel {
private JComponent ui = null;
ProgressBarPanel() {
initUI();
}
public void initUI() {
if (ui!=null) return;
ui = new JPanel(new BorderLayout(4,4));
ui.setBorder(new EmptyBorder(4,4,4,4));
JProgressBar progressBar = new JProgressBar(0, 100) {
public Dimension getPreferredSize() {
// base this on a multiple of the default preferred size to
// account for the size of the font used to paint the
// progress string
return new Dimension(400,40);
}
};
progressBar.setValue(50);
progressBar.setStringPainted(true);
ui.add(progressBar);
ui.add(new JLabel("Status: N/A"), BorderLayout.PAGE_START);
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
ProgressBarPanel o = new ProgressBarPanel();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
我想创建一个 JFrame
,我刚刚找到了完美的框架。
我想重新创建这个:
获取此帧的代码如下所示:
progressBar = new JProgressBar();
statusLbl = new JLabel();
statusLbl1 = new JLabel();
percentLbl = new JLabel();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setTitle("Auto-Updater");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
formWindowClosing(evt);
}
});
statusLbl.setText("Status:");
statusLbl1.setText("N/A");
percentLbl.setText("0%");
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(statusLbl).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(statusLbl1).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 304, 32767)
.addComponent(percentLbl)).addComponent(progressBar, GroupLayout.Alignment.TRAILING, -1, 380, 32767))
.addContainerGap()));
layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(
layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(statusLbl1, -1, -1, 32767)
.addComponent(percentLbl))
.addComponent(statusLbl, -1, -1, 32767)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(progressBar, -2, 30, -2).addContainerGap(-1, 32767)));
pack();
但我认为这看起来很难看并且可读性为零,所以我问你:如何使用不同的布局重新创建此框架,或者如何以不同的方式使用此布局以使其可读?
如果进度字符串可以包含在进度条中,则可以使用单个 JPanel
和 BorderLayout
来完成。将标签放在 PAGE_START
中,将进度条放在 CENTER
中(如果没有指定约束,这是默认设置)。
注意: 我倾向于在 JDialog
或 JOptionPane
中显示该面板而不是 JFrame
,但在这里是基于框架的版本。
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class ProgressBarPanel {
private JComponent ui = null;
ProgressBarPanel() {
initUI();
}
public void initUI() {
if (ui!=null) return;
ui = new JPanel(new BorderLayout(4,4));
ui.setBorder(new EmptyBorder(4,4,4,4));
JProgressBar progressBar = new JProgressBar(0, 100) {
public Dimension getPreferredSize() {
// base this on a multiple of the default preferred size to
// account for the size of the font used to paint the
// progress string
return new Dimension(400,40);
}
};
progressBar.setValue(50);
progressBar.setStringPainted(true);
ui.add(progressBar);
ui.add(new JLabel("Status: N/A"), BorderLayout.PAGE_START);
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
ProgressBarPanel o = new ProgressBarPanel();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}