简体   繁体   English

Java Swing如何在文本位置的菜单中设置图标

[英]Java swing how to set icon in menu on text position

I need to put a red rectangle as a separator in JMenu. 我需要在JMenu中放置一个红色矩形作为分隔符。 It cannot be just a separator instance, it has to be JMenuItem(JSeparator goes all the width of the menu). 它不能只是分隔符实例,而必须是JMenuItem(JSeparator遍及整个菜单宽度)。 My issue is that when I set this rectangle as an Icon it (correctly...) moves all the menu components to the right widening whole Menu. 我的问题是,当我将此矩形设置为图标时(正确...),它会将所有菜单组件移至向右扩展整个菜单的位置。 I want it to be where text position from the above and the down elements is. 我希望它是上方和下方元素的文字位置。

I am trying with html but it paints nothing or just a small vertical black spot. 我正在尝试使用html,但它什么也没有画,也没有画一个小的垂直黑点。

JMenuItem sep = new JMenuItem();
    sep.setText("<html><hr size=5 style='background:red;'></html>");

在此处输入图片说明

You can create a customized separator by extending JMenuItem 您可以通过扩展JMenuItem来创建自定义的分隔符

class RedSeparator extends JMenuItem {

    public RedSeparator() {
        setBorder(BorderFactory.createLineBorder(Color.RED));
        setPreferredSize(new Dimension(1,1));
    }
}

You can add separator as a normal JMenuItem 您可以将分隔符添加为普通的JMenuItem

JMenu menu = new JMenu("File");
menu.add(new JMenuItem("Save"));
menu.add(new RedSeparator());
menu.add(new JMenuItem("Exit"));

Hope this helps. 希望这可以帮助。

Your html is a small vertical spot because you haven't provided a width. 您的html是一个小的垂直位置,因为您没有提供宽度。 By providing a width such as the below you will see your line: 通过提供以下宽度,您将看到一行:

sep.setText("<html><hr style=\"width:100px;\"></html>");

The fact that your line is black despite a color being provided might stem from this bug: 尽管提供了颜色,但您的线条仍然是黑色的事实可能源于此错误:

https://bugs.openjdk.java.net/browse/JDK-5059678 https://bugs.openjdk.java.net/browse/JDK-5059678


Instead of using the hr tag, you could style a component, which would give the added benefit of more flexibility with the width. 除了使用hr标签之外,您还可以设置组件的样式,这将为宽度带来更多灵活性。 With the above, your separator would always be 100 px, meaning when a large/small text based item is added to the menu it could look out of place 有了以上内容,分隔符将始终为100 px,这意味着将基于文本的大/小项目添加到菜单时,它看起来可能不合适

Would something like this work for what you need? 这样的东西可以满足您的需求吗?

private JMenuItem createColoredSeparator(Color color){
    JMenuItem separator = new JMenuItem();
    separator.setPreferredSize(new Dimension(5,5));
    separator.setBackground(color);
    separator.setEnabled(false); //make the item unclickable so menu doesn't close
    return separator;
}

Example usage: 用法示例:

JMenu menu = new JMenu("Menu");
menu.add(new JMenuItem("VeryLongMenuItemName"));
menu.add(createColoredSeparator(Color.RED));
menu.add(new JMenuItem("MenuItemName"));

Output: 输出:

在此处输入图片说明


To make the above not fill the full width with the chosen color, you can add a Border of the default color to conceal a portion of the background with these edits in createColoredSeparator : 要使上面的内容不能用所选颜色填充整个宽度,您可以在createColoredSeparator添加以下默认颜色的边框,以使用这些编辑来隐藏背景的一部分:

separator.setPreferredSize(new Dimension(20,20));   

separator.setBorder(BorderFactory.createLineBorder(UIManager.getColor("MenuItem.background"), 8));

Output: 输出:
在此处输入图片说明


Update: I didn't know you needed a raised border applied on top of a potential solution. 更新:我不知道您是否需要在潜在解决方案之上应用凸起的边框。 You can achieve that with a CompoundBorder which is covered as part of How to use Borders . 您可以使用CompoundBorder来实现该目的,这是How to Use Borders的一部分 This allows you to merge multiple borders together, two at a time with one being applied on the inside and the other on the outside. 这使您可以将多个边界合并在一起,一次合并两个边界,一个边界应用于内部,另一个边界应用于外部。

Updating to the below: 更新到以下内容:

 private JMenuItem createColoredSeparator(Color color){ JMenuItem separator = new JMenuItem(); separator.setPreferredSize(new Dimension(50,50)); //Increased to highlight whats happening //Imitate the default raised border Border raisedBorder = BorderFactory.createBevelBorder(BevelBorder.RAISED, UIManager.getColor("MenuItem.background"),UIManager.getColor("MenuItem.background").darker()); //Merge the borders with the raised border applied outside of the border concealing part of the background separator.setBorder(new CompoundBorder( raisedBorder, BorderFactory.createLineBorder(UIManager.getColor("MenuItem.background"), 8))); separator.setBackground(color); separator.setEnabled(false); //make the item un-clickable so menu doesn't close return separator; } 

provides the following: 提供以下内容:

在此处输入图片说明

Try to add css for <hr> like <hr style='height :5px;' /> 

您可以根据需要增加身高。

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

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