⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aiai58.java

📁 一个java记事本无错源代码 一个java记事本无错源代码
💻 JAVA
字号:
import java.awt.*; //用户界面及绘图
import java.awt.event.*; //
import java.awt.print.*; 
import java.awt.datatransfer.*; 
import java.io.*; //输入输出
import javax.swing.*; //可视化组件,在所有平台上效果一样
import javax.swing.event.*; 
import javax.swing.text.*; 

public class aiai58 extends JPanel 
{ 
JTextArea jta = new JTextArea("", 20, 60); 

JScrollPane jsp = new JScrollPane(jta); 

JMenuBar jmb = new JMenuBar(); 
JMenu file = new JMenu("文件"); 
JMenu edit = new JMenu("编辑"); 
JMenu help = new JMenu("帮助"); 
JMenu search = new JMenu("查找"); 

JToolBar toolBar = new JToolBar(); 

JMenuItem jmi; 

Clipboard clipbd = getToolkit().getSystemClipboard(); 

PrinterJob prtMe = PrinterJob.getPrinterJob(); 

String text = ""; 

public aiai58() 
{ 
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(aiai58.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(aiai58.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 pageSetupL implements ActionListener 
{ 
public void actionPerformed(ActionEvent e) 
{ 
prtMe.printDialog(); 
} 
} 

class printL implements ActionListener 
{ 
public void actionPerformed(ActionEvent e) 
{ 
try 
{ 
prtMe.print(); 
} 
catch(Exception ew) 
{ 
} 
} 
} 

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(aiai58.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) 
{ 
String selection = jta.getSelectedText(); 
jta.replaceRange("", jta.getSelectionStart(), 
jta.getSelectionEnd()); 
} 
} 

class selectAllL implements ActionListener 
{ 
public void actionPerformed(ActionEvent e) 
{ 
jta.selectAll(); 
} 
} 

class findL implements ActionListener 
{ 
public void actionPerformed(ActionEvent e) 
{ 
String find = ""; 
find = JOptionPane.showInputDialog( 
"输入要查找的字符: "); 
} 
} 

class findNextL implements ActionListener 
{ 
public void actionPerformed(ActionEvent e) 
{ 
} 
} 

class aboutL implements ActionListener 
{ 
public void actionPerformed(ActionEvent e) 
{ 
JOptionPane.showMessageDialog(null, 
"本程序作者:李德寅 学号:042010214"); 
} 
} 

class jtaL implements ActionListener 
{ 
public void actionPerformed(ActionEvent e) 
{ 
} 
} 

file.add(jmi = new JMenuItem("新建", KeyEvent.VK_N)); 
jmi.addActionListener(new newL()); 
file.add(jmi = new JMenuItem("打开", KeyEvent.VK_O)); 
jmi.addActionListener(new openL()); 
file.add(jmi = new JMenuItem("保存", KeyEvent.VK_S)); 
jmi.addActionListener(new saveL()); 
file.addSeparator(); 
file.add(jmi = new JMenuItem("页面设置", KeyEvent.VK_G)); 
jmi.addActionListener(new pageSetupL()); 
file.add(jmi = new JMenuItem("打印", KeyEvent.VK_P)); 
jmi.addActionListener(new printL()); 
file.addSeparator(); 
file.add(jmi = new JMenuItem("退出", KeyEvent.VK_X)); 
jmi.addActionListener(new exitL()); 

edit.add(jmi = new JMenuItem("复制", KeyEvent.VK_C)); 
jmi.addActionListener(new copyL()); 
edit.add(jmi = new JMenuItem("剪切", KeyEvent.VK_T)); 
jmi.addActionListener(new cutL()); 
edit.add(jmi = new JMenuItem("粘贴", KeyEvent.VK_P)); 
jmi.addActionListener(new pasteL()); 
edit.add(jmi = new JMenuItem("删除", KeyEvent.VK_D)); 
jmi.addActionListener(new deleteL()); 
edit.addSeparator(); 
edit.add(jmi = new JMenuItem("全选", KeyEvent.VK_A)); 
jmi.addActionListener(new selectAllL()); 

search.add(jmi = new JMenuItem("查找", KeyEvent.VK_F)); 
jmi.addActionListener(new findL()); 
search.add(jmi = new JMenuItem("查找下一个", KeyEvent.VK_N)); 
jmi.addActionListener(new findNextL()); 

help.add(jmi = new JMenuItem("关于", KeyEvent.VK_A)); 
jmi.addActionListener(new aboutL()); 

setLayout(new BorderLayout()); 
file.setMnemonic(KeyEvent.VK_F); 
jmb.add(file); 
edit.setMnemonic(KeyEvent.VK_E); 
jmb.add(edit); 
search.setMnemonic(KeyEvent.VK_S); 
jmb.add(search); 
jmb.add(Box.createHorizontalGlue()); 
help.setMnemonic(KeyEvent.VK_H); 
jmb.add(help); 

toolBar.setFloatable(true); 
addButtons(toolBar); 

add(jmb, BorderLayout.NORTH); 
add(toolBar, BorderLayout.CENTER); 
add(jsp, BorderLayout.SOUTH); 

jta.getCaret().setVisible(true); 
jta.setCaretPosition(0); 
} 

public static void main(String args[]) 
{ 
JFrame f = new JFrame(); 
aiai58 applet = new aiai58(); 
f.setTitle("『记事本程序』 .::制作:李德寅 学号:042010214::."); 
f.setBackground(Color.black); 
f.getContentPane().add(applet, BorderLayout.CENTER); 
f.addWindowListener(new appCloseL()); 
f.setSize(800, 500); 
f.setVisible(true); 
f.pack(); 
} 

protected void addButtons(JToolBar toolBar) 
{ 
JButton button = new JButton(new ImageIcon("images/new.gif")); 
button.setToolTipText("新建一个文档"); 
button.addActionListener(new ActionListener() 
{ 
public void actionPerformed(ActionEvent e) 
{ 
jta.setDocument(new PlainDocument()); 
} 
}); 
toolBar.add(button); 

JButton button1 = new JButton(new ImageIcon("images/open.gif")); 
button1.setToolTipText("打开文档"); 
button1.addActionListener(new ActionListener() 
{ 
public void actionPerformed(ActionEvent e) 
{ 
JFileChooser fc = new JFileChooser(); 
int returnVal = fc.showDialog(aiai58.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; 
} 
} 
}); 
toolBar.add(button1); 

JButton button2 = new JButton(new ImageIcon("images/save.gif")); 
button2.setToolTipText("Save the document"); 
button2.addActionListener(new ActionListener() 
{ 
public void actionPerformed(ActionEvent e) 
{ 
JFileChooser fc = new JFileChooser(); 
int returnVal = fc.showSaveDialog(aiai58.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; 
} 
} 
}); 
toolBar.add(button2); 

JButton button3 = new JButton(new ImageIcon("images/copy.gif")); 
button3.setToolTipText("复制选中的文本"); 
button3.addActionListener(new ActionListener() 
{ 
public void actionPerformed(ActionEvent e) 
{ 
String selection = jta.getSelectedText(); 
StringSelection clipString = new StringSelection(selection); 
clipbd.setContents(clipString, clipString); 
} 
}); 
toolBar.add(button3); 

JButton button4 = new JButton(new ImageIcon("images/cut.gif")); 
button4.setToolTipText("剪切选中的文本"); 
button4.addActionListener(new 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()); 
} 
}); 
toolBar.add(button4); 

JButton button5 = new JButton(new ImageIcon("images/paste.gif")); 
button5.setToolTipText("Paste clipboard"); 
button5.addActionListener(new ActionListener() 
{ 
public void actionPerformed(ActionEvent e) 
{ 
Transferable clipData = clipbd.getContents(aiai58.this); 
try 
{ 
String clipString = 
(String)clipData.getTransferData( 
DataFlavor.stringFlavor); 
jta.replaceRange(clipString, 
jta.getSelectionStart(), jta.getSelectionEnd()); 
} 
catch(Exception ex) 
{ 
} 
} 
}); 
toolBar.add(button5); 

JButton button6 = new JButton(new ImageIcon("images/about.gif")); 
button6.setToolTipText("关于记事本"); 
button6.addActionListener(new ActionListener() 
{ 
public void actionPerformed(ActionEvent e) 
{ 
JOptionPane.showMessageDialog(null, 
"作者:李德寅 学号:042010214"); 
} 
}); 
toolBar.add(button6); 
} 

protected static final class appCloseL extends WindowAdapter 
{ 
public void windowClosing(WindowEvent e) 
{ 
System.exit(0); 
} 
} 
} ///:~ 
 
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -