1:对于页面搭建以及菜单搭建的准备
做这些操作前需要导入javax.swing.*包。该包中包含的JFrame类其实就是为我们搭建页面所在的窗体,也可以理解为结构,而JMenuBar类是为了菜单的搭建
2:JFrame的运用
package lianxi2;
import javax.swing.*;
public class test {
public static void main(String[] args) {
JFrame jf=new JFrame();
//设置界面大小,单位是像素
jf.setSize(680,600);
//将页面锁定,没有这句话,鼠标点击页面外的,该页面就会自动置后
jf.setAlwaysOnTop(true);
//居中
jf.setLocationRelativeTo(null);
//关闭模式
//0表示,无法关闭,点击多少次,也无法关闭,点击虚拟机的终止程序,才关闭
//1表示默认,即关闭页面不会终止程序
//2表示多个页面时,只有关闭最后一个界面时,虚拟机才会关掉
//3表示点击关闭,虚拟机也终止程序,同时多个页面,其中一个关闭(设置了该关闭模式的),则所有页面都关闭
jf.setDefaultCloseOperation(2);
//可视化,没有这段代码,界面无法展现
jf.setVisible(true);
JFrame k=new JFrame();
k.setSize(400,200);
k.setTitle("kkk");
k.setDefaultCloseOperation(3);
k.setVisible(true);
}
}
3:JMenuBar的运用
package lianxi2;
import javax.swing.*;
public class test {
public static void main(String[] args) {
JFrame jf=new JFrame();
jf.setTitle("测试");
jf.setSize(600,500);
jf.setAlwaysOnTop(true);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(0);
//创建菜单体
JMenuBar jm=new JMenuBar();
//创建菜单体中的选项
JMenu gong=new JMenu("功能");
JMenu abo=new JMenu("关于");
//创建选项中的条目
JMenuItem rep=new JMenuItem("重新游戏");
JMenuItem red=new JMenuItem("重新登录");
JMenuItem go=new JMenuItem("公众号");
//后面因为他们都是独立的,根据逻辑性,一个一个开始添加
gong.add(rep);
gong.add(red);
abo.add(go);
jm.add(gong);
jm.add(abo);
jf.setJMenuBar(jm);
jf.setVisible(true);
}
}