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

Pai*_*ick 53 java swing netbeans statusbar

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

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

kro*_*ock 102

使用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);
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

  • 对我不起作用,状态栏最终出现在窗口中间 (2认同)

Sim*_*mon 6

不幸的是,Swing没有对StatusBars的原生支持.您可以使用a 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);        
    }        
}
Run Code Online (Sandbox Code Playgroud)

然后在主面板中:

statusBar = new StatusBar();
getContentPane().add(statusBar, java.awt.BorderLayout.SOUTH);
Run Code Online (Sandbox Code Playgroud)

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