標籤:
package com.niit.javagui;import java.awt.Color;import java.awt.MenuBar;import java.awt.event.InputEvent;import java.awt.event.KeyEvent;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JPanel;import javax.swing.JTabbedPane;import javax.swing.KeyStroke;/** * @author: Annie * @date:2016年5月30日 * @description: */public class JTabbedPaneDemo extends JFrame{ public static void main(String[] args) { new JTabbedPaneDemo(); } JTabbedPane tabbedPane; JPanel jp1,jp2,jp3; JMenuBar menuBar; JMenu filemenu,editMenu,formatMenu,helpmenu,checkMenu; JMenuItem itemNew,itemOpen,itemSave,itemSaveAs,itemSet,itemPrint,itemExit, itemAutomatic,itemFont,itemaStatusBar,itemaCheckHelp,itemAbout; public JTabbedPaneDemo(){ setVisible(true); setSize(400,400); setTitle("選項卡面板例子"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); instantiationObject(); keyWord(); addFunction(); jp1.setBackground(Color.CYAN); jp2.setBackground(Color.RED); jp3.setBackground(Color.BLUE); } /** * 執行個體化對象方法 * */ public void instantiationObject(){ jp1 = new JPanel(); jp2 = new JPanel(); jp3 = new JPanel(); tabbedPane = new JTabbedPane(JTabbedPane.BOTTOM); menuBar = new JMenuBar(); filemenu = new JMenu("檔案(F)"); editMenu = new JMenu("編輯(E)"); formatMenu = new JMenu("格式(o)"); checkMenu = new JMenu("查看(V)"); helpmenu = new JMenu("協助(H)"); itemNew = new JMenuItem("建立檔案"); itemOpen = new JMenuItem("開啟"); itemSave = new JMenuItem("儲存"); itemSaveAs = new JMenuItem("另存新檔"); itemSet = new JMenuItem("版面設定"); itemPrint = new JMenuItem("列印"); itemExit = new JMenuItem("退出"); itemAutomatic = new JMenuItem("自動轉行"); itemFont = new JMenuItem("字型"); itemaStatusBar = new JMenuItem("狀態列"); itemaCheckHelp = new JMenuItem("查看協助"); itemAbout = new JMenuItem("關於記事本"); } /** * 添加組件方法 * */ public void addFunction(){ setJMenuBar(menuBar); menuBar.add(filemenu); menuBar.add(editMenu); menuBar.add(formatMenu); menuBar.add(checkMenu); menuBar.add(helpmenu); filemenu.add(itemNew); filemenu.add(itemOpen); filemenu.add(itemSave); filemenu.add(itemSaveAs); filemenu.addSeparator(); filemenu.add(itemSet); filemenu.add(itemPrint); filemenu.add(itemExit); formatMenu.add(itemAutomatic); formatMenu.add(itemFont); checkMenu.add(itemaStatusBar); helpmenu.add(itemaCheckHelp); helpmenu.add(itemAbout); tabbedPane.add("連絡人", jp1); tabbedPane.add("發現", jp2); tabbedPane.add("我", jp3); add(tabbedPane); } /** * 添加關鍵字的方法 * */ public void keyWord(){ itemNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK)); itemOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK)); itemSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK)); itemSaveAs.setAccelerator(KeyStroke.getKeyStroke(‘A‘)); itemSet.setAccelerator(KeyStroke.getKeyStroke(‘U‘)); itemPrint.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK)); itemExit.setAccelerator(KeyStroke.getKeyStroke(‘X‘)); itemAutomatic.setAccelerator(KeyStroke.getKeyStroke(‘W‘)); itemFont.setAccelerator(KeyStroke.getKeyStroke(‘F‘)); itemaStatusBar.setAccelerator(KeyStroke.getKeyStroke(‘S‘)); itemaCheckHelp.setAccelerator(KeyStroke.getKeyStroke(‘H‘)); itemAbout.setAccelerator(KeyStroke.getKeyStroke(‘A‘)); }}
:
GUI記事本+切換面板