怎么用JAVA在一個(gè)程序里面創(chuàng)建多個(gè)圖形界面窗口?
創(chuàng)建一個(gè)窗口程序(JFrame),標(biāo)題欄起名為“瀏覽器”,有一個(gè)菜單條,有“文件”、“編輯”、“查看”3個(gè)菜單。“文件”菜單有兩個(gè)菜單項(xiàng),一項(xiàng)是“打開”,一項(xiàng)是“保存”,“打開”項(xiàng)做成子菜單,有兩個(gè)菜單項(xiàng)“打開x”和“打開y”,“保存”項(xiàng)就是普通的菜單項(xiàng)。“編輯”菜單就一個(gè)菜單項(xiàng)“復(fù)制”。“查看”有兩個(gè)菜單項(xiàng)“停止”和“刷新”。在菜單及菜單項(xiàng)上添加合適的快捷鍵。
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("保存");//菜單項(xiàng)
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");//菜單項(xiàng)
h.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,InputEvent.CTRL_MASK));
JMenuItem i=new JMenuItem("打開y");//菜單項(xiàng)
i.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G,InputEvent.CTRL_MASK));
JMenuItem j=new JMenuItem("復(fù)制");//菜單項(xiàng)
j.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,InputEvent.CTRL_MASK));
JMenuItem k=new JMenuItem("停止");//菜單項(xiàng)
k.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I,InputEvent.CTRL_MASK));
JMenuItem l=new JMenuItem("刷新");//菜單項(xiàng)
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);
}
}