Win 上带有 JMenuItem setHorizo​​ntalTextPosition 的双图标

Double icons with JMenuItem setHorizontalTextPosition on Win

使用 JMenuItem setHorizontalTextPosition(SwingConstants.LEFT) 和 Windows 外观时会呈现两个图标。它在默认 Java 外观和感觉下运行良好。

我刚刚提交了一份 Java 错误报告,在此发布以供遇到相同问题的其他人使用。

是否有人可以建议其他解决方法?

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingConstants;
import javax.swing.UIManager;


public class WinMenuItemIcon {

public static void main(String[] args) {
    
    //NOTE: Bug happens with Windows L&F
    String name = UIManager.getSystemLookAndFeelClassName();
    try {
        UIManager.setLookAndFeel( name );
    } catch (Exception e) {
        e.printStackTrace();
    }

    JFrame frame = new JFrame();
    frame.setTitle("Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("Menu");
    
    ImageIcon icon = createIcon();
    
    JMenuItem menuItem = new JMenuItem("Command", icon);
    menuItem.setHorizontalTextPosition(SwingConstants.LEFT);
    menu.add(menuItem);
    menuBar.add(menu);

    frame.setJMenuBar(menuBar);
    frame.setPreferredSize(new Dimension(500, 500));
    frame.pack();
    frame.setVisible(true);
}

protected static ImageIcon createIcon() {
    BufferedImage bi = new BufferedImage(25,25,BufferedImage.TYPE_INT_ARGB);
    Graphics g = bi.createGraphics();
    g.setColor(Color.RED);
    g.fillOval(0,0, 25, 25);
    return new ImageIcon(bi);
}

}

删除行

 menuItem.setHorizontalTextPosition(SwingConstants.LEFT);

你将只有 1 个图标

或使用 if 语句排除该行 windows 外观如下

if(!UIManager.getLookAndFeel().equals("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"))
{
     menuItem.setHorizontalTextPosition(SwingConstants.LEFT);
}

这是 @aterai 代码的替代方案,如果根据您的需要稍微调整一下,它会更好地工作,可能是一些错误,但在这里这样做我不确定 @aterai 需要中心看起来如何但是这是我的假设:-

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class WinMenuItemIconTest {
private static JMenuBar makeManuBar() {

    JMenuItem menuItem0 = new JMenuItem("Command", createIcon()); 

    JMenuItem menuItem1 = new JMenuItem("LEFT bug?", createIcon()); // always left by defaulr


    JMenuItem menuItem2 = new JMenuItem("CENTER bug?", createIcon());

     menuItem2.setMargin(new Insets(5, 50, 5, 5)); // using set margin to centerise 

    JMenuItem menuItem3 = new JMenuItem("RIGHT_TO_LEFT", createIcon());
    menuItem3.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); // Right to left seems to work

    JMenu menu = new JMenu("Menu");
    menu.add(menuItem0);
    menu.add(menuItem1);
    menu.add(menuItem2);
    menu.add(menuItem3);

    JMenuBar menuBar = new JMenuBar();
    menuBar.add(menu);
    return menuBar;

  }
      public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
          //NOTE: Bug happens with Windows L&F
          String name = UIManager.getSystemLookAndFeelClassName();
          try {
            UIManager.setLookAndFeel(name);
          } catch (Exception e) {
            e.printStackTrace();
          }
          JFrame frame = new JFrame("Test");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setJMenuBar(makeManuBar());
          frame.setSize(320, 240);
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
        });
      }
      protected static ImageIcon createIcon() {
        BufferedImage bi = new BufferedImage(25, 25, BufferedImage.TYPE_INT_ARGB);
        Graphics g = bi.createGraphics();
        g.setColor(Color.RED);
        g.fillOval(0, 0, 25, 25);
        return new ImageIcon(bi);
      }
    }

注意:- 我也在 Windows 10

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class WinMenuItemIconTest {
  private static JMenuBar makeManuBar() {
    JMenuItem menuItem0 = new JMenuItem("Command", createIcon());

    JMenuItem menuItem1 = new JMenuItem("LEFT bug?", createIcon());
    menuItem1.setHorizontalTextPosition(SwingConstants.LEFT);
    //menuItem1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

    JMenuItem menuItem2 = new JMenuItem("CENTER bug?", createIcon());
    menuItem2.setHorizontalTextPosition(SwingConstants.CENTER);

    JMenuItem menuItem3 = new JMenuItem("RIGHT_TO_LEFT", createIcon());
    menuItem3.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

    JMenu menu = new JMenu("Menu");
    menu.add(menuItem0);
    menu.add(menuItem1);
    menu.add(menuItem2);
    menu.add(menuItem3);

    JMenuBar menuBar = new JMenuBar();
    menuBar.add(menu);
    return menuBar;
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(() -> {
      //NOTE: Bug happens with Windows L&F
      String name = UIManager.getSystemLookAndFeelClassName();
      try {
        UIManager.setLookAndFeel(name);
      } catch (Exception e) {
        e.printStackTrace();
      }
      JFrame frame = new JFrame("Test");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setJMenuBar(makeManuBar());
      frame.setSize(320, 240);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
    });
  }
  protected static ImageIcon createIcon() {
    BufferedImage bi = new BufferedImage(25, 25, BufferedImage.TYPE_INT_ARGB);
    Graphics g = bi.createGraphics();
    g.setColor(Color.RED);
    g.fillOval(0, 0, 25, 25);
    g.dispose();
    return new ImageIcon(bi);
  }
}

从左到右对齐图标和标签文本:

  JMenuItem menuItem3 = new JMenuItem("LEFT_TO_RIGHT", createIcon());
  menuItem3.setHorizontalTextPosition(JMenuItem.RIGHT);
  menuItem3.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);

上面的代码对我有用。