Java-Swing组件模拟记事本界面练习

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.KeyEvent;

import javax.swing.*;

public class Demo extends JFrame{
public Demo() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(200, 200, 500, 400);
setTitle(“记事本”);
/**
* 添加记事本上边的菜单栏,并设置相应的快捷键
/
JMenuBar mnb=new JMenuBar();
setJMenuBar(mnb);
JMenu wenjian=new JMenu(“文件(F)”);
wenjian.setMnemonic(KeyEvent.VK_F);
JMenu bianji=new JMenu(“编辑(E)”);
bianji.setMnemonic(KeyEvent.VK_E);
JMenu geshi=new JMenu(“格式(O)”);
geshi.setMnemonic(KeyEvent.VK_O);
JMenu chakan=new JMenu(“查看(V)”);
chakan.setMnemonic(KeyEvent.VK_V);
JMenu bangzhu=new JMenu(“帮助(H)”);
bangzhu.setMnemonic(KeyEvent.VK_H);
mnb.add(wenjian);
mnb.add(bianji);
mnb.add(geshi);
mnb.add(chakan);
mnb.add(bangzhu);
/
*
* 添加文件下级菜单,并添加快捷键,添加部分菜单项的加速器
/
JMenuItem xinjian=new JMenuItem(“新建(N)”);
xinjian.setMnemonic(KeyEvent.VK_N);
xinjian.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,KeyEvent.CTRL_DOWN_MASK));
JMenuItem xinchaungkou=new JMenuItem(“新窗口(W)”);
xinchaungkou.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK));
xinchaungkou.setMnemonic(KeyEvent.VK_W);
JMenuItem dakai=new JMenuItem(“打开(O)…”);
dakai.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,KeyEvent.CTRL_DOWN_MASK));
dakai.setMnemonic(KeyEvent.VK_O);
JMenuItem baocun=new JMenuItem(“保存(S)”);
baocun.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,KeyEvent.CTRL_DOWN_MASK));
baocun.setMnemonic(KeyEvent.VK_S);
JMenuItem lingcunwei=new JMenuItem(“另存为(A)…”);
lingcunwei.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK));
lingcunwei.setMnemonic(KeyEvent.VK_A);
JMenuItem yemianshezhi=new JMenuItem(“页面设置(U)…”);
yemianshezhi.setMnemonic(KeyEvent.VK_U);
JMenuItem dayin=new JMenuItem(“打印§…”);
dayin.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,KeyEvent.CTRL_DOWN_MASK));
dayin.setMnemonic(KeyEvent.VK_P);
JMenuItem tuichu=new JMenuItem(“退出(X)”);
tuichu.setMnemonic(KeyEvent.VK_X);
wenjian.add(xinjian);
wenjian.add(xinchaungkou);
wenjian.add(dakai);
wenjian.add(baocun);
wenjian.add(lingcunwei);
wenjian.addSeparator();//添加相应的分割线
wenjian.add(yemianshezhi);
wenjian.add(dayin);
wenjian.addSeparator();//添加相应的分割线
wenjian.add(tuichu);
/
*
* 添加编辑的菜单项、快捷键、和相应的加速器
/
JMenuItem chexiao=new JMenuItem(“撤销(U)”);
chexiao.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,KeyEvent.CTRL_DOWN_MASK));
chexiao.setMnemonic(KeyEvent.VK_U);
JMenuItem jianqie=new JMenuItem(“剪切(T)”);
jianqie.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,KeyEvent.CTRL_DOWN_MASK));
jianqie.setMnemonic(KeyEvent.VK_T);
JMenuItem fuzhi=new JMenuItem(“复制©”);
fuzhi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,KeyEvent.CTRL_DOWN_MASK));
fuzhi.setMnemonic(KeyEvent.VK_C);
JMenuItem zhantie=new JMenuItem(“粘贴§”);
zhantie.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,KeyEvent.CTRL_DOWN_MASK));
zhantie.setMnemonic(KeyEvent.VK_P);
JMenuItem shanchu=new JMenuItem(“删除(L)”);
shanchu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0));
shanchu.setMnemonic(KeyEvent.VK_L);
JMenuItem Bingsous=new JMenuItem(“使用Bing搜索…”);
Bingsous.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,KeyEvent.CTRL_DOWN_MASK));
JMenuItem chazhao=new JMenuItem(“查找(F)…”);
chazhao.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,KeyEvent.CTRL_DOWN_MASK));
chazhao.setMnemonic(KeyEvent.VK_F);
JMenuItem chazhaos=new JMenuItem(“查找上一个(N)”);
chazhaos.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3,0));
chazhaos.setMnemonic(KeyEvent.VK_N);
JMenuItem chazhaox=new JMenuItem(“查找下一个(V)”);
chazhaox.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3,KeyEvent.SHIFT_DOWN_MASK));
chazhaox.setMnemonic(KeyEvent.VK_V);
JMenuItem tihuan=new JMenuItem(“替换®…”);
tihuan.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,KeyEvent.CTRL_DOWN_MASK));
tihuan.setMnemonic(KeyEvent.VK_R);
JMenuItem zhuandao=new JMenuItem(“转到(G)…”);
zhuandao.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G,KeyEvent.CTRL_DOWN_MASK));
zhuandao.setMnemonic(KeyEvent.VK_G);
JMenuItem qunaxuan=new JMenuItem(“全选(A)”);
qunaxuan.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,KeyEvent.CTRL_DOWN_MASK));
qunaxuan.setMnemonic(KeyEvent.VK_A);
JMenuItem shijian_riqi=new JMenuItem(“时间/日期(D)”);
shijian_riqi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5,0));
shijian_riqi.setMnemonic(KeyEvent.VK_D);
bianji.add(chexiao);
bianji.addSeparator();//添加相应的分割线
bianji.add(jianqie);
bianji.add(fuzhi);
bianji.add(zhantie);
bianji.add(shanchu);
bianji.addSeparator();//添加相应的分割线
bianji.add(Bingsous);
bianji.add(chazhao);
bianji.add(chazhaos);
bianji.add(chazhaox);
bianji.add(tihuan);
bianji.add(zhuandao);
bianji.addSeparator();//添加相应的分割线
bianji.add(qunaxuan);
bianji.add(shijian_riqi);
/
*
* 添加格式相关菜单项、快捷键
/
JMenuItem zidonghuanhang=new JMenuItem(“自动换行(W)”);
zidonghuanhang.setMnemonic(KeyEvent.VK_W);
JMenuItem ziti=new JMenuItem(“字体(F)…”);
ziti.setMnemonic(KeyEvent.VK_F);
geshi.add(zidonghuanhang);
geshi.add(ziti);
/
*
* 添加查看相关的菜单项、快捷键、加速器
/
JMenu suofang=new JMenu(“缩放(Z)”);
suofang.setMnemonic(KeyEvent.VK_Z);
JMenuItem fangda=new JMenuItem(“放大(I)”);
fangda.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_PLUS,KeyEvent.CTRL_DOWN_MASK));
fangda.setMnemonic(KeyEvent.VK_I);
JMenuItem suoxiao=new JMenuItem(“缩小(O)”);
suoxiao.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_MINUS,KeyEvent.CTRL_DOWN_MASK));
suoxiao.setMnemonic(KeyEvent.VK_O);
JMenuItem huifumoren=new JMenuItem(“恢复默认缩放”);
huifumoren.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_0,KeyEvent.CTRL_DOWN_MASK));
suofang.add(fangda);
suofang.add(suoxiao);
suofang.add(huifumoren);
JMenuItem zhuangtailan=new JMenuItem(“状态栏(S)”);
zhuangtailan.setMnemonic(KeyEvent.VK_S);
chakan.add(suofang);
chakan.add(zhuangtailan);
/
*
* 添加帮助的菜单项、快捷键
*/
JMenuItem chakanbangzhu=new JMenuItem(“查看帮助(H)”);
chakanbangzhu.setMnemonic(KeyEvent.VK_H);
JMenuItem fasongfankui=new JMenuItem(“发送反馈(F)”);
fasongfankui.setMnemonic(KeyEvent.VK_F);
JMenuItem guanyujishiben=new JMenuItem(“关于记事本(A)”);
guanyujishiben.setMnemonic(KeyEvent.VK_A);
bangzhu.add(chakanbangzhu);
bangzhu.add(fasongfankui);
bangzhu.add(guanyujishiben);

	JTextArea text=new JTextArea();
	JScrollPane scroll=new JScrollPane(text);
	Container c=getContentPane();
	c.add(scroll,BorderLayout.CENTER);
	setVisible(true);
	
}
public static void main(String[] args) {
	new Demo();
}

}
//界面的对应功能还没有学会-_-

你可能感兴趣的:(Java-Swing组件模拟记事本界面练习)