📄 notepadframe.java
字号:
package notepadshow;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.io.*;/** * Title: Java输入输出演示程序 * Description: Java输入输出演示程序,用于北京师范大学计算机系Java课程教学示范。 * Copyright: Copyright (c) 2001 * Company: 北京师范大学计算机系 * @author 孙一林 * @version 1.0 */public class NotepadFrame extends JFrame { JPanel contentPane; JToolBar jToolBar = new JToolBar(); JButton jButton1 = new JButton(); JButton jButton2 = new JButton(); JButton jButton3 = new JButton(); ImageIcon image1; ImageIcon image2; ImageIcon image3; JTextArea jTextArea1 = new JTextArea(); JFileChooser jFileChooser1 = new JFileChooser(); /**Construct the frame*/ public NotepadFrame() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } /**Component initialization*/ private void jbInit() throws Exception { image1 = new ImageIcon(notepadshow.NotepadFrame.class.getResource("openFile.gif")); image2 = new ImageIcon(notepadshow.NotepadFrame.class.getResource("closeFile.gif")); image3 = new ImageIcon(notepadshow.NotepadFrame.class.getResource("help.gif")); //setIconImage(Toolkit.getDefaultToolkit().createImage(NotepadFrame.class.getResource("[Your Icon]"))); contentPane = (JPanel) this.getContentPane(); contentPane.setLayout(null); this.setSize(new Dimension(400, 300)); this.setTitle("记事本"); jButton1.setIcon(image1); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton1_actionPerformed(e); } }); jButton1.setToolTipText("Open File"); jButton2.setIcon(image2); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton2_actionPerformed(e); } }); jButton2.setToolTipText("Close File"); jButton3.setIcon(image3); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { jButton3_actionPerformed(e); } }); jButton3.setToolTipText("Help"); jTextArea1.setBounds(new Rectangle(0, 31, 400, 269)); jToolBar.setBounds(new Rectangle(0, 0, 400, 31)); jFileChooser1.setSelectedFiles(null); jFileChooser1.setDialogTitle(""); jFileChooser1.setAutoscrolls(true); jFileChooser1.setToolTipText(""); jFileChooser1.setBounds(new Rectangle(357, 32, 6, 7)); jToolBar.add(jButton1); jToolBar.add(jButton2); jToolBar.add(jButton3); contentPane.add(jFileChooser1, null); contentPane.add(jTextArea1, null); contentPane.add(jToolBar, null); } /**File | Exit action performed*/ public void jMenuFileExit_actionPerformed(ActionEvent e) { System.exit(0); } /**Help | About action performed*/ public void jMenuHelpAbout_actionPerformed(ActionEvent e) { NotepadFrame_AboutBox dlg = new NotepadFrame_AboutBox(this); Dimension dlgSize = dlg.getPreferredSize(); Dimension frmSize = getSize(); Point loc = getLocation(); dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y); dlg.setModal(true); dlg.show(); } /**Overridden so we can exit when window is closed*/ protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } void jButton3_actionPerformed(ActionEvent e) { NotepadFrame_AboutBox dlg = new NotepadFrame_AboutBox(this); Dimension dlgSize = dlg.getPreferredSize(); Dimension frmSize = getSize(); Point loc = getLocation(); dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y); dlg.setModal(true); dlg.show(); } void jButton1_actionPerformed(ActionEvent e) { int val = jFileChooser1.showOpenDialog(this); String myFileName = jFileChooser1.getSelectedFile().getName(); try{ FileInputStream myfile = new FileInputStream(myFileName); byte buff[] = new byte[myfile.available()]; val = myfile.read(buff); myfile.close(); String s = new String(buff); jTextArea1.setText(s); } catch(IOException ioe){ } } void jButton2_actionPerformed(ActionEvent e) { int val = jFileChooser1.showSaveDialog(this); String myFileName = jFileChooser1.getSelectedFile().getName(); try{ File file = new File (myFileName); FileWriter outText = new FileWriter(file); String text = jTextArea1.getText(); outText.write(text); outText.close(); } catch(IOException ioe){ } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -