记事本(纯界面)
Posted 南笙一梦
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了记事本(纯界面)相关的知识,希望对你有一定的参考价值。
代码内容:
package NoteBook;
import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.IOException;
import javax.swing.*;
import static javax.swing.JFrame.*;
public class Note extends JFrame
JMenuBar menubar;
JMenu file, editor, format, view, help;
JMenuItem item1, item2, item3, item4, item5, item6, item7; //文件部分
JMenuItem itemm1, itemm2, itemm3, itemm4, itemm5, itemm6,
itemm7, itemm8, itemm9, itemm10, itemm11; 编辑部分
JMenuItem ite1, ite2, ite3, ite4, ite5; //其他三部分
JTextArea area;
public Note()
public Note(String name, int x, int y, int w, int h)
init(name);
setLocation(x, y);
setSize(w, h);
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
void init(String s)
setTitle(s);
this.setIconImage(this.getToolkit().getImage(getClass().getResource("note.png")));
menubar = new JMenuBar();
file = new JMenu("文件(F)");
editor = new JMenu("编辑(E)");
format = new JMenu("格式(O)");
view = new JMenu("查看(V)");
help = new JMenu("帮助(H)");
文件菜单
item1 = new JMenuItem("新建(N)");
item2 = new JMenuItem("打开(O)...");
item3 = new JMenuItem("保存(S)");
item4 = new JMenuItem("另存为(A)...");
item5 = new JMenuItem("页面设置(U)...");
item6 = new JMenuItem("打印(P)...");
item7 = new JMenuItem("退出(X)");
file.add(item1);
file.add(item2);
file.add(item3);
file.add(item4);
file.addSeparator(); //分隔线
file.add(item5);
file.add(item6);
file.addSeparator(); //分隔线
file.add(item7);
item1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
InputEvent.CTRL_MASK));
item2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
InputEvent.CTRL_MASK));
item3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
InputEvent.CTRL_MASK));
item6.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,
InputEvent.CTRL_MASK));
menubar.add(file);
编辑菜单
itemm1 = new JMenuItem("撤销(U)");
itemm2 = new JMenuItem("剪切(T)...");
itemm3 = new JMenuItem("复制(C)");
itemm4 = new JMenuItem("粘贴(P)...");
itemm5 = new JMenuItem("删除(L)...");
itemm6 = new JMenuItem("查找(F)...");
itemm7 = new JMenuItem("查找下一个(N)");
itemm8 = new JMenuItem("替换(R)...");
itemm9 = new JMenuItem("转到(G)...");
itemm10 = new JMenuItem("全选(A)");
itemm11 = new JMenuItem("时间/日期(D)");
editor.add(itemm1);
editor.addSeparator(); //分隔线
editor.add(itemm2);
editor.add(itemm3);
editor.add(itemm4);
editor.add(itemm5);
editor.addSeparator(); //分隔线
editor.add(itemm6);
editor.add(itemm7);
editor.add(itemm8);
editor.add(itemm9);
editor.addSeparator(); //分隔线
editor.add(itemm10);
editor.add(itemm11);
itemm1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,
InputEvent.CTRL_MASK));
itemm2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
InputEvent.CTRL_MASK));
itemm3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
InputEvent.CTRL_MASK));
itemm4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,
InputEvent.CTRL_MASK));
itemm5.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0));
itemm6.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,
InputEvent.CTRL_MASK));
itemm7.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0));
itemm8.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,
InputEvent.CTRL_MASK));
itemm9.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G,
InputEvent.CTRL_MASK));
itemm10.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,
InputEvent.CTRL_MASK));
itemm11.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5,
0));
menubar.add(editor);
格式菜单
ite1 = new JMenuItem("自动换行(W)");
ite2 = new JMenuItem("字体(F)...");
format.add(ite1);
format.add(ite2);
menubar.add(format);
查看菜单
ite3 = new JMenuItem("状态栏(s)");
view.add(ite1);
menubar.add(view);
帮助菜单
ite4 = new JMenuItem("查看帮助(H)");
ite5 = new JMenuItem("关于记事本(A)");
help.add(ite4);
help.addSeparator(); //分隔线
help.add(ite5);
menubar.add(help);
setJMenuBar(menubar);
area = new JTextArea();
add(new JScrollPane(area));
package NoteBook;
/*
* 作者:赵志君
*/
public class Test
public static void main(String[] args)
Note note = new Note("记事本", 100, 100, 800, 500);
后期学会事件再写详细方法
以上是关于记事本(纯界面)的主要内容,如果未能解决你的问题,请参考以下文章