📄 notepad.java
字号:
package visual;
import java.awt.BorderLayout;
import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
public class NotePad extends JFrame {
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
try {
NotePad frame = new NotePad();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the frame
*/
private String file;
private String temp;
JPanel panel;
JToolBar toolBar;
JButton button;
JLabel label;
JButton button_1;
JMenuBar menuBar;
JMenu menu;
JMenuItem menuItem;
JMenuItem menuItem_1;
JMenuItem menuItem_2;
JScrollPane scrollPane;
JEditorPane editorPane;
JLabel label_1;
JButton button_2;
private StringBuffer reader=new StringBuffer();
public NotePad() {
super();
setBounds(100, 100, 500, 494);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel.setLayout(new BorderLayout());
getContentPane().add(panel, BorderLayout.NORTH);
toolBar = new JToolBar();
panel.add(toolBar);
button = new JButton();
toolBar.add(button);
button.setText(" 打开 ");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
buttonActionPerformed();
}
});
label = new JLabel();
label.setText(" ");
toolBar.add(label);
button_1 = new JButton();
toolBar.add(button_1);
button_1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
button_1Performed();
}
});
button_1.setText(" 保存 ");
label_1 = new JLabel();
label_1.setText(" ");
toolBar.add(label_1);
button_2 = new JButton();
button_2.setText(" 退出 ");
button_2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
toolBar.add(button_2);
menuBar = new JMenuBar();
setJMenuBar(menuBar);
menu = new JMenu();
menu.setText("文件");
menuBar.add(menu);
menuItem = new JMenuItem();
menuItem.setText("打开");
menuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
buttonActionPerformed();
}
});
menu.add(menuItem);
menu.addSeparator();
menuItem_1 = new JMenuItem();
menuItem_1.setText("保存");
menuItem_1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
button_1Performed();
}
});
menu.add(menuItem_1);
menu.addSeparator();
JMenuItem menuItem_2 = new JMenuItem();
menuItem_2.setText("退出");
menuItem_2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
menu.add(menuItem_2);
scrollPane = new JScrollPane();
getContentPane().add(scrollPane);
editorPane = new JEditorPane();
scrollPane.setViewportView(editorPane);
//
}
void buttonActionPerformed(){
FileDialog filedialog=new FileDialog(this,"附件");
filedialog.setVisible(true);
file=filedialog.getDirectory()+filedialog.getFile();
File f=new File(file);
try {
FileInputStream fis=new FileInputStream(f);
InputStreamReader isr=new InputStreamReader(fis);
BufferedReader br=new BufferedReader(isr);
while((temp=br.readLine())!=null){
reader.append(temp);
reader.append("\n");
}
//System.out.println(reader);
br.close();
editorPane.setText(reader.toString());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
}
void button_1Performed(){
FileDialog filedialog=new FileDialog(this,"附件");
filedialog.setVisible(true);
file=filedialog.getDirectory()+filedialog.getFile();
File f=new File(file);
try {
f.createNewFile();
FileOutputStream fos=new FileOutputStream(f);
OutputStreamWriter osw=new OutputStreamWriter(fos);
BufferedWriter bw=new BufferedWriter(osw);
temp=editorPane.getText();
bw.write(temp);
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -