简体   繁体   English

为JToolbar创建复合Swing组件

[英]Creating a composite Swing component for JToolbar

When using setRollover(true), buttons on Swing toolbars are flat without border and the border is drawn only when hovering/pushing the button. 使用setRollover(true)时,Swing工具栏上的按钮是平坦的,没有边框,并且仅在悬停/按下按钮时才绘制边框。 However, if the buttons are first added to a panel, and then the panel is added to the toolbar, this does not work. 但是,如果先将按钮添加到面板,然后再将面板添加到工具栏,则此操作将无效。 Is there some easy way how to achieve it? 有一些简单的方法可以实现它吗?

I want the buttons to be in a JPanel to make them act as a single component (imagine a paging component with first/prev/next/last page buttons). 我希望这些按钮位于JPanel中,以使它们充当单个组件(想象具有第一页/上一页/下一页/最后一页按钮的分页组件)。 I also want it to work regardless of L&F (as it would if the JPanel was not between the toolbar and the buttons). 我也希望它能与L&F无关(就像JPanel不在工具栏和按钮之间一样)。

EDIT: 编辑:

Compare the buttons One & Two (added directly) with buttons Three & Four (added via a JPanel) in the following example: 在下面的示例中,将按钮一和二(直接添加)与按钮三和四(通过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);
    }
}

Here are the screenshots: 这是屏幕截图:

The toolbar on Nimbus LF: Nimbus LF上的工具栏:

Nimbus LF上的工具栏

The same toolbar when mouse hovers over the second button (the mouse cursor is not shown): 鼠标悬停在第二个按钮上时,使用相同的工具栏(未显示鼠标光标):

悬停的雨云

The same toolbar on Windows LF: Windows LF上的相同工具栏:

Windows LF上的相同工具栏

I would like the Three and Four buttons to work the same way as the One and Two buttons. 我希望“三个”和“四个”按钮的工作方式与“一个”和“两个”按钮相同。

1) I'd suggesting to set JMenuBar as container rather than JToolbar , 1)我建议将JMenuBar设置为容器而不是JToolbar

disadvantages: 缺点:

  • isn't moveable and detachable, nor out of Container 不可移动和可拆卸,也不能移出Container

  • could by placed everywhere but only inside Container, like as another JComponent by using LayoutManager 可以放置在任何地方,但只能放置在Container中,例如使用LayoutManager作为另一个JComponent


2) for JToolBar would be better to place there one JPanel nested another JComponents , as shows from your code example 2)对于JToolBar ,最好将一个JPanel嵌套在另一个JComponents ,如代码示例所示


3) in your code example you define one JButton fouth times, in Java is required define as separate Objects 3)在您的代码示例中,您定义了一个JButton两次,在Java中需要定义为单独的Objects

Using another JToolbar instead of JPanel works. 使用另一个JToolbar代替JPanel是可行的。

But: then I would probably (or maybe not?) have a problem, if I wanted to include the composite component into a dialog or something else than a toolbar. 但是:如果我想将复合组件包括在对话框或工具栏之外的其他组件中,那么我可能会(或可能不会?)遇到问题。 (This would be similar to having two types of buttons, one for toolbars and one for the rest) (这类似于具有两种类型的按钮,一种用于工具栏,另一种用于其他按钮)

this is the portion of the code from the question adding a panel changed to add a toolbar. 这是问题的代码部分,其中添加面板已更改为添加工具栏。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM