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

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

}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kfepiza

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值