Win 上带有 JMenuItem setHorizo​​ntalTextPosition 的双图标 [英] Double icons with JMenuItem setHorizontalTextPosition on Win

查看:35
本文介绍了Win 上带有 JMenuItem setHorizo​​ntalTextPosition 的双图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Windows 外观和感觉中使用 JMenuItem setHorizo​​ntalTextPosition(SwingConstants.LEFT) 时呈现两个图标.它适用于默认的 Java 外观.

我刚刚提交了一份 Java 错误报告,在这里发布给遇到同样问题的其他人.

有人有其他解决方法建议吗?

import java.awt.Color;导入 java.awt.Dimension;导入 java.awt.Graphics;导入 java.awt.image.BufferedImage;导入 javax.swing.ImageIcon;导入 javax.swing.JFrame;导入 javax.swing.JMenu;导入 javax.swing.JMenuBar;导入 javax.swing.JMenuItem;导入 javax.swing.SwingConstants;导入 javax.swing.UIManager;公共类 WinMenuItemIcon {公共静态无效主(字符串 [] args){//注意:错误发生在 Windows L&FString name = UIManager.getSystemLookAndFeelClassName();尝试 {UIManager.setLookAndFeel( 名称 );} 捕获(异常 e){e.printStackTrace();}JFrame frame = new JFrame();frame.setTitle(测试");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JMenuBar menuBar = new JMenuBar();JMenu menu = new JMenu("Menu");ImageIcon 图标 = createIcon();JMenuItem menuItem = new JMenuItem("Command", icon);menuItem.setHorizo​​ntalTextPosition(SwingConstants.LEFT);menu.add(menuItem);menuBar.add(菜单);frame.setJMenuBar(menuBar);frame.setPreferredSize(new Dimension(500, 500));框架.pack();frame.setVisible(true);}受保护的静态 ImageIcon createIcon() {BufferedImage bi = new BufferedImage(25,25,BufferedImage.TYPE_INT_ARGB);图形 g = bi.createGraphics();g.setColor(Color.RED);g.fillOval(0,0, 25, 25);返回新的 ImageIcon(bi);}}

解决方案

  • 我的环境:Windows 10 64bit + JDK 1.8.0_72
  • 我不确定这是否是一个错误... 现在这个错误似乎已修复:

    import java.awt.*;导入 java.awt.image.BufferedImage;导入 javax.swing.*;公共类 WinMenuItemIconTest {私有静态 JMenuBar makeManuBar() {JMenuItem menuItem0 = new JMenuItem("Command", createIcon());JMenuItem menuItem1 = new JMenuItem("LEFT bug?", createIcon());menuItem1.setHorizo​​ntalTextPosition(SwingConstants.LEFT);//menuItem1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);JMenuItem menuItem2 = new JMenuItem("CENTER bug?", createIcon());menuItem2.setHorizo​​ntalTextPosition(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(菜单);返回菜单栏;}公共静态无效主(字符串 [] args){EventQueue.invokeLater(() -> {//注意:错误发生在 Windows L&FString name = UIManager.getSystemLookAndFeelClassName();尝试 {UIManager.setLookAndFeel(name);} 捕获(异常 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);});}受保护的静态 ImageIcon createIcon() {BufferedImage bi = new BufferedImage(25, 25, BufferedImage.TYPE_INT_ARGB);图形 g = bi.createGraphics();g.setColor(Color.RED);g.fillOval(0, 0, 25, 25);g.dispose();返回新的 ImageIcon(bi);}}

    Two icons are rendered when using JMenuItem setHorizontalTextPosition(SwingConstants.LEFT) with Windows Look and Feel. It works fine with the default Java Look and Feel.

    I just filed a Java bug report, posting here for anyone else having the same problem.

    Does anyone have another workaround to suggest?

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

    解决方案

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

    这篇关于Win 上带有 JMenuItem setHorizo​​ntalTextPosition 的双图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆