我想创建一个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();
但是我认为这看起来很丑陋并且可读性为零,所以我问你:我如何使用不同的布局重新创建这个框架,或者我如何以不同的方式使用这种布局以使其可读?
答案 0 :(得分:3)
如果进度字符串可以包含在进度条中,则只需使用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);
}
}