2024年1月23日发(作者:)

实验内容:设计与实现一个文本编辑器,要求具有方便的图形用户界面,包括标题栏、菜单栏,菜单栏有相关的菜单和菜单项,有快捷键,具有编辑、打开、保存、复制、粘贴等功能(详细功能要求通过运行提供的字节码文件来确定,主类是JNotePadUI)。完成有关类的设计,并调试运行,给出有代表性的程序输出结果(程序运行截图)。

程序运行截图如下;

图一

1

图二

图三

2

图四

图五

3

图六

4

图三

程序代码如下:

//

import .*;

import .*;

import ansfer.*;

import .*;

import .*;

import .*;

public class JNotePadUI extends JPanel {

// 变量定义

JTextArea jta = new JTextArea("", 24, 40);

JScrollPane jsp = new JScrollPane(jta);

// 菜单条

JMenuBar jmb = new JMenuBar();

JMenu file = new JMenu("文件(F)", true);

JMenu edit = new JMenu("编辑(E)", true);

JMenu help = new JMenu("帮助(H)", true);

// 工具条

JToolBar toolBar = new JToolBar();

5

// 菜单内容

JMenuItem jmi;

// 实例化剪切板

Clipboard clipbd = getToolkit().getSystemClipboard();

String text = "";

// 构造函数

public JNotePadUI() {

class newL implements ActionListener {

public void actionPerformed(ActionEvent e) {

ument(new PlainDocument());

}

}

// 打开功能

class openL implements ActionListener {

public void actionPerformed(ActionEvent e) {

JFileChooser fc = new JFileChooser();

int returnVal = alog(, "打开");

if (returnVal == E_OPTION) {

String file = ectedFile().getPath();

if (file == null) {

return;

}

// 读取文件

try {

Reader in = new FileReader(file);

char[] buff = new char[4096];

int nch;

while ((nch = (buff, 0, )) != -1) {

ument(new PlainDocument());

(new String(buff, 0, nch));

}

} catch (IOException io) {

n("IOException: " + sage());

}

} else {

return;

}

}

}

// 保存文件

class saveL implements ActionListener {

public void actionPerformed(ActionEvent e) {

JFileChooser fc = new JFileChooser();

6

int returnVal = veDialog();

if (returnVal == E_OPTION) {

String savefile = ectedFile().getPath();

if (savefile == null) {

return;

} else {

String docToSave = t();

if (docToSave != null) {

FileOutputStream fstrm = null;

BufferedOutputStream ostrm = null;

try {

fstrm = new FileOutputStream(savefile);

ostrm = new BufferedOutputStream(fstrm);

byte[] bytes = null;

try {

bytes = es();

} catch (Exception e1) {

tackTrace();

}

(bytes);

} catch (IOException io) {

n("IOException: "

+ sage());

} finally {

try {

();

();

();

} catch (IOException ioe) {

n("IOException: "

+ sage());

}

}

}

}

} else {

return;

}

}

}

// 退出

class exitL implements ActionListener {

public void actionPerformed(ActionEvent e) {

7

(0);

}

}

// 复制

class copyL implements ActionListener {

public void actionPerformed(ActionEvent e) {

String selection = ectedText();

StringSelection clipString = new StringSelection(selection);

tents(clipString, clipString);

}

}

// 剪切

class cutL implements ActionListener {

public void actionPerformed(ActionEvent e) {

String selection = ectedText();

StringSelection clipString = new StringSelection(selection);

tents(clipString, clipString);

eRange("", ectionStart(), jta

.getSelectionEnd());

}

}

// 粘贴

class pasteL implements ActionListener {

public void actionPerformed(ActionEvent e) {

Transferable clipData = tents();

try {

String clipString = (String) clipData

.getTransferData(Flavor);

eRange(clipString, ectionStart(), jta .getSelectionEnd());

} catch (Exception ex) {

}

}

}

// 删除

class deleteL implements ActionListener {

public void actionPerformed(ActionEvent e) {

eRange("", ectionStart(), jta

.getSelectionEnd());

}

}

//帮助

class help_h implements ActionListener{

public void actionPerformed(ActionEvent e){

8

ssageDialog(jta,"记事本支持拖入文本读取n" + "由于对编码不熟悉做得n不是很好 n ", "帮助主题", ATION_MESSAGE);

}

}

//关于

class help_a implements ActionListener{

public void actionPerformed(ActionEvent e){

ssageDialog(jta," 的记事本n" + "

mail:bahargul317@" + " JAVA图形界面练习n", "关于记事本",

ATION_MESSAGE);

}

}

// 事件监听

class jtaL implements ActionListener {

public void actionPerformed(ActionEvent e) {

}

}

// 快捷键设置

(jmi = new JMenuItem("新建N", 'N'));

elerator(Stroke(_N,

_MASK));

ionListener(new newL());

(jmi = new JMenuItem("打开O", 'O'));

elerator(Stroke(_O,

_MASK));

ionListener(new openL());

(jmi = new JMenuItem("保存S", 'S'));

elerator(Stroke(_S,

_MASK));

ionListener(new saveL());

arator();

(jmi = new JMenuItem("退出X", 'X'));

elerator(Stroke(_X,

_MASK));

ionListener(new exitL());

(jmi = new JMenuItem("复制C", 'C'));

elerator(Stroke(_C,

_MASK));

ionListener(new copyL());

(jmi = new JMenuItem("剪切T", 'T'));

elerator(Stroke(_T,

_MASK));

ionListener(new cutL());

(jmi = new JMenuItem("粘帖V",'V'));

9

elerator(Stroke(_V,

_MASK));

ionListener(new pasteL());

(jmi = new JMenuItem("删除D", 'D'));

elerator(Stroke(_D,

_MASK));

ionListener(new deleteL());

arator();

(jmi = new JMenuItem("帮助(H)", 'H'));

elerator(Stroke(_H,

_MASK));

ionListener(new help_h());

(jmi = new JMenuItem("关于(A)", 'A'));

elerator(Stroke(_A,

_MASK));

ionListener(new help_a());

arator();

setLayout(new BorderLayout());

monic('F');

monic('E');

monic('H');

(file);

(edit);

(help);

atable(true);

add(jmb, );

add(toolBar, );

add(jsp, );

et().setVisible(true);

etPosition(0);

}

// 关闭窗口

protected static final class appCloseL extends WindowAdapter {

public void windowClosing(WindowEvent e) {

(0);

}

}

// 主函数,程序入口

public static void main(String args[]) {

JFrame f = new JFrame();

JNotePadUI applet = new JNotePadUI();

10

le("写字板 巴哈尔古力。库来西");

kground(ray);

tentPane().add(applet, );

dowListener(new appCloseL());

e(800, 500);

ible(true);

();

}

}

11