如何在 Java 应用程序底部创建一个栏,如状态栏?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3035880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I create a bar in the bottom of a Java app, like a status bar?
提问by Paintrick
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.
我正在创建一个 Java 应用程序,并希望在应用程序底部有一个栏,我在其中显示一个文本栏和一个状态(进度)栏。
Only I can't seem to find the control in NetBeans neither do I know the code to create in manually.
只有我似乎无法在 NetBeans 中找到控件,我也不知道要手动创建的代码。
采纳答案by krock
Create a JFrame or JPanel with a BorderLayout, give it something like a BevelBorderor line border so it is seperated off from the rest of the content and then add the status panel at BorderLayout.SOUTH.
创建一个带有 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);
Here is the result of the above status bar code on my machine:
这是我机器上上述状态条码的结果:
回答by Simon
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:
不幸的是,Swing 没有对 StatusBar 的原生支持。您可以使用一个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);
}
}
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
来自:http: //www.java-tips.org/java-se-tips/javax.swing/creating-a-status-bar.html
回答by Richard Walton
I would recommend the status bar component in the SwingX library- Here is the API doc for the status bar is here
我会推荐SwingX 库中的状态栏组件- 这里是状态栏的 API 文档在这里
A good example of its use is here.
Have fun.
玩得开心。
回答by arcamax
I have used swing library from L2FProd. The Status bar library they have provided is very good.
我使用了L2FProd 的Swing 库。他们提供的状态栏库非常好。
Below is how you would use it:
以下是您将如何使用它:
- Download the JAR they are providing and put it in your classpath
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.
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%", "*"} );
Now add the above
statusBar
to the main panel you have (BorderLayout and set it to the south side).
- 下载他们提供的 JAR 并将其放在您的类路径中
状态栏在内部将栏区域划分为区域。每个区域可以包含一个组件(JLabel、JButton 等)。想法是用所需的区域和组件填充栏。
实例化状态栏如下....
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%", "*"} );
现在将上述内容添加
statusBar
到您拥有的主面板(BorderLayout 并将其设置为南侧)。
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....
查看我在这里工作的其中一个应用程序的示例屏幕截图(它有 2 个区域)。如果您遇到任何问题,请告诉我......
回答by t.animal
For a more modern look than the accepted answer go with a GridBagLayout and a JSeparator:
要获得比公认答案更现代的外观,请使用 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);