生活随笔
收集整理的這篇文章主要介紹了
java使用教程——组件及事件处理——菜单(添加图标)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
菜單條JMenuBar
菜單JMenu
菜單項JMenuItem
menuFruit.addSeparator(); //在菜單添加分隔線
public class Example9_2 {public static void main(String args
[]) {WindowMenu win
=new WindowMenu("帶菜單的窗口",20,30,600,290);}
}
C:/Users/86156/OneDrive/圖片/水果照片java中用到/banana.jpg
路徑的書寫
C:\Users\86156\OneDrive\圖片\水果照片java中用到\banana.jpg
這倆都可以
import javax.swing.*;
public class WindowMenu extends JFrame {JMenuBar menubar
;JMenu menuFruit
;JMenuItem bananaItem
,pearItem
;JMenu appleMenu
;JMenuItem redAppleItem
,yellowAppleItem
;public WindowMenu(){} public WindowMenu(String s
,int x
,int y
,int w
,int h
) {init(s
);setLocation(x
,y
);setSize(w
,h
);setVisible(true);setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE
); }void init(String s
){setTitle(s
); menubar
=new JMenuBar(); menuFruit
=new JMenu("水果菜單"); bananaItem
=new JMenuItem("香蕉"); bananaItem
.setIcon(new ImageIcon("C:/Users/86156/OneDrive/圖片/水果照片java中用到/banana.jpg"));pearItem
=new JMenuItem("甜梨");pearItem
.setIcon(new ImageIcon("C:/Users/86156/OneDrive/圖片/水果照片java中用到/pear.jpg"));appleMenu
=new JMenu("蘋果"); redAppleItem
= new JMenuItem("紅蘋果");redAppleItem
.setIcon(new ImageIcon("C:/Users/86156/OneDrive/圖片/水果照片java中用到/redApple.jpg")); yellowAppleItem
= new JMenuItem("黃蘋果"); yellowAppleItem
.setIcon(new ImageIcon("C:/Users/86156/OneDrive/圖片/水果照片java中用到/yellowApple.png"));menuFruit
.add(bananaItem
); menuFruit
.add(pearItem
); menuFruit
.addSeparator(); menuFruit
.add(appleMenu
); appleMenu
.add(redAppleItem
); appleMenu
.add(yellowAppleItem
); menubar
.add(menuFruit
); setJMenuBar(menubar
); }
}
總結
以上是生活随笔為你收集整理的java使用教程——组件及事件处理——菜单(添加图标)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。