【问题标题】:Creating a composite Swing component for JToolbar为 JToolbar 创建复合 Swing 组件
【发布时间】:2012-03-30 20:38:00
【问题描述】:

当使用 setRollover(true) 时,Swing 工具栏上的按钮是扁平的,没有边框,只有在悬停/按下按钮时才会绘制边框。但是,如果先将按钮添加到面板,然后将面板添加到工具栏,则这不起作用。有没有一些简单的方法来实现它?

我希望按钮位于 JPanel 中,以使它们充当单个组件(想象一个带有第一页/上一页/下一页/最后一页按钮的分页组件)。无论 L&F 是什么,我也希望它能够工作(就像 JPanel 不在工具栏和按钮之间一样)。

编辑:

在以下示例中比较按钮一和二(直接添加)与按钮三和四(通过 JPanel 添加):

import javax.swing.*;

public class ToolbarTest extends JFrame {
    ToolbarTest() {
        JToolBar toolbar = new JToolBar();
        toolbar.setRollover(true);

        JButton button = new JButton("One");
        button.setFocusable(false);
        toolbar.add(button);

        button = new JButton("Two");
        button.setFocusable(false);
        toolbar.add(button);

        JPanel panel = new JPanel();
        button = new JButton("Three");
        button.setFocusable(false);
        panel.add(button);

        button = new JButton("Four");
        button.setFocusable(false);
        panel.add(button);

        toolbar.add(panel);

        add(toolbar);
        pack();
    }

    public static void main(String[] args) throws Throwable {
        // optional: set look and feel (some lf might ignore the rollover property)
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {      // or "Windows", "Motif"
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }

        ToolbarTest frame = new ToolbarTest();
        frame.setVisible(true);
    }
}

以下是截图:

Nimbus LF 上的工具栏:

鼠标悬停在第二个按钮上时的相同工具栏(鼠标光标未显示):

Windows LF 上的相同工具栏:

我希望 3 和 4 按钮的工作方式与 1 和 2 按钮相同。

【问题讨论】:

  • 从未见过。请使用您的SSCCE 编辑问题
  • 我添加了一个例子。
  • 一定是JToolBar吗???,你需要用JToolBar在屏幕上移动吗???,
  • 如果可能的话,我更喜欢标准的 JToolbar,但是如果有其他工具栏实现可以处理这个问题,那么我不反对使用它。 (如果您的问题是我是否需要一般使用工具栏,那么是的 - 该应用程序具有菜单、工具栏等。LF 确保工具栏在给定平台上充当工具栏,它是可拆卸的等)
  • 好问题 :-) 查看代码显示 BasicToolBarUI 仅处理直接子级。没什么可做的......

标签: java swing jtoolbar


【解决方案1】:

使用另一个 JToolbar 代替 JPanel 是可行的。

但是:如果我想将复合组件包含到对话框或工具栏以外的其他东西中,我可能会(或者可能不会?)有问题。 (这类似于有两种类型的按钮,一种用于工具栏,另一种用于其余按钮)

这是问题中的代码部分,添加面板已更改为添加工具栏。

JToolBar component = new JToolBar();
component.setRollover(true);
component.setBorder(null);
component.setFloatable(false);

button = new JButton("Three");
button.setFocusable(false);
component.add(button);

button = new JButton("Four");
button.setFocusable(false);
component.add(button);

toolbar.add(component);

【讨论】:

    【解决方案2】:

    1) 我建议to set JMenuBar as container 而不是JToolbar

    缺点:

    • 不可移动和可拆卸,也不脱离Container

    • 可以通过使用LayoutManager 放置在任何地方,但只能放在容器内,就像另一个JComponent 一样


    2) 对于JToolBar,最好将一个JPanel 嵌套在另一个JComponents 中,如您的代码示例所示


    3) 在您的代码示例中,您第四次定义了一个 JButton,在 Java 中需要定义为单独的 Objects

    【讨论】:

    • Ad 3):我确实定义了 4 个独立的 JButton 对象。只有变量被重用。对象不是。
    • 我不确定我是否理解。我想获得嵌入在面板中的按钮的翻转效果,而不仅仅是直接添加的按钮。但是如果我按照你的建议使用 JMenuBar,那么我不会得到任何按钮的效果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 2014-09-09
    • 2012-07-11
    • 1970-01-01
    相关资源
    最近更新 更多