- 相關(guān)推薦
Java菜單和工具欄學(xué)習(xí)教程
引導(dǎo)語(yǔ):工具欄是顯示位圖式按鈕行的控制條,位圖式按鈕用來(lái)執(zhí)行命令。以下是百分網(wǎng)小編分享給大家的Java菜單和工具欄學(xué)習(xí)教程,歡迎閱讀!
1.1 菜單和工具欄
菜單和工具欄可提供簡(jiǎn)單明了的指示說(shuō)明,讓用戶非常方便的完成軟件操作。利用菜單可以將程序功能模塊化。
1.1.1 JMenuBar 菜單
菜單的組織方式為:一個(gè)菜單條 (JMenuBar)中可以包含多個(gè)菜單(JMenu),一個(gè)菜單中可以包含多個(gè)菜單項(xiàng)(JMenuItem及其子類)。有一些支持菜單的組件,如JFrame、JDialog以及JApplet,都有一個(gè)setMenuBar(JMenuBar bar)方法,可以利用這個(gè)方法來(lái)設(shè)置菜單條。
菜單項(xiàng)是菜單系統(tǒng)中最基本的組件,用戶與菜單的交互主要是菜單項(xiàng)的交互,因此事件處理也是針對(duì)菜單項(xiàng)的。當(dāng)用戶選擇了某個(gè)菜單項(xiàng),就會(huì)觸發(fā)一個(gè)ActionEvent事件,可以編寫相應(yīng)的類實(shí)現(xiàn)ActionListener接口對(duì)該事件進(jìn)行處理。
例1-1演示了如何創(chuàng)建一個(gè)完整的菜單系統(tǒng),可以通過(guò)點(diǎn)擊菜單項(xiàng)讓菜單項(xiàng)做出反應(yīng)。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class JMenuBarTest extends JFrame{
private JMenuBar bar = new JMenuBar();
private JMenu menuFile = new JMenu("文件");
private JMenuItem itemFile1 = new JMenuItem("新建");
private JMenuItem itemFile2 = new JMenuItem("打開");
private JMenuItem itemFile3 = new JMenuItem("保存");
private JMenuItem itemFile4 = new JMenuItem("退出");
private JMenu menuHelp = new JMenu("幫助");
private JMenuItem itemHelp1 = new JMenuItem("幫助主題");
private JMenuItem itemHelp2 = new JMenuItem("關(guān)于記事本");
private JTextArea ta = new JTextArea(10,30);
public JMenuBarTest(String title){
super(title);
//設(shè)置快捷鍵
itemFile1.setAccelerator(KeyStroke.getKeyStroke('N',KeyEvent.CTRL_MASK));
itemFile2.setAccelerator(KeyStroke.getKeyStroke('O',KeyEvent.CTRL_MASK));
itemFile3.setAccelerator(KeyStroke.getKeyStroke('S',KeyEvent.CTRL_MASK));
itemFile4.setAccelerator(KeyStroke.getKeyStroke('E',KeyEvent.CTRL_MASK));
//添加JMenuItem到JMenu
menuFile.add(itemFile1);
menuFile.add(itemFile2);
menuFile.add(itemFile3);
menuFile.addSeparator();//加分割線
menuFile.add(itemFile4);
menuHelp.add(itemHelp1);
menuHelp.addSeparator();//加分割線
menuHelp.add(itemHelp2);
//添加JMenu到JBar
this.setJMenuBar(bar);
bar.add(menuFile);
bar.add(menuHelp);
Container contentPane = this.getContentPane();
contentPane.add(ta);
pack();
this.setVisible(true);
//注冊(cè)監(jiān)測(cè)器
itemFile1.addActionListener(new MyActionListener());
itemFile2.addActionListener(new MyActionListener());
itemFile3.addActionListener(new MyActionListener());
itemFile4.addActionListener(new MyActionListener());
itemHelp1.addActionListener(new MyActionListener());
itemHelp2.addActionListener(new MyActionListener());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
ta.setText("您按下了菜單項(xiàng):"+e.getActionCommand());
}
}
}
public class Test1_1 {
public static void main(String[] args) {
new JMenuBarTest("記事本");
}
}
1.1.2 JToolBar 工具欄
JToolBar(工具欄)是提供快速訪問(wèn)常用菜單命令的一個(gè)按鈕欄,一般和菜單欄一起出現(xiàn),當(dāng)然也可獨(dú)立出現(xiàn)。
JToolBar提供了四個(gè)構(gòu)造方法用于創(chuàng)建JToolBar對(duì)象。
表1-13 JToolBar構(gòu)造方法
構(gòu)造方法說(shuō)明
JToolBar()創(chuàng)建新的工具欄;默認(rèn)的方向?yàn)?HORIZONTAL
JToolBar(int orientation)創(chuàng)建具有指定 orientation 的新工具欄
JToolBar(String name)創(chuàng)建一個(gè)具有指定 name 的新工具
JToolBar(String name,
int orientation)
創(chuàng)建一個(gè)具有指定 name 和 orientation 的新工具欄各參數(shù)意義:
name - 工具欄的名稱
orientation - 初始方向,值可為 HORIZONTAL(水平方向) 或 VERTICAL (垂直方向)
工具欄的添加很簡(jiǎn)單,直接使用JFrame的add方法即可完成添加,工具欄內(nèi)可添加按鈕等組件。
例1-9演示了單獨(dú)的一個(gè)工具欄,該程序未添加事件處理,若要添加事件處理,實(shí)際上是對(duì)添加到工具欄內(nèi)的組件的事件處理,如添加JButton則可處理ActionEvent事件。
[例1-9]
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class JToolBarTest extends JFrame{
private JToolBar tb = new JToolBar();
private JButton[] tbButtons;
public JToolBarTest(){
String[] images = {"1.jpg","2.jpg"};
//創(chuàng)建ImageIcon數(shù)組
ImageIcon[] toolImage = new ImageIcon[images.length];
tbButtons = new JButton[images.length];
for(int i=0;i //ImageIcon數(shù)組每個(gè)元素初始化
toolImage[i] = new ImageIcon("bin"+images[i]);
//創(chuàng)建帶有圖標(biāo)的JButton
tbButtons[i] = new JButton(toolImage[i]);
//將帶有圖標(biāo)的JButton添加到工具欄
tb.add(tbButtons[i]);
}
this.add(tb);//添加工具欄到JFrame
pack();
setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class Test1_9 {
public static void main(String[] args) {
new JToolBarTest();
}
}
1.1.3 工具提示
工具欄的一個(gè)缺點(diǎn)是用戶常常要猜測(cè)他上面的小圖標(biāo)按鈕所代表的含義。為了解決該問(wèn)題,java提供了工具提示。當(dāng)鼠標(biāo)在一個(gè)按鈕上停留一段時(shí)間后,工具提示就會(huì)被激活。工具提示文本顯示在一個(gè)有顏色的矩形內(nèi),當(dāng)鼠標(biāo)移開按鈕后,工具提示消失。
工具提示并不是只在工具欄中可用,所有的Swing組件都支持工具提示,也就是說(shuō)你可以在JButton、JList等都可以設(shè)置工具提示。工具提示是由ToolTipManager來(lái)維護(hù)的,我們可以通過(guò)這個(gè)類來(lái)設(shè)置從光標(biāo)開始停留在組件上到顯示工具提示之間的時(shí)間間隔以及顯示工具提示信息的時(shí)長(zhǎng)。
修改例1-9,給工具欄添加工具提示,并利用ToolTipManager類來(lái)控制工具顯示時(shí)間。
[例1-10]
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class JToolBarTest extends JFrame{
private JToolBar tb = new JToolBar();
private JButton[] tbButtons;
public JToolBarTest(String title){
super(title);
String[] images = {"1.jpg","2.jpg"};
//創(chuàng)建ImageIcon數(shù)組
ImageIcon[] toolImage = new ImageIcon[images.length];
tbButtons = new JButton[images.length];
for(int i=0;i //ImageIcon數(shù)組每個(gè)元素初始化
toolImage[i] = new ImageIcon("bin"+images[i]);
//創(chuàng)建帶有圖標(biāo)的JButton
tbButtons[i] = new JButton(toolImage[i]);
//將帶有圖標(biāo)的JButton添加到工具欄
tb.add(tbButtons[i]);
}
//設(shè)置工具提示
tbButtons[0].setToolTipText("刪除");
tbButtons[1].setToolTipText("取消");
//設(shè)置從光標(biāo)開始停留在組件上到顯示工具提示之間的時(shí)間間隔為0.1秒
ToolTipManager.sharedInstance().setInitialDelay(100);
//設(shè)置工具提示信息顯示時(shí)長(zhǎng)為1秒
ToolTipManager.sharedInstance().setDismissDelay(1000);
this.add(tb);//添加工具欄到JFrame
pack();
setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class Test1_10 {
public static void main(String[] args) {
new JToolBarTest("JToolBar測(cè)試");
}
}
【Java菜單和工具欄學(xué)習(xí)教程】相關(guān)文章:
Java的特點(diǎn)學(xué)習(xí)教程01-22
Java數(shù)組的基礎(chǔ)學(xué)習(xí)教程08-12
Java數(shù)組的基本學(xué)習(xí)教程03-01
photoshop工具欄教程02-01
java常量和變量入門教程04-01
Java入門教程:常量和變量02-10
Dreamweaver創(chuàng)建跳轉(zhuǎn)菜單教程03-21
java教程之Java編程基礎(chǔ)04-18