📄 frame1.java
字号:
package popnenu;import java.awt.*;import java.awt.event.*;import javax.swing.*;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2002</p> * <p>Company: </p> * @author unascribed * @version 1.0 */public class Frame1 extends JFrame { JPanel contentPane; BorderLayout borderLayout1 = new BorderLayout(); PopupMenu popupMenu1 = new PopupMenu(); Menu menu1 = new Menu(); MenuItem menuItem1 = new MenuItem(); MenuItem menuItem2 = new MenuItem(); Menu menu2 = new Menu(); MenuItem menuItem3 = new MenuItem(); MenuItem menuItem4 = new MenuItem(); MenuItem menuItem5 = new MenuItem(); //Construct the frame public Frame1() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } //Component initialization private void jbInit() throws Exception { //setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]"))); contentPane = (JPanel) this.getContentPane(); contentPane.setLayout(borderLayout1); this.setResizable(false); this.setSize(new Dimension(400, 300)); this.setTitle("弹出式菜单"); this.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(MouseEvent e) { this_mouseClicked(e); } }); menu1.setLabel("改变颜色"); menuItem1.setLabel("blue"); menuItem1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { menuItem1_actionPerformed(e); } }); menuItem2.setLabel("red"); menuItem2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { menuItem2_actionPerformed(e); } }); menu2.setLabel("改变标题"); menuItem3.setLabel("中文"); menuItem3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { menuItem3_actionPerformed(e); } }); menuItem4.setLabel("英文"); menuItem4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { menuItem4_actionPerformed(e); } }); menuItem5.setLabel("退出"); menuItem5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { menuItem5_actionPerformed(e); } }); popupMenu1.add(menu1); popupMenu1.add(menu2); popupMenu1.add(menuItem5); menu1.add(menuItem1); menu1.add(menuItem2); menu2.add(menuItem3); menu2.add(menuItem4); } //Overridden so we can exit when window is closed protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } void this_mouseClicked(MouseEvent e) {this.add(popupMenu1); if(e.getModifiers()==Event.META_MASK){ popupMenu1.show(this,e.getX(),e.getY());} } void menuItem1_actionPerformed(ActionEvent e) { setBackground(Color.blue); } void menuItem2_actionPerformed(ActionEvent e) { setBackground(Color.red); } void menuItem3_actionPerformed(ActionEvent e) {this.setTitle("弹出式菜单显示"); } void menuItem4_actionPerformed(ActionEvent e) { this.setTitle("Show Popup Menu "); } void menuItem5_actionPerformed(ActionEvent e) { System.exit(0); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -