當使用JMenuItem
setHorizontalTextPosition(SwingConstants.LEFT)
時,將呈現兩個圖標與Windows外觀和感覺。它適用於默認的Java外觀和感覺。JMenuItem上的雙圖標setHorizontalTextPosition在Win
我剛剛提交了一個Java bug報告,在這裏發佈給其他人有同樣的問題。
有沒有人有另一種解決方法建議?謝謝。
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);
}
}
可能會有所幫助張貼的截圖!如果有的話也鏈接錯誤報告頁面! – flakes