测试JMenuBar能不能直接添加JMenuItem 答案是不应该

257 阅读2分钟

测试JMenuBar能不能直接添加JMenuItem 答案是不应该

JMenuBar 用来实现下拉菜单 , 可直接添加 JMenu JPopupMenu 用来实现右键菜单 , 可直接添加 JMenu和JMenuItem JMenu是JMenuItem的子类,也是JMenuItem的容器, 可直接添加 JMenu和JMenuItem

JMenuBar应该添加JMenu, 它实现了自己的添加JMenu的方法 源码如下 ↓

	    /**
	     * Appends the specified menu to the end of the menu bar.
	     *
	     * @param c the JMenu component to add
	     * @return the menu component
	     */
	    public JMenu add(JMenu c) {
	        super.add(c);
	        return c;
	    }
	

JMenuBar的add(JMenu)和add(JMenuItem)是两个不同的方法

add(JMenu) 是 JMenuBar自己的add方法
add(JMenuItem) 是 继承 java.awt.Container 的方法 , 用于添加Component不是Menu专用的

JPopupMenu与JMenuBar的add方法的不同

javax.swing.JPopupMenu也实现了自己的add方法, 不同的是, JPopupMenu是add(JMenuItem); 而JMenu是JMenuItem的子类
所以JPopupMenu的add方法既能添加JMenu也能添加JMenuItem
源码:

    public JMenuItem add(JMenuItem menuItem) {
        super.add(menuItem);
        return menuItem;
    }

JMenuBar的add源代码

    /**
     * Appends the specified menu to the end of the menu bar.
     *
     * @param c the <code>JMenu</code> component to add
     * @return the menu component
     */
    public JMenu add(JMenu c) {
        super.add(c);
        return c;
    }

JPopupMenu的add源代码

    /**
     * Appends the specified menu item to the end of this menu.
     *
     * @param menuItem the <code>JMenuItem</code> to add
     * @return the <code>JMenuItem</code> added
     */
    public JMenuItem add(JMenuItem menuItem) {
        super.add(menuItem);
        return menuItem;
    }

测试代码1

package menu;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

public class 测试JMenuBar能不能直接添加JMenuItem结果是不应该 {
	
	static JFrame frame = new JFrame(new SecurityManager() {public String n() {return getClassContext()[1].getSimpleName();}}.n());
	
	static JMenuItem jMenuItem = new JMenuItem("JMenuItem被拉伸了,之后的JMenu被顶到了右边→");
	
	static JMenu jMenu = new JMenu("JMenu001");
	
	static JMenuBar jMenuBar = new JMenuBar();
	
	static {
		frame.addWindowListener(new WindowAdapter() {@Override public void windowClosing(WindowEvent e) {frame.dispose();System.exit(0);}});
		frame.setBounds(new Rectangle(new Point(100, 50), new Dimension(1024,768)));
		
		jMenuBar.add(jMenuItem); // 这个是继承 java.awt.Container 的方法
		jMenuBar.add(jMenu); // 这个是 JMenuBar 自己的方法
		jMenu = new JMenu("JMenu002"); jMenuBar.add(jMenu);
		jMenu = new JMenu("JMenu003"); jMenuBar.add(jMenu);
		
		frame.setJMenuBar(jMenuBar);
		
		JLabel jlb = new JLabel("""
<html><head><style>
	.DivC1{font-size:20px;}
	pre{font-size:14px; color:white; background-color:black;}

</style></head><body style="font-size:14px;">
	<div class="DivC1"><span style="color:#0099ff;">↑↑↑↑↑↑</span> 可以看得到, JMenuBar直接添加的JMenuItem显示不正常,被拉伸了</div>
	<div class="DivC1">JMenuBar应该添加JMenu, 它实现了自己的添加JMenu的方法 源码如下 ↓</div>
	<pre><code>
	    /**
	     * Appends the specified menu to the end of the menu bar.
	     *
	     * @param c the <code>JMenu</code> component to add
	     * @return the menu component
	     */
	    public JMenu add(JMenu c) {
	        super.add(c);
	        return c;
	    }
	</pre></code>
	<dl>
		<dt><h1>JMenuBar的add(JMenu)和add(JMenuItem)是两个不同的方法
		<dd>add(JMenu) 是 JMenuBar自己的add方法
		<dd>add(JMenuItem) 是 继承 java.awt.Container 的方法 , 用于添加Component不是Menu专用的
		<dt><h1>JPopupMenu与JMenuBar的add方法的不同
		<dd>javax.swing.JPopupMenu也实现了自己的add方法, 不同的是, JPopupMenu是add(JMenuItem); 而JMenu是JMenuItem的子类<br/>
		所以JPopupMenu的add方法既能添加JMenu也能添加JMenuItem
		<dd>源码:<pre><code>
    public JMenuItem add(JMenuItem menuItem) {
        super.add(menuItem);
        return menuItem;
    }</code></pre>
				
</body></html>
				""");
		
		jlb.setVerticalAlignment(JLabel.TOP);
		frame.getContentPane().add(jlb,BorderLayout.CENTER);
	}
	public  static  void main(String...arguments) {frame.setVisible(true);}

}

在这里插入图片描述