日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Swing-右键菜单

發布時間:2023/12/20 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Swing-右键菜单 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

MyFrame :

設計思路:

1、創建JPopup右鍵菜單對象;

2、添加內容面板JPanel;

3、創建一個自定義的右鍵菜單項目鍵,包括圖片+名字+事件;

4、將菜單項添加到右鍵菜單;

5、添加面板事件處理:當右鍵點擊面板時,出現右鍵菜單;

6、創建一個監聽器,用于表示點擊菜單項操作;

package swing01;import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.net.URL;import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.JToolBar;public class MyFrame extends JFrame {//彈出式菜單JPopupMenu popup=new JPopupMenu();public MyFrame(String title){super(title);//內容面板JPanel root=new JPanel();this.setContentPane(root);root.setLayout(new BorderLayout());//右鍵菜單popup.add( createMenuItem("ic_open.png", "fileOpen","打開"));popup.add( createMenuItem("ic_save.png", "fileSave","保存"));popup.add( createMenuItem("ic_saveas.png","fileSaveAs","另存為"));popup.addSeparator();popup.add( createMenuItem("ic_help.png", "fileHelp","幫助"));//添加鼠標響應事件,當點擊右鍵時,彈出菜單root.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {// TODO Auto-generated method stubsuper.mouseClicked(e);if(e.getButton()==MouseEvent.BUTTON3){popup.show(e.getComponent(), e.getX(), e.getY());}}});}protected JMenuItem createMenuItem(String iceName,String action,String text){JMenuItem item=new JMenuItem(text);item.setActionCommand(action);item.addActionListener(actionListener);if(iceName!=null){String imagePath="/images/"+iceName;URL imageURL=getClass().getResource(imagePath);item.setIcon(new ImageIcon(imageURL));}return item;}//創建一個監聽器//注意:actionListener是類的屬性private ActionListener actionListener=new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubString action=e.getActionCommand();System.out.println("執行命令:"+action);//當是打開命令時,再彈出一個窗口if(action.equals("fileOpen")){JOptionPane.showMessageDialog(MyFrame.this, action);}}};}

main

package swing01;import java.awt.Container; import java.awt.FlowLayout;import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel;public class MyDemo {private static void createGUI(){// JFrame指一個窗口,構造方法的參數為窗口標題JFrame frame = new MyFrame("Swing Demo");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 設置窗口的其他參數,如窗口大小frame.setSize(400, 300);// 顯示窗口frame.setVisible(true);}public static void main(String[] args){javax.swing.SwingUtilities.invokeLater(new Runnable() {public void run(){createGUI();}});} }

?

總結

以上是生活随笔為你收集整理的Swing-右键菜单的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。