问题描述:请使用Java编程语言实现图形化的写字板,能够完成写字寄存和擦除的功能。
程序源码:
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;
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();
// 菜单内容
JMenuItem jmi;
// 实例化剪切板
Clipboard clipbd = getToolkit().getSystemClipboard();
String text = "";
// 构造函数
public JNotePadUI()
{
class newL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
jta.setDocument(new PlainDocument());
}
}
// 打开功能
class openL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JFileChooser fc = new JFileChooser();
int returnVal = fc.showDialog(JNotePadUI.this, "打开");
if (returnVal == JFileChooser.APPROVE_OPTION)
{
String file = fc.getSelectedFile().getPath();
if (file == null)
{
return;
}
// 读取文件
try
{
Reader in = new FileReader(file);
char[] buff = new char[4096];
int nch;
while ((nch = in.read(buff, 0, buff.length)) != -1)
{
jta.setDocument(new PlainDocument());
jta.append(new String(buff, 0, nch));
}
}
catch (IOException io)
{
System.err.println("IOException: " + io.getMessage());
}
}
else
{
return;
}
}
}
// 保存文件
class saveL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JFileChooser fc = new JFileChooser();
int returnVal = fc.showSaveDialog(JNotePadUI.this);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
String savefile = fc.getSelectedFile().getPath();
if (savefile == null)
{
return;
}
else
{
String docToSave = jta.getText();
if (docToSave != null)
{
FileOutputStream fstrm = null;
BufferedOutputStream ostrm = null;
try
{
fstrm = new FileOutputStream(savefile);
ostrm = new BufferedOutputStream(fstrm);
byte[] bytes = null;
try
{
bytes = docToSave.getBytes();
}
catch (Exception e1)
{
e1.printStackTrace();
}
ostrm.write(bytes);
}
catch (IOException io)
{
System.err.println("IOException: "+ io.getMessage());
}
finally
{
try
{
ostrm.flush();
fstrm.close();
ostrm.close();
}
catch (IOException ioe)
{
System.err.println("IOException: "+ ioe.getMessage());
}
}
}
}
}
else
{
return;
}
}
}
// 退出
class exitL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
// 复制
class copyL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String selection = jta.getSelectedText();
StringSelection clipString = new StringSelection(selection);
clipbd.setContents(clipString, clipString);
}
}
// 剪切
class cutL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String selection = jta.getSelectedText();
StringSelection clipString = new StringSelection(selection);
clipbd.setContents(clipString, clipString);
jta.replaceRange("", jta.getSelectionStart(), jta.getSelectionEnd());
}
}
// 粘贴
class pasteL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Transferable clipData = clipbd.getContents(JNotePadUI.this);
try
{
String clipString = (String) clipData.getTransferData(DataFlavor.stringFlavor);
jta.replaceRange(clipString, jta.getSelectionStart(), jta.getSelectionEnd());
}
catch (Exception ex)
{
}
}
}
// 删除
class deleteL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
jta.replaceRange("", jta.getSelectionStart(), jta.getSelectionEnd());
}
}
//帮助
class help_h implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(jta,"写字板支持拖入文本读取\n" + "由于对编码不熟悉做得\n不是很好 \n ", "帮助主题", JOptionPane.INFORMATION_MESSAGE);
}
}
//关于
class help_a implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(jta,"有不懂的地方联系\n" + " " + " JAVA图形界面练习\n", "关于记事本", JOptionPane.INFORMATION_MESSAGE);
}
}
// 事件监听
class jtaL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
// 快捷键设置
file.add(jmi = new JMenuItem("新建N", 'N'));
jmi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK));
jmi.addActionListener(new newL());
file.add(jmi = new JMenuItem("打开O", 'O'));
jmi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK));
jmi.addActionListener(new openL());
file.add(jmi = new JMenuItem("保存S", 'S'));
jmi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
jmi.addActionListener(new saveL());
file.addSeparator();
file.add(jmi = new JMenuItem("退出E", 'E'));
jmi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, InputEvent.CTRL_MASK));
jmi.addActionListener(new exitL());
edit.add(jmi = new JMenuItem("复制C", 'C'));
jmi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK));
jmi.addActionListener(new copyL());
edit.add(jmi = new JMenuItem("剪切X", 'X'));
jmi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK));
jmi.addActionListener(new cutL());
edit.add(jmi = new JMenuItem("粘帖V",'V'));
jmi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK));
jmi.addActionListener(new pasteL());
edit.add(jmi = new JMenuItem("删除D", 'D'));
jmi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, InputEvent.CTRL_MASK));
jmi.addActionListener(new deleteL());
edit.addSeparator();
help.add(jmi = new JMenuItem("帮助(H)", 'H'));
jmi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, InputEvent.CTRL_MASK));
jmi.addActionListener(new help_h());
help.add(jmi = new JMenuItem("关于(A)", 'A'));
jmi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK));
jmi.addActionListener(new help_a());
help.addSeparator();
setLayout(new BorderLayout());
file.setMnemonic('F');
edit.setMnemonic('E');
help.setMnemonic('H');
jmb.add(file);
jmb.add(edit);
jmb.add(help);
toolBar.setFloatable(true);
add(jmb, BorderLayout.NORTH);
add(toolBar, BorderLayout.CENTER);
add(jsp, BorderLayout.SOUTH);
jta.getCaret().setVisible(true);
jta.setCaretPosition(0);
}
// 关闭窗口
protected static final class appCloseL extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
// 主函数,程序入口
public static void main(String args[])
{
JFrame f = new JFrame();
JNotePadUI applet = new JNotePadUI();
f.setTitle("写字板");
f.setBackground(Color.lightGray);
f.getContentPane().add(applet, BorderLayout.CENTER);
f.addWindowListener(new appCloseL());
f.setSize(800, 500);
f.setLocation(500,170);
f.setVisible(true);
f.pack();
f.setResizable(false);
}
}