建立一個視窗程式(JFrame),標題欄起名為“瀏覽器”,有一個選單條,有“檔案”、“編輯”、“檢視”3個選單。“檔案”選單有兩個選單項,一項是“開啟”,一項是“儲存”,“開啟”項做成子選單,有兩個選單項“開啟x”和“開啟y”,“儲存”項就是普通的選單項。“編輯”選單就一個選單項“複製”。“檢視”有兩個選單項“停止”和“重新整理”。在選單及選單項上新增合適的快捷鍵。
package 視窗;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.InputEvent;
public class Text {
public static void main(String[] args) {
JFrame a=new JFrame("瀏覽器");//頂層容器
JMenuBar b=new JMenuBar();//選單條
JMenu c=new JMenu("檔案A");//選單
c.setMnemonic("A");
//c.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK));
JMenu d=new JMenu("編輯B");//選單
d.setMnemonic("B");
//d.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B,InputEvent.CTRL_MASK));
JMenu e=new JMenu("檢視C");//選單
c.setMnemonic("C");
//e.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
JMenuItem f=new JMenuItem("儲存");//選單項
f.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,InputEvent.CTRL_MASK));
JMenu g=new JMenu("開啟");//子選單
//g.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.CTRL_MASK));
JMenuItem h=new JMenuItem("開啟x");//選單項
h.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,InputEvent.CTRL_MASK));
JMenuItem i=new JMenuItem("開啟y");//選單項
i.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G,InputEvent.CTRL_MASK));
JMenuItem j=new JMenuItem("複製");//選單項
j.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,InputEvent.CTRL_MASK));
JMenuItem k=new JMenuItem("停止");//選單項
k.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I,InputEvent.CTRL_MASK));
JMenuItem l=new JMenuItem("重新整理");//選單項
l.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G,InputEvent.CTRL_MASK));
g.add(h);
g.add(i);
c.add(g);
c.add(f);
d.add(j);
e.add(k);
e.add(l);
b.add(c);
b.add(d);
b.add(e);
a.setJMenuBar(b); //將選單條加入視窗
a.setBounds(260,100,188,108);
a.setVisible(true);
a.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
建立一個視窗程式(JFrame),標題欄起名為“瀏覽器”,有一個選單條,有“檔案”、“編輯”、“檢視”3個選單。“檔案”選單有兩個選單項,一項是“開啟”,一項是“儲存”,“開啟”項做成子選單,有兩個選單項“開啟x”和“開啟y”,“儲存”項就是普通的選單項。“編輯”選單就一個選單項“複製”。“檢視”有兩個選單項“停止”和“重新整理”。在選單及選單項上新增合適的快捷鍵。
package 視窗;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.InputEvent;
public class Text {
public static void main(String[] args) {
JFrame a=new JFrame("瀏覽器");//頂層容器
JMenuBar b=new JMenuBar();//選單條
JMenu c=new JMenu("檔案A");//選單
c.setMnemonic("A");
//c.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK));
JMenu d=new JMenu("編輯B");//選單
d.setMnemonic("B");
//d.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B,InputEvent.CTRL_MASK));
JMenu e=new JMenu("檢視C");//選單
c.setMnemonic("C");
//e.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
JMenuItem f=new JMenuItem("儲存");//選單項
f.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,InputEvent.CTRL_MASK));
JMenu g=new JMenu("開啟");//子選單
//g.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.CTRL_MASK));
JMenuItem h=new JMenuItem("開啟x");//選單項
h.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,InputEvent.CTRL_MASK));
JMenuItem i=new JMenuItem("開啟y");//選單項
i.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G,InputEvent.CTRL_MASK));
JMenuItem j=new JMenuItem("複製");//選單項
j.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,InputEvent.CTRL_MASK));
JMenuItem k=new JMenuItem("停止");//選單項
k.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I,InputEvent.CTRL_MASK));
JMenuItem l=new JMenuItem("重新整理");//選單項
l.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G,InputEvent.CTRL_MASK));
g.add(h);
g.add(i);
c.add(g);
c.add(f);
d.add(j);
e.add(k);
e.add(l);
b.add(c);
b.add(d);
b.add(e);
a.setJMenuBar(b); //將選單條加入視窗
a.setBounds(260,100,188,108);
a.setVisible(true);
a.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}