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

发布于 2024-09-05 04:15:41 字数 114 浏览 3 评论 0原文

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

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

I am in the process of creating a Java app and would like to have a bar
on the bottom of the app, in which I display a text bar and a status (progress) bar.

Only I can't seem to find the control in NetBeans neither do I know the code to create in manually.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

笨死的猪 2024-09-12 04:15:41

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

以下是上述状态栏代码在我的机器上的结果:

在此处输入图像描述

Create a JFrame or JPanel with a BorderLayout, give it something like a BevelBorder or line border so it is seperated off from the rest of the content and then add the status panel at 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);

Here is the result of the above status bar code on my machine:

enter image description here

回忆躺在深渊里 2024-09-12 04:15:41

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

Unfortunately Swing does not have a native support for StatusBars.
You can use a BorderLayout and a label or whatever you need to display at the bottom:

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);        
    }        
}

Then in your Main Panel:

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

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

蓬勃野心 2024-09-12 04:15:41

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

下面是您将如何使用它:

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

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

    导入java.awt.Component;
    导入 javax.swing.BorderFactory;
    导入 javax.swing.JLabel;
    导入 com.l2fprod.common.swing.StatusBar;
    
    StatusBar statusBar = new StatusBar();
    statusBar.setZoneBorder(BorderFactory.createLineBorder(Color.GRAY));
    状态栏.setZones(
        new String[] { "first_zone", "second_zone", "remaining_zones" },
        新组件[] {
            新的 JLabel(“第一个”),
            新的 JLabel(“第二个”),
            新的 JLabel(“剩余”)
        },
        新字符串[] {"25%", "25%", "*"}
    );
    
  4. 现在将上面的 statusBar 添加到您拥有的主面板(BorderLayout 并将其设置为南侧)。

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

I have used swing library from L2FProd. The Status bar library they have provided is very good.

Below is how you would use it:

  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).

See a sample screenshot from one of the apps I am working on here (it has 2 zones). Let me know if you face any issues....

如梦 2024-09-12 04:15:41

我推荐 SwingX 库 中的状态栏组件 - 这是 状态栏在这里

< a href="http://www.javalobby.org/java/forums/t18979.html" rel="nofollow noreferrer">这里是一个很好的使用示例。

祝你玩得开心。

I would recommend the status bar component in the SwingX library - Here is the API doc for the status bar is here

A good example of its use is here.

Have fun.

巷子口的你 2024-09-12 04:15:41

为了比接受的答案更现代的外观,请使用 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);

结果的屏幕截图

For a more modern look than the accepted answer go with a GridBagLayout and a 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

老子叫无熙 2024-09-12 04:15:41

您应该使用正在处理的框架的分层窗格

    JPanel statusPanel = new JPanel();
    statusPanel.setBorder(new MatteBorder(1, 0, 0, 0, Color.gray));
    statusPanel.setLayout(new BorderLayout());
    statusPanel.setMinimumSize(new Dimension(0, 16));
    statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.X_AXIS));
    statusPanel.add(statusIcon, LEFT_ALIGNMENT);
    statusPanel.add(statusLabel);
    rootPane.add(statusPanel, BorderLayout.SOUTH);

    getLayeredPane().setLayout(new BorderLayout());
    getLayeredPane().add(statusPanel, BorderLayout.SOUTH);

You should use the Layered Pane of the frame you are working on:

    JPanel statusPanel = new JPanel();
    statusPanel.setBorder(new MatteBorder(1, 0, 0, 0, Color.gray));
    statusPanel.setLayout(new BorderLayout());
    statusPanel.setMinimumSize(new Dimension(0, 16));
    statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.X_AXIS));
    statusPanel.add(statusIcon, LEFT_ALIGNMENT);
    statusPanel.add(statusLabel);
    rootPane.add(statusPanel, BorderLayout.SOUTH);

    getLayeredPane().setLayout(new BorderLayout());
    getLayeredPane().add(statusPanel, BorderLayout.SOUTH);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文