JavaAwtSwing右键菜单 javax.swing.JPopupMenu javax.swing.JMenuItem javax.swing.JMenu

javax.swing.JPopupMenu 可以用于右键弹出菜单

类声明
public class JPopupMenu extends JComponent implements Accessible,MenuElement { 继承自 javax.swing.JComponent
实例化方法
JPopupMenu jPopupMenu = new JPopupMenu();
JPopupMenu jPopupMenu = new JPopupMenu("好像看不见");
添加JMenu和JMenuItem
JMenu是JMenuItem的子类, JMenu也可作为JMenuItem的容器
JPopupMenu和JMenu有自己的add方法, 和祖宗Component的add方法没有关系
JPopupMenu和JMenu有简化的add(String)方法,一步生成并添加MenuItem , 源码 public JMenuItem add(String s) { return add(new JMenuItem(s)); }
JPopupMenu和JMenu获取子部件的方法不一样
JPopupMenu获取子部件和数量用继承自java.awt.Container的 getComponent(int i) 和 getComponentCount() 方法
JMenu获取子部件用自己的 getMenuComponent(int i) 和 getMenuComponentCount() 方法

JComponent和它的子类拥有 public void setComponentPopupMenu(JPopupMenu popup) 方法用于添加JPopupMenu为右键菜单

例如: jLabel.setComponentPopupMenu(jPopupMenu);

示例代码1

package menu;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Swing右键弹出菜单JPopupMenu2206012039 {
	
	static void pln(Object o) {System.out.println(o);}
	
	static Frame frame = new Frame(new SecurityManager() {public String nm() {return this.getClassContext()[1].getSimpleName();} }.nm());
	
	static JLabel jLabel = new JLabel();
	
	static JPopupMenu jPopupMenu = new JPopupMenu("好像看不见");
	
	static JMenu jMenu = new JMenu("JMenu");
	
	static JMenuItem jMenuItem = new JMenuItem("JMenuItem");
	
	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(1600, 900)));
		frame.add(jLabel);
		
		jLabel.setComponentPopupMenu(jPopupMenu); //setComponentPopupMenu源自JComponent, Jlabe是它的子类
		jPopupMenu.add("JPopupMenu可以直接add(String)创建并添加MenuItem");
		jPopupMenu.add(jMenu);
		jMenu.add("JMenu也可以直接add(String)创建并添加MenuItem");
		jMenu.add(jMenuItem); for(int c=1;c<10;c++) {jMenu.add("JMenu下的JMenuItem-"+c); jPopupMenu.add("JPopupMenu下的JMenuItem-"+c);}
		pln(jPopupMenu.getComponentCount()); // 结果不为0
		pln(jMenu.getComponentCount()); // 结果为0
		pln(jMenu.getMenuComponentCount());
		for(int i=0; i<jPopupMenu.getComponentCount(); i++) {
			JMenuItem mi = (JMenuItem)jPopupMenu.getComponent(i);
			mi.addActionListener(ev->{JOptionPane.showConfirmDialog(frame, mi.getText());});
		}
		for(int i=0; i<jMenu.getMenuComponentCount();i++) {
			JMenuItem mi = (JMenuItem)jMenu.getMenuComponent(i);
			mi.addActionListener(ev->{JOptionPane.showMessageDialog(mi, mi.getText());}  );
		}
		
		
		jLabel.setText("""
				
					好像看不到title
					
				
					

javax.swing.JPopupMenu 可以用于右键弹出菜单

类声明
public class JPopupMenu extends JComponent implements Accessible,MenuElement { 继承自 javax.swing.JComponent
实例化方法
JPopupMenu jPopupMenu = new JPopupMenu();
JPopupMenu jPopupMenu = new JPopupMenu("好像看不见");
添加JMenu和JMenuItem
JMenu是JMenuItem的子类, JMenu也可作为JMenuItem的容器
JPopupMenu和JMenu有自己的add方法, 和祖宗Component的add方法没有关系
JPopupMenu和JMenu有简化的add(String)方法,一步生成并添加MenuItem , 源码 public JMenuItem add(String s) { return add(new JMenuItem(s)); }
JPopupMenu和JMenu获取子部件的方法不一样
JPopupMenu获取子部件和数量用继承自java.awt.Container的 getComponent(int i) 和 getComponentCount() 方法
JMenu获取子部件用自己的 getMenuComponent(int i) 和 getMenuComponentCount() 方法

JComponent和它的子类拥有 public void setComponentPopupMenu(JPopupMenu popup) 方法用于添加JPopupMenu为右键菜单

例如: jLabel.setComponentPopupMenu(jPopupMenu); """
); jLabel.setVerticalAlignment(JLabel.TOP); } public static void main(String...arguments) { frame.setVisible(true); } }

效果

JavaAwtSwing右键菜单 javax.swing.JPopupMenu javax.swing.JMenuItem javax.swing.JMenu_第1张图片

你可能感兴趣的:(JAVA,#,Java图形Awt,Swing,JavaFX,java,开发语言)