【问题标题】:JTabbedPane spacingJTabbedPane 间距
【发布时间】:2012-04-19 05:06:49
【问题描述】:

原始顶部标签方向设置:

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

有问题的右标签方向设置:

从上面的 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);

}

谢谢。

【问题讨论】:

  • 如果您不希望事物重叠,为什么要使用玻璃窗格?让布局管理器为您做这些事情。
  • When the tabs are set to orientate on top, the Quit button would align nicely next to the tabs but when the tabs are orientated to the right, it would overlap.我想在右上角有退出按钮。 JFrame 的关闭按钮用于将窗口图标化到系统托盘,因此应添加退出按钮以关闭应用程序。
  • 哪部分解释了“玻璃窗格”的使用?
  • @thotheolh 与将 JButton 放入 Right_Top_Corner 的方式相同,可以在容器内的任何位置移动
  • 按钮的位置计算有问题,您没有显示...

标签: java swing space jtabbedpane glasspane


【解决方案1】:

好吧,我建议您将退出按钮从 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 );
}

第一个选项卡式窗格(顶部)在顶部和选项卡之间有 30 像素的间隙。第二个选项卡式窗格设置了默认间隙。

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

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

我也不能保证这将适用于所有本机 UI,但我认为应该支持这些基本功能。至少它在 Windows、Mac OS 和 Metal LaFs 中受支持。

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

【讨论】:

    【解决方案2】:

    好的。对我来说听起来有点奇怪......但你可以有 2 个按钮:一个在 GlassPane 中(如果 TabbedPane 朝上则可见)和一个在顶部的 Bar 中(如果 TabbedPane 朝右则可见)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-25
      • 2011-10-11
      • 2014-08-24
      • 2011-04-09
      • 1970-01-01
      • 1970-01-01
      • 2012-12-07
      相关资源
      最近更新 更多