簡體   English   中英

調整JMenuItem的大小?

[英]Resizing a JMenuItem?

我目前正在嘗試自定義JMenuItems的大小以使其更大。 由此:

在此處輸入圖片說明

更像這樣:

在此處輸入圖片說明

從外觀上,我有幾種選擇:

1:擴展JMenuItem並覆蓋getPreferredSize()。 或者我想這樣做(這就是上面的圖像的實現方式):

    private JMenuItem getCustomMenuItem(String s){
    JMenuItem item = new JMenuItem(s){
        @Override
        public Dimension getPreferredSize(){
            return new Dimension(100, 20);
        }
    };
    return item;
}

JMenuItem print = getCustomMenuItem("Print");

但是我很確定這是不對的,因為布局管理器可能不尊重我給的尺寸。 我可能還必須編寫或重寫其他項,例如JCheckBoxMenuItems。

2:我還可以在每個菜單項之間添加水平支柱以處理垂直間距,並在標簽的每一側使用空格來處理菜單的寬度。 但這似乎很笨拙,當我想對齊文本和圖像menu.add(Box.createVerticalStrut(10))時可能會導致問題

3:或者我可以向每個menuItem添加復合邊框: menuItem.setBorder(BorderFactory.createCompoundBorder(menuItem.getBorder(), new EmptyBorder(5,5,5,5)))

4:或者以某種方式告訴用戶設置菜單項寬度的外觀-我不太了解。

還有其他方法嗎? 方法1可行,但我認為添加復合邊框可能是我應該采取的方法。 我想我可以擴展JMenuItem並將邊框添加到自定義項(這也允許我強制轉換JCheckBoxMenuItem)?

感謝您提供任何幫助,還沒有真正找到確切的答案,並且想知道在更通用的情況下自定義組件的最佳實踐是什么,同時避免了整個get / set preferredSize()陷阱。

我通常調用setIconTextGap(int)方法使菜單項看起來更好。

例:

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Locale;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class TestMenuItem
{
    public static void main (String [] a) {
        Locale.setDefault (Locale.ENGLISH);
        SwingUtilities.invokeLater (new Runnable () {
            @Override public void run () {
                try {
                    UIManager.setLookAndFeel (UIManager.getSystemLookAndFeelClassName ());
                    createAndShowGUI ();
                }
                catch (Exception e) {
                    e.printStackTrace ();
                }
            }
        });
    }
    private static void createAndShowGUI () {
        JFrame frame = new JFrame ("Test Menu Item");
        // --- JMenuBar ---
        JMenuBar menuBar = new JMenuBar ();
        JMenu menu = new JMenu ("File");
        menu.add (createMenuItem ("Print", KeyStroke.getKeyStroke (KeyEvent.VK_P, ActionEvent.ALT_MASK)));
        menu.add (createMenuItem ("Exit", KeyStroke.getKeyStroke (KeyEvent.VK_ESCAPE, 0)));
        JMenu anotherMenu = new JMenu ("View");
        JMenuItem longerMenuItem = createMenuItem ("Longer Item", null);
        longerMenuItem.setIconTextGap (80);
        anotherMenu.add (longerMenuItem);
        menuBar.add (menu);
        menuBar.add (anotherMenu);
        // --- Closing frame ---
        frame.setJMenuBar (menuBar);
        // frame.pack (); use pack in a real application !!
        frame.setSize (500, 400); // used just for convenience !!
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo (null);
        frame.setVisible (true);
    }
    public static JMenuItem createMenuItem (String text, KeyStroke keyStroke) {
        JMenuItem menuItem = new JMenuItem (text);
        if (keyStroke != null) menuItem.setAccelerator (keyStroke);
        menuItem.setIconTextGap (40);
        return menuItem;
    }
}

這是結果(值= 20):

JMenuItem差距測試(值= 20)

且差距較大(值= 80):

在此處輸入圖片說明

JMenuItemAbstractButton擴展而來,后者具有setMargin(...)方法:

menuItem.setMargin( new Insets(...) );

編輯:

一些LAF允許您設置此屬性:

UIManager.put("MenuItem.margin", new Insets(...));

請查看UIManager默認值以獲取更多信息。

JMenuItem item = new JMenuItem("FILE");
item.add(new JButton("FILE BUTTON"));
item.setLayout(new FlowLayout()); // set FlowLayout for item
item.setPreferredSize(new Dimension(200, 100));//set size here
menu.add(item);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM