java JTabbedPane 的标签位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13264927/
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
Tabs' position of JTabbedPane
提问by Issam2204
I would like to know how can I set the tabs' position of JTabbedPane starting from the left.By default they are in a central position because i use Mac OS X.
我想知道如何从左侧开始设置 JTabbedPane 的选项卡位置。默认情况下,它们处于中间位置,因为我使用的是 Mac OS X。
If you need more info please let me know.
如果您需要更多信息,请告诉我。
see this image to get an idea of what I get in my mac using the default JTabbedPane:
查看此图像以了解我使用默认 JTabbedPane 在我的 mac 中获得的内容:
回答by Skjalg
If your target platform is Mac, you would most likely follow the style guides for OSX. Java Development Guide for Mac. Page 40 talks about Tabbed Panes.
如果您的目标平台是 Mac,您很可能会遵循 OSX 的风格指南。 适用于 Mac 的 Java 开发指南。第 40 页讨论了选项卡式窗格。
By default you can only position the tabs on the different sides, for example:
默认情况下,您只能将选项卡放在不同侧,例如:
// Alternative placements:
// JTabbedPane.TOP
// JTabbedPane.RIGHT
// JTabbedPane.LEFT
// JTabbedPane.BOTTOM.
tabbedPane.setTabPlacement(JTabbedPane.TOP);
But if you want to have the tabs on top, but adjusted to left rather than center, you need to either pick a different look and feel, or modify the system look and feel. This is possible but a bit more involved, and as pointed out allready, it is not reccomended. You have to perform all changes before you display anything. For a starting point on that topic, refer to How to write a Custom Look and Feel
但是,如果您想将选项卡放在顶部,但调整到左侧而不是中心,您需要选择不同的外观,或者修改系统外观。这是可能的,但涉及更多,正如已经指出的那样,不推荐这样做。在显示任何内容之前,您必须执行所有更改。有关该主题的起点,请参阅如何编写自定义外观
For running example see code:
运行示例见代码:
import java.awt.*;
import javax.swing.*;
public class Tab {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame("TabbedPane");
frame.getContentPane().add(new TabView());
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setMinimumSize(new Dimension(800, 450));
frame.setLocationRelativeTo(null); // Center
frame.pack();
frame.setVisible(true);
}
});
}
static class TabView extends JPanel {
private JTabbedPane tabbedPane;
TabView() {
createComponents();
makeLayout();
}
private void createComponents() {
tabbedPane = new JTabbedPane();
tabbedPane.addTab("Hello", new JLabel("World"));
tabbedPane.addTab("Goodbye", new JLabel("Sunshine"));
}
private void makeLayout() {
setLayout(new BorderLayout());
// Alternative placements: JTabbedPane.TOP, JTabbedPane.RIGHT, JTabbedPane.LEFT JTabbedPane.BOTTOM.
tabbedPane.setTabPlacement(JTabbedPane.TOP);
// Alternative layout policies: JTabbedPane.WRAP_TAB_LAYOUT or JTabbedPane.SCROLL_TAB_LAYOUT.
tabbedPane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
add(tabbedPane, BorderLayout.CENTER);
}
}
}