<address id="ousso"></address>
<form id="ousso"><track id="ousso"><big id="ousso"></big></track></form>
  1. java語言

    Java菜單和工具欄學習教程

    時間:2025-04-05 19:00:59 java語言 我要投稿
    • 相關推薦

    Java菜單和工具欄學習教程

      引導語:工具欄是顯示位圖式按鈕行的控制條,位圖式按鈕用來執行命令。以下是百分網小編分享給大家的Java菜單和工具欄學習教程,歡迎閱讀!

    Java菜單和工具欄學習教程

      1.1 菜單和工具欄

      菜單和工具欄可提供簡單明了的指示說明,讓用戶非常方便的完成軟件操作。利用菜單可以將程序功能模塊化。

      1.1.1 JMenuBar 菜單

      菜單的組織方式為:一個菜單條 (JMenuBar)中可以包含多個菜單(JMenu),一個菜單中可以包含多個菜單項(JMenuItem及其子類)。有一些支持菜單的組件,如JFrame、JDialog以及JApplet,都有一個setMenuBar(JMenuBar bar)方法,可以利用這個方法來設置菜單條。

      菜單項是菜單系統中最基本的組件,用戶與菜單的交互主要是菜單項的交互,因此事件處理也是針對菜單項的。當用戶選擇了某個菜單項,就會觸發一個ActionEvent事件,可以編寫相應的類實現ActionListener接口對該事件進行處理。

      例1-1演示了如何創建一個完整的菜單系統,可以通過點擊菜單項讓菜單項做出反應。

      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("關于記事本");

      private JTextArea ta = new JTextArea(10,30);

      public JMenuBarTest(String title){

      super(title);

      //設置快捷鍵

      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);

      //注冊監測器

      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("您按下了菜單項:"+e.getActionCommand());

      }

      }

      }

      public class Test1_1 {

      public static void main(String[] args) {

      new JMenuBarTest("記事本");

      }

      }

      1.1.2 JToolBar 工具欄

      JToolBar(工具欄)是提供快速訪問常用菜單命令的一個按鈕欄,一般和菜單欄一起出現,當然也可獨立出現。

      JToolBar提供了四個構造方法用于創建JToolBar對象。

      表1-13 JToolBar構造方法

      構造方法說明

      JToolBar()創建新的工具欄;默認的方向為 HORIZONTAL

      JToolBar(int orientation)創建具有指定 orientation 的新工具欄

      JToolBar(String name)創建一個具有指定 name 的新工具

      JToolBar(String name,

      int orientation)

      創建一個具有指定 name 和 orientation 的新工具欄各參數意義:

      name - 工具欄的名稱

      orientation - 初始方向,值可為 HORIZONTAL(水平方向) 或 VERTICAL (垂直方向)

      工具欄的添加很簡單,直接使用JFrame的add方法即可完成添加,工具欄內可添加按鈕等組件。

      例1-9演示了單獨的一個工具欄,該程序未添加事件處理,若要添加事件處理,實際上是對添加到工具欄內的組件的事件處理,如添加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"};

      //創建ImageIcon數組

      ImageIcon[] toolImage = new ImageIcon[images.length];

      tbButtons = new JButton[images.length];

      for(int i=0;i  //ImageIcon數組每個元素初始化

      toolImage[i] = new ImageIcon("bin"+images[i]);

      //創建帶有圖標的JButton

      tbButtons[i] = new JButton(toolImage[i]);

      //將帶有圖標的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 工具提示

      工具欄的一個缺點是用戶常常要猜測他上面的小圖標按鈕所代表的含義。為了解決該問題,java提供了工具提示。當鼠標在一個按鈕上停留一段時間后,工具提示就會被激活。工具提示文本顯示在一個有顏色的矩形內,當鼠標移開按鈕后,工具提示消失。

      工具提示并不是只在工具欄中可用,所有的Swing組件都支持工具提示,也就是說你可以在JButton、JList等都可以設置工具提示。工具提示是由ToolTipManager來維護的,我們可以通過這個類來設置從光標開始停留在組件上到顯示工具提示之間的時間間隔以及顯示工具提示信息的時長。

      修改例1-9,給工具欄添加工具提示,并利用ToolTipManager類來控制工具顯示時間。

      [例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"};

      //創建ImageIcon數組

      ImageIcon[] toolImage = new ImageIcon[images.length];

      tbButtons = new JButton[images.length];

      for(int i=0;i  //ImageIcon數組每個元素初始化

      toolImage[i] = new ImageIcon("bin"+images[i]);

      //創建帶有圖標的JButton

      tbButtons[i] = new JButton(toolImage[i]);

      //將帶有圖標的JButton添加到工具欄

      tb.add(tbButtons[i]);

      }

      //設置工具提示

      tbButtons[0].setToolTipText("刪除");

      tbButtons[1].setToolTipText("取消");

      //設置從光標開始停留在組件上到顯示工具提示之間的時間間隔為0.1秒

      ToolTipManager.sharedInstance().setInitialDelay(100);

      //設置工具提示信息顯示時長為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測試");

      }

      }

    【Java菜單和工具欄學習教程】相關文章:

    Java的特點學習教程01-22

    Java數組的基礎學習教程08-12

    Java數組的基本學習教程03-01

    photoshop工具欄教程02-01

    Java對話框學習教程05-31

    java常量和變量入門教程04-01

    Java入門教程:常量和變量02-10

    Dreamweaver創建跳轉菜單教程03-21

    java教程之Java編程基礎04-18

    <address id="ousso"></address>
    <form id="ousso"><track id="ousso"><big id="ousso"></big></track></form>
    1. 日日做夜狠狠爱欧美黑人