为什么 addSeparator() 不能与我的 JToolBar 一起使用?

发布于 2024-09-08 16:44:18 字数 458 浏览 4 评论 0原文

我无法让 JSeparator 显示在 JToolBar 中。我的工具栏创建如下:

public class ToolBar extends JToolBar {
    super();

    FlowLayout layout = new FlowLayout(FlowLayout.LEFT, 10, 5);
    setLayout(layout);

    add(new JButton("Button 1"));
    addSeparator();
    add(new JButton("Button 2"));
    add(new JButton("Button 3"));
    addSeparator();

    // Show
    setVisible(true);
    setFloatable(false);

}

任何想法都会非常感激,我已经尝试让它工作太久了>(

I am having trouble getting a JSeparator to show up inside of a JToolBar. My toolbar is created as follows :

public class ToolBar extends JToolBar {
    super();

    FlowLayout layout = new FlowLayout(FlowLayout.LEFT, 10, 5);
    setLayout(layout);

    add(new JButton("Button 1"));
    addSeparator();
    add(new JButton("Button 2"));
    add(new JButton("Button 3"));
    addSeparator();

    // Show
    setVisible(true);
    setFloatable(false);

}

Any thoughts would be really appreciated, I have been trying to get this to work for way too long now >(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

三寸金莲 2024-09-15 16:44:18

在那里尝试您的代码,当我调用 addSeparator() 方法时,它会在按钮之间创建一个空格,但没有可见的分隔线。

但是,如果我将方法更改为 addSeparator(new Dimension(20,20)) 它就会创建可见的分隔线。

问题可能是默认的外观创建了高度为 1 的分隔符,因此您将无法看到它。

我在 Mac OSX 上运行它。

Trying your code there, when I call the addSeparator() method it creates a space between the buttons but no visible separation line.

But if I change the method to addSeparator(new Dimension(20,20)) it then creates the visible separation line.

The problem could be that the default look and feel creates a separator of height 1 so you would be unable to see it.

I am running it on Mac OSX.

意中人 2024-09-15 16:44:18

您遇到的最大问题是不需要子类化 JToolBar 并在其上设置布局。只需创建它的一个实例并开始添加按钮和分隔符即可。

一般来说,Swing 团队不建议对 Swing 组件进行子类化。

您的代码应如下所示:

JToolBar t = new JToolbar();

t.add(new JButton("Button 1"));
t.addSeparator();
t.add(new JButton("Button 2"));
t.add(new JButton("Button 3"));
t.addSeparator();

// Show
t.setVisible(true);
t.setFloatable(false);

最后的建议是不要使用按钮。使用行动。这样,可以在工具栏、菜单等上使用相同的操作。更多信息请访问 http://java.sun.com/docs /books/tutorial/uiswing/misc/action.html

更新:
工具栏分隔符的外观取决于您使用的 LAF。

The biggest problem you have is that there is no need to sub-class JToolBar and set layout on it. Just create an instance of it and start adding buttons and separators.

In general Swing team does not recommend sub-classing Swing components.

You code should look like:

JToolBar t = new JToolbar();

t.add(new JButton("Button 1"));
t.addSeparator();
t.add(new JButton("Button 2"));
t.add(new JButton("Button 3"));
t.addSeparator();

// Show
t.setVisible(true);
t.setFloatable(false);

The last advice would be not to use buttons. Use actions. This way same actions can be used on toolbar, menus ect. More info at http://java.sun.com/docs/books/tutorial/uiswing/misc/action.html

UPDATE:
The way the toolbar separator looks depends on LAF you're using.

南风几经秋 2024-09-15 16:44:18

我遇到了同样的问题。我发现根本原因是最大尺寸造成的。

调整后就正常了。

    // ---------------------------------------
    // debug below:
    // ---------------------------------------
    JSeparator separator = new JSeparator(JSeparator.VERTICAL);
    System.err.println("getMaximumSize(): " + separator.getMaximumSize());
    System.err.println("getMinimumSize(): " + separator.getMinimumSize());
    separator.setMaximumSize(new Dimension(2, separator.getMaximumSize().height));


    // ---------------------------------------
    // real sample below
    // ---------------------------------------
    // adds a vertical space bar
    toolBar.add(Box.createHorizontalStrut(5));

    // adds a vertical separator
    JSeparator separator = new JSeparator(JSeparator.VERTICAL);
    Dimension maximumSize = separator.getMaximumSize();
    maximumSize.width = 2;
    separator.setMaximumSize(maximumSize); // Important! Update it!
    toolBar.add(separator);

    // adds a vertical space bar
    toolBar.add(Box.createHorizontalStrut(5));

作者:TJ Tsai([电子邮件受保护]

I met the same problem. I found that the root cause was caused from the maximum size.

After adjusting it, it became normal.

    // ---------------------------------------
    // debug below:
    // ---------------------------------------
    JSeparator separator = new JSeparator(JSeparator.VERTICAL);
    System.err.println("getMaximumSize(): " + separator.getMaximumSize());
    System.err.println("getMinimumSize(): " + separator.getMinimumSize());
    separator.setMaximumSize(new Dimension(2, separator.getMaximumSize().height));


    // ---------------------------------------
    // real sample below
    // ---------------------------------------
    // adds a vertical space bar
    toolBar.add(Box.createHorizontalStrut(5));

    // adds a vertical separator
    JSeparator separator = new JSeparator(JSeparator.VERTICAL);
    Dimension maximumSize = separator.getMaximumSize();
    maximumSize.width = 2;
    separator.setMaximumSize(maximumSize); // Important! Update it!
    toolBar.add(separator);

    // adds a vertical space bar
    toolBar.add(Box.createHorizontalStrut(5));

by TJ Tsai ([email protected])

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文