Java詳細(xì)解析下拉菜單和彈出菜單的使用
Swing菜單組件
下拉式菜單介紹
創(chuàng)建一個下拉式菜單,需要三個組件,JmenuBar(菜單欄),Jmenu(菜單),Jmenultem(菜單項(xiàng))。
下拉式菜單的三個組件
JMenuBar(菜單欄)
表示一個水平的菜單欄,用來管理菜單,不參與同用戶的交互式操作,菜單欄可以放在容器的任何位置,但通常情況下會使用頂級窗口的setJMenuBar方法將它放置在頂級窗口的頂部,JMenuBar有一個午無參的構(gòu)造函數(shù),創(chuàng)建菜單欄時,只需要使用new關(guān)鍵字創(chuàng)建JMenuBar對象,創(chuàng)建完成之后,可以調(diào)用add(JMenu c)方法添加JMenu菜單。
JMenu(菜單)
用來整合管理菜單項(xiàng),菜單可以是單一層次的結(jié)構(gòu),也可以是多層次的結(jié)構(gòu),可以使用JMenu(String text)創(chuàng)建JMenu菜單,其中的參數(shù)text表示菜單上的文本。
菜單的常用方法
| 方法聲明 | 功能描述 |
|---|---|
| void JMenuItem add(JMenuItem menuItem) | 將菜單項(xiàng)添加到菜單末尾,返回此菜單項(xiàng) |
| void addSeparator() | 將分隔符添加到菜單末尾 |
| JMenuItem getItem(int pos) | 返回指定索引處的菜單項(xiàng),第一個菜單項(xiàng)的索引為0 |
| int getItemCount() | 返回菜單上的項(xiàng)數(shù),菜單項(xiàng)和分隔符都計(jì)算在內(nèi) |
| void JMenuItem insert(JmuenuItem menuItem,int pos) | 在指定索引處插入菜單項(xiàng) |
| void insertsertSeparator(int pos) | 在指定索引處插入分隔符 |
| void remove(int pos) | 從菜單中移除指定索引處的菜單項(xiàng) |
| void remove(JMenuItem menuItem) | 從菜單中移除指定的菜單項(xiàng) |
| void removeAll() | 從菜單中移除所有的菜單項(xiàng) |
JMenuItem(菜單項(xiàng))
在創(chuàng)建菜單項(xiàng)時,通常會采用構(gòu)造方法JMenuItem為菜單項(xiàng)指定文本內(nèi)容。因?yàn)镴MenuItem是繼承AbstractButton類的,所以可以吧它看作一個按鈕,可以用無參的構(gòu)造方法創(chuàng)建一個菜單項(xiàng),可以調(diào)用從AbstractButton類中繼承的setText(String text)方法和setItem()方法為其設(shè)置文本和圖標(biāo)。
下拉式菜單的創(chuàng)建與使用
package Swing;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CaiDan extends JFrame {
public CaiDan(){
//創(chuàng)建菜單欄
JMenuBar menuBar=new JMenuBar();
//將菜單欄添加到JFrame窗口中
this.setJMenuBar(menuBar);
//創(chuàng)建菜單
JMenu menu=new JMenu("操作");
//將菜單添加到菜單欄上
menuBar.add(menu);
//創(chuàng)建兩個菜單項(xiàng)
JMenuItem item1=new JMenuItem("彈出窗口");
JMenuItem item2=new JMenuItem("關(guān)閉");
//為菜單項(xiàng)添加事件監(jiān)聽器
item1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//創(chuàng)建一個JDialog窗口
JDialog dialog=new JDialog(CaiDan.this,true);
dialog.setTitle("彈出窗口");
dialog.setSize(200,200);
dialog.setLocation(50,50);
dialog.setVisible(true);
}
});
item2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
//將菜單項(xiàng)添加到菜單中
menu.add(item1);
menu.addSeparator();
menu.add(item2);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300,300);
this.setVisible(true);
}
public static void main(String[] args) {
new CaiDan();
}
}
創(chuàng)建和添加下拉式菜單的一般步驟
- 創(chuàng)建一個JMenuBar菜單欄對象,將其放置在JFrame窗口的頂部
- 創(chuàng)建JMenu菜單對象,將其添加到JMenuBar菜單欄中
- 創(chuàng)建JMenuItem菜單項(xiàng),將其添加到JMenuBar菜單中。
彈出式菜單介紹
在系統(tǒng)中單擊右鍵出現(xiàn)的菜單就是彈出式菜單,在Java的Swing組件中,彈出式菜單用JPopupMenu表示。通過add()方法添加JMenuItem菜單項(xiàng),默認(rèn)是不可見的。
彈出式菜單的創(chuàng)建與使用
package Swing;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class TanChu extends JFrame {
private JPopupMenu popupMenu;
public TanChu(){
//創(chuàng)建一個JPopupMenu菜單
popupMenu=new JPopupMenu();
//創(chuàng)建三個JMenuItem菜單項(xiàng)
JMenuItem refreshItem=new JMenuItem("refresh");
JMenuItem createItem=new JMenuItem("create");
JMenuItem exitItem=new JMenuItem("exit");
//為exitItem菜單項(xiàng)添加事件監(jiān)聽器
exitItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
//向JPopupMenu菜單添加菜單項(xiàng)
popupMenu.add(refreshItem);
popupMenu.add(createItem);
popupMenu.addSeparator();
popupMenu.add(exitItem);
//向JFrame窗口添加clicked鼠標(biāo)事件監(jiān)聽器
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//通過值匹配,當(dāng)點(diǎn)擊右鍵的時候,顯示JPopupMenu菜單
if(e.getButton()==e.BUTTON3){
popupMenu.show(e.getComponent(),e.getX(),e.getY());
}
}
});
this.setSize(300,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
new TanChu();
}
}
小結(jié)
以上就是我對于Java中幾種常見菜單的理解,以便于我們之后在不同開發(fā)中根據(jù)不同的情況設(shè)置不同的菜單,提高用戶的體驗(yàn)感。
到此這篇關(guān)于Java詳細(xì)解析下拉菜單和彈出菜單的使用的文章就介紹到這了,更多相關(guān)Java菜單內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)異步執(zhí)行的8種方式總結(jié)
這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)異步執(zhí)行的8種方式,異步編程不會阻塞程序的執(zhí)行,它將耗時的操作提交給后臺線程或其他執(zhí)行環(huán)境,并立即返回,使得程序可以繼續(xù)執(zhí)行其他任務(wù),需要的朋友可以參考下2023-09-09
源碼解析springbatch的job運(yùn)行機(jī)制
這篇文章主要介紹了springbatch的job是如何運(yùn)行的,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
Spring Boot中的屬性綁定的實(shí)現(xiàn)
這篇文章主要介紹了Spring Boot中的屬性綁定的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
解決springboot服務(wù)啟動報(bào)錯:Unable?to?start?embedded?contain
這篇文章主要介紹了解決springboot服務(wù)啟動報(bào)錯:Unable?to?start?embedded?contain的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
詳解Springboot2.3集成Spring security 框架(原生集成)
這篇文章主要介紹了詳解Springboot2.3集成Spring security 框架(原生集成),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08

