gpt4 book ai didi

java - JTabbedPane间距

转载 作者:行者123 更新时间:2023-12-01 15:25:29 24 4
gpt4 key购买 nike

原始顶部选项卡方向设置:

http://dl.dropbox.com/u/3238736/screenshots/Screenshot-PasswordStore-1.png

有问题的右选项卡方向设置:

enter image description here

从上面的 GUI 中,我的 JTabbedPane(右侧的蓝色选项卡)与“退出”按钮(使用 GlassPane 呈现)重叠。

注意:退出按钮使用 GlassPane 呈现在右上角。

我想要一些关于移动蓝色选项卡以便为“退出”按钮留出一些间距的技术建议?

创建 GlassPane 以插入退出按钮的代码如下所示:

public void addUniversalQuitBtn() {
// Thanks to http://www.java-forums.org/awt-swing/12267-how-add-jbutton-tabbed-pane-headder.html forum post regarding adding a button on glasspane.
Rectangle tabBounds = mainTabPane.getBoundsAt(0);
Container glassPane = (Container) this.getRootPane().getGlassPane();
glassPane.setVisible(true);
glassPane.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(tabBounds.y, 0, 0, 10);
gbc.anchor = GridBagConstraints.NORTHEAST;
quitBtn.setPreferredSize(new Dimension(quitBtn.getPreferredSize().width, (int) tabBounds.getHeight() - 2));
glassPane.add(quitBtn, gbc);

}

谢谢。

最佳答案

好吧,我建议您将退出按钮从 glasspane 移至某个适当的容器,但对于标准 Swing JTabbedPane,您不能那样做...

所以这是某种解决方案:

public static void main ( String[] args )
{
JFrame frame = new JFrame ();

Insets current = UIManager.getInsets ( "TabbedPane.tabAreaInsets" );
UIManager.put ( "TabbedPane.tabAreaInsets",
new Insets ( current.top, 40, current.bottom, current.right ) );

JTabbedPane tabbedPane = new JTabbedPane ();
tabbedPane.setTabPlacement ( JTabbedPane.RIGHT );
tabbedPane.addTab ( "Tab 1", new JLabel () );
tabbedPane.addTab ( "Tab 2", new JLabel () );
frame.add ( tabbedPane );

UIManager.put ( "TabbedPane.tabAreaInsets", current );

JTabbedPane tabbedPane2 = new JTabbedPane ();
tabbedPane2.setTabPlacement ( JTabbedPane.RIGHT );
tabbedPane2.addTab ( "Tab 3", new JLabel () );
tabbedPane2.addTab ( "Tab 4", new JLabel () );
frame.add ( tabbedPane2, BorderLayout.SOUTH );

frame.setSize ( 400, 400 );
frame.setLocationRelativeTo ( null );
frame.setVisible ( true );
}

第一个选项卡式 Pane (顶部的一个)顶部和选项卡之间有 30px 的间隙。第二个选项卡式 Pane 设置了默认间隙。

通过更改“TabbedPane.tabAreaInsets”键下的插入,您可以操纵选项卡运行和选项卡式 Pane 侧面之间的间距。请注意,当选项卡位置与顶部不同时,这些插图会旋转。因此,如果您想使用右侧制表符位置修改顶部间距,则应该修改左侧制表符而不是顶部制表符,就像我在示例中所做的那样。

并且不要忘记放回旧的 Insets 值,否则该更改将影响更改后创建的所有选项卡式 Pane 。

此外,我不能保证这适用于所有 native UI,但我认为应该支持此类基本功能。至少 Windows、Mac OS 和 Metal LaFs 都支持它。

还有一件事 - 您将无法在运行时更改已创建的选项卡式 Pane 的选项卡区域插入,因为它在创建时保存到特定的系统 UI 中,并且无法更新该值(至少没有使用反射“功能”并暴力访问私有(private)字段)。因此,如果您希望仅在一种选项卡放置中存在间隙,则必须重新创建选项卡式 Pane 。

关于java - JTabbedPane间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10221971/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com