【问题标题】:How can I create a bar in the bottom of a Java app, like a status bar?如何在 Java 应用程序的底部创建一个栏,例如状态栏?
【发布时间】:2011-03-03 10:16:36
【问题描述】:

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

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

【问题讨论】:

    标签: java swing netbeans statusbar


    【解决方案1】:

    创建一个带有 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);
    

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

    【讨论】:

    • 对我不起作用,状态栏最终位于窗口中间
    • 您需要在 JPanel 中使用不同的布局。和上面的代码 sn-p 一样,BoxLayout 会产生如图所示的结果。
    【解决方案2】:

    不幸的是,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

    【讨论】:

    • 使用 JPanel,而不是 JLabel 作为状态栏是更好的主意。
    • @Bozhidar Batsov:是的。
    【解决方案3】:

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

    下面是你将如何使用它:

    1. 下载他们提供的 JAR 并将其放入您的类路径中
    2. 状态栏在内部将栏区域划分为区域。每个区域都可以包含一个组件(JLabel、JButton 等)。想法是用所需的区域和组件填充栏。

    3. 如下实例化状态栏....

      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. 现在将上述statusBar 添加到您拥有的主面板(BorderLayout 并将其设置为南侧)。

    查看我正在开发的一个应用程序的示例屏幕截图here(它有 2 个区域)。如果您遇到任何问题,请告诉我......

    【讨论】:

    • 截图是死链接。
    【解决方案4】:

    我会推荐SwingX library 中的状态栏组件 - 这是status bar is here 的 API 文档

    A good example of its use is here.

    玩得开心。

    【讨论】:

    • 将整个 SwingX 引入一个实现起来很简单的组件对我来说似乎不是最好的主意...如果您已经在应用程序中使用 SwingX,那很好,否则就是完全矫枉过正......
    【解决方案5】:

    要获得比公认答案更现代的外观,请使用 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-04
      相关资源
      最近更新 更多