java swing 状态栏_swing-如何在Java应用程序的底部创建一个状态栏?

在Java Swing中创建一个状态栏,可以使用BorderLayout布局管理器,在JFrame或JPanel的南部分添加一个面板作为状态栏。可以使用JLabel显示文本,配合BoxLayout实现水平布局。Swing库如L2FProd或SwingX提供了更丰富的状态栏组件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

swing-如何在Java应用程序的底部创建一个状态栏?

我正在创建Java应用程序,并且想要一个酒吧在应用程序底部,在其中显示文本栏和状态(进度)栏。

只有我似乎无法在NetBeans中找到该控件,我也不知道手动创建的代码。

5个解决方案

102 votes

创建一个带有BorderLayout的JFrame或JPanel,为其提供类似BevelBorder或线条边框的外观,以便将其与其余内容分开,然后在BorderLayout.SOUTH处添加状态面板。

JFrame frame = new JFrame();

frame.setLayout(new BorderLayout());

frame.setSize(200, 200);

// create the status bar panel and shove it down the bottom of the frame

JPanel statusPanel = new JPanel();

statusPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));

frame.add(statusPanel, BorderLayout.SOUTH);

statusPanel.setPreferredSize(new Dimension(frame.getWidth(), 16));

statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.X_AXIS));

JLabel statusLabel = new JLabel("status");

statusLabel.setHorizontalAlignment(SwingConstants.LEFT);

statusPanel.add(statusLabel);

frame.setVisible(true);

这是我的机器上上述状态条形码的结果:

ikLft.png

krock answered 2020-01-10T07:13:16Z

6 votes

不幸的是,Swing没有对StatusBars的本地支持。您可以使用BorderLayout和标签,或在底部显示的内容:

public class StatusBar extends JLabel {

/** Creates a new instance of StatusBar */

public StatusBar() {

super();

super.setPreferredSize(new Dimension(100, 16));

setMessage("Ready");

}

public void setMessage(String message) {

setText(" "+message);

}

}

然后在您的主面板中:

statusBar = new StatusBar();

getContentPane().add(statusBar, java.awt.BorderLayout.SOUTH);

来自:[http://www.java-tips.org/java-se-tips/javax.swing/creating-a-status-bar.html]

Simon answered 2020-01-10T07:13:45Z

4 votes

我使用了L2FProd的swing库。 他们提供的状态栏库非常好。

以下是您的用法:

下载他们提供的JAR并将其放在您的类路径中

状态栏在内部将栏区域划分为区域。 每个区域可以包含一个组件(JLabel,JButton等)。 想法是用所需的区域和组件填充该栏。

实例化状态栏如下。

statusBar

现在将上面的statusBar添加到您拥有的主面板中(BorderLayout并将其设置在南侧)。

在我正在使用的其中一个应用程序中查看示例屏幕截图(它有2个区域)。 让我知道您是否遇到任何问题。

arcamax answered 2020-01-10T07:14:36Z

2 votes

我建议在SwingX库中使用状态栏组件-这是状态栏的API文档

这里是使用它的一个很好的例子。

玩得开心。

Richard Walton answered 2020-01-10T07:15:05Z

0 votes

要获得比接受的答案更现代的外观,请使用GridBagLayout和JSeparator:

JPanel outerPanel = new JPanel(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();

gbc.gridx = 0;

gbc.anchor = GridBagConstraints.PAGE_START;

gbc.fill = GridBagConstraints.HORIZONTAL;

gbc.weightx = 1;

gbc.weighty = 0;

JPanel menuJPanel = new JPanel();

menuJPanel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.RED));

outerPanel.add(menuJPanel, gbc);

gbc.insets = new Insets(5, 5, 5, 5);

gbc.fill = GridBagConstraints.BOTH;

gbc.weighty = 1;

JPanel contentJPanel = new JPanel();

contentJPanel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLUE));

outerPanel.add(contentJPanel, gbc);

gbc.fill = GridBagConstraints.HORIZONTAL;

gbc.weighty = 0;

gbc.insets = new Insets(0, 0, 0, 0);

outerPanel.add(new JSeparator(JSeparator.HORIZONTAL), gbc);

outerPanel.add(new JPanel(), gbc);

o6QRL.png

t.animal answered 2020-01-10T07:15:25Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值