如何在Java应用程序底部创建一个类似状态栏的条形菜单?

57

我正在创建一个Java应用程序,并希望在应用程序底部有一栏,其中我可以显示一个文本栏和状态(进度)栏。

但是我似乎在NetBeans中找不到该控件,也不知道如何手动创建所需控件的代码。

5个回答

111

创建一个具有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);
这是上面状态栏代码在我的机器上的结果:

enter image description here


2
对我来说没有起作用,状态栏最终出现在窗口中间。 - Alex R
1
你需要在JPanel内使用不同的布局。就像上面的代码片段一样,BoxLayout会产生与图片中相似的结果。 - Kake_Fisk

6

很遗憾,Swing不支持原生的状态栏。 您可以使用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

这篇文章介绍了如何在Java Swing中创建状态栏。状态栏是一个常见的用户界面组件,用于显示应用程序的状态信息和其他有用的信息。本文提供了一些示例代码和说明,以帮助您创建自己的状态栏。


1
使用JPanel作为状态栏,比使用JLabel更好的想法。 - Bozhidar Batsov
@Bozhidar Batsov:是的,没错。 - Simon

4

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

以下是使用方法:

  1. Download the JAR they are providing and put it in your classpath
  2. The status bar internally divides the bar area into zone. Each zone can contain a Component (JLabel, JButton, etc). Idea is to fill the bar with required zones and the components.

  3. Instantiate the status bar as below....

    import java.awt.Component;
    import javax.swing.BorderFactory;
    import javax.swing.JLabel;
    import com.l2fprod.common.swing.StatusBar;
    
    StatusBar statusBar = new StatusBar();
    statusBar.setZoneBorder(BorderFactory.createLineBorder(Color.GRAY));
    statusBar.setZones(
        new String[] { "first_zone", "second_zone", "remaining_zones" },
        new Component[] {
            new JLabel("first"),
            new JLabel("second"),
            new JLabel("remaining")
        },
        new String[] {"25%", "25%", "*"}
    );
    
  4. Now add the above statusBar to the main panel you have (BorderLayout and set it to the south side).

看一下我正在开发的其中一个应用程序的样本截图(这个应用程序有两个区域)。如果你遇到任何问题,请告诉我....


4
截图链接已失效。 - GKFX

3

5
对于一个容易实现的组件来说,将整个SwingX引入似乎不是最好的选择...如果你已经在应用中使用SwingX,那么可以接受,但否则这就是过度设计了... - Bozhidar Batsov

1

如果您想要比已接受的答案更现代化的外观,请使用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);

screenshot of the result


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接