我想制作一个带有文本栏、进度条之类的状态栏。然而,事情正在扼杀。
起初我只有文本栏,状态栏如下例所示(我不需要父容器):
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Main
{
public static void main(String[] args)
{
//Display actual GUI
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new Gui();
}
});
}
}
class Gui
{
public Gui()
{
JFrame masterWindow = new JFrame();
masterWindow.setSize(1100, 800);
masterWindow.setMinimumSize(new Dimension(400, 400));
masterWindow.setLocationRelativeTo(null);
masterWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel rootPanel = new JPanel();
rootPanel.setLayout(new BorderLayout());
StatusBar statusBar = new StatusBar();
rootPanel.add(statusBar, BorderLayout.SOUTH);
masterWindow.setContentPane(rootPanel);
masterWindow.setVisible(true);
}
}
class StatusBar extends JLabel
{
private static final long serialVersionUID = 1L;
StatusBar()
{
super("welcomeText");
add(textBar);
setOpaque(true);
setBorder(BorderFactory.createLoweredSoftBevelBorder());
}
}
这可以正常工作。但是,请记住状态栏的高度。现在,如果将 StatusBar 类更改为下面的类,高度将几乎翻倍。为什么会这样?
这种设置并不需要更多的垂直空间:
public class StatusBar extends JPanel
{
private static final long serialVersionUID = 1L;
StatusBar()
{
super();
JLabel textBar = new JLabel(Main.language.getString("welcomeText"));
add(textBar);
setOpaque(true);
setBorder(BorderFactory.createLoweredSoftBevelBorder());
}
}
为什么会发生这种情况,我该如何阻止它?
编辑: BorderLayout 确实有效,但我想使用 MigLayout。
我想通了,但现在又出现了另一个问题。额外的进度条不会停靠在东边,分隔符甚至不可见。
这是代码:
public class StatusBar extends JPanel
{
private static final long serialVersionUID = 1L;
JLabel textBar;
JProgressBar progressBar;
StatusBar()
{
setLayout(new MigLayout("insets 0, gap 0!")); //Mig has some gaps inbetween components, this will remove the gaps
textBar = new JLabel(Main.language.getString("welcomeText"));
progressBar = new JProgressBar(0, 100);
JSeparator separator = new JSeparator(SwingConstants.VERTICAL);
add(textBar);
add(separator);
add(progressBar, "east");
setOpaque(true);
setBorder(BorderFactory.createLoweredSoftBevelBorder());
}
}
我可以提出一个新问题,但我认为这不值得......