Java记事本

适合刚入门Java基础不好的同学

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package cn.edu.seig.home;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class MainUI extends JFrame {

private static final long serialVersionUID = -8533346717300013642L;

private JTextArea jTextArea1;
private JScrollPane jsp; //滚动条
private static String name;
private JFileChooser jFC = new JFileChooser();

public static void main(String[] args) {
new MainUI().init();
}

// 初始化界面
public void init() {
//设置窗口大小
setSize(800, 600);
setLocationRelativeTo(null);
// 设置关闭按钮动作不执行任何操作
setDefaultCloseOperation(0);
name = "新建文本文档.txt";
// 设置标志图案
ImageIcon ii = new ImageIcon("file\\1.png");
setIconImage(ii.getImage());
// 设置文件名
setTitle(name + " - 记事本");
// 添加菜单栏
addMenu();
// 添加文本框
jTextArea1 = new JTextArea();
// 添加滚动条
jsp = new JScrollPane(jTextArea1);
jsp.setPreferredSize(new Dimension(780, 550));
add(jsp, BorderLayout.CENTER);
setVisible(true);// 设置窗口可见
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
if (showSaveDialog() != JOptionPane.CANCEL_OPTION)// 若不是取消按钮,关闭程序
setDefaultCloseOperation(3);
}
});
}

// 添加菜单栏
private void addMenu() {
// 添加菜单栏
JMenuBar jmb = new JMenuBar();
// 设置菜单栏位置在顶级窗口的顶部
setJMenuBar(jmb);
// 菜单栏菜单
JMenu jm_file = new JMenu("文件");
JMenu jm_edit = new JMenu("编辑");
jmb.add(jm_file);
jmb.add(jm_edit);
// 给文件菜单添加菜单项
JMenuItem item1 = new JMenuItem("打开");
JMenuItem item2 = new JMenuItem("保存");
//将item1、item2添加到jm_file
jm_file.add(item1);
jm_file.add(item2);

// "打开"菜单项添加动作监听器
item1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 是否保存对原文件修改
if (showSaveDialog() == JOptionPane.CANCEL_OPTION) {// 取消按钮,则返回
return;
}
// 弹出一个 "Open File" 文件选择器对话框
int select = jFC.showOpenDialog(MainUI.this);
// 选择打开文件,则读写文件
if (select == JFileChooser.APPROVE_OPTION) {
jTextArea1.setText(readFile());// 写入文本框
jTextArea1.setCaretPosition(0);// 定位光标至行首
}
}
});

// “保存”菜单项添加动作监听器
item2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (jFC.getSelectedFile() == null) {
createFile();//调用创建文件方法
} else {
showSaveDialog();//调用文件是否保存方法
}
}
});
}

// 读取文件
private String readFile() {
// 声明缓冲字符流变量
BufferedReader br = null;
StringBuilder sb = null;
try {
br = new BufferedReader(new FileReader(jFC.getSelectedFile()));
sb = new StringBuilder();// 创建缓冲字符串
String str;
int count = 0;//控制添加换行符,与原文保持一致
while ((str = br.readLine()) != null) {
if (count == 0)
sb.append(str);
if (count++ != 0)
sb.append("\n" + str);// 添加换行
}
} catch (FileNotFoundException e1) {
// 弹出“文件未找到”对话框,返回null
JOptionPane.showMessageDialog(null, "未找到该文件!");
return null;
} catch (IOException e1) {
// 弹出“文件读取异常”对话框,返回null
JOptionPane.showMessageDialog(null, "文件读取异常");
return null;
} finally {
// 关闭字符流
if (br != null)
try {
br.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
name = jFC.getSelectedFile().getName();
setTitle(name + " - 记事本");//设置文件名
return sb.toString();
}

// 保存对话框
private int showSaveDialog() {
int select = 0;
// 判断文本框是否为空
if (!jTextArea1.getText().equals("")) {
// 判断文件是否为空
if (jFC.getSelectedFile() == null) {
select = JOptionPane.showConfirmDialog(MainUI.this, "是否保存修改?");
if (select == JOptionPane.YES_OPTION) {
createFile();//文件不存在,则创建文件
}
} else {
if (!isSaved()) {// 文件未保存
select = JOptionPane.showConfirmDialog(MainUI.this, "是否保存修改?");
if (select == JOptionPane.YES_OPTION) {
saveFile(jFC.getSelectedFile());
}
}
}
}
return select;// 返回选项
}

// 创建新文件
private void createFile() {
// String name = null;
File file = null;
// 选择保存或取消
if (jFC.showSaveDialog(MainUI.this) == JFileChooser.APPROVE_OPTION) {
file = jFC.getSelectedFile();// 获取选中的文件
} else {
return;
}
name = jFC.getName(file);// 获取输入的文件名
if (file.exists()) { // 若选择已有文件----询问是否要覆盖
int i = JOptionPane.showConfirmDialog(null, "该文件已存在,是否覆盖原文件", "确认", JOptionPane.YES_NO_OPTION);
if (i == JOptionPane.YES_OPTION) {
saveFile(file);
} else {
jFC.showSaveDialog(MainUI.this);// 重新选择
}
} else {//文件不存在,则直接保存
saveFile(file);
}
}

// 判断文件是否保存
private Boolean isSaved() {
// 比较内容
if (jTextArea1.getText().equals(readFile())) {
return true;
}
return false;
}

// 保存文件,文件不存在则创建新文件
private void saveFile(File file) {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
bw.write(jTextArea1.getText());//写入文件
bw.flush();
} catch (FileNotFoundException e1) {
JOptionPane.showMessageDialog(MainUI.this, "文件保存出错" + e1.getMessage());
} catch (IOException e1) {
e1.printStackTrace();
} finally {//finally是表示最后一定会执行的语句
try {
if (bw != null)
bw.close();
} catch (IOException e1) {
}
}
}
}

ActionListener添加窗口联动

创建界面我是用windowBuilder的

创建界面

不懂怎么创建可以看文章末尾

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.awt.EventQueue;
import javax.swing.JFrame;

public class Window_text {

private JFrame frame;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Window_text window = new Window_text();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public Window_text() {
initialize();
}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}

触发事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Window_text {

private JFrame frame;
//新建面板
public JPanel panel = new JPanel();

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Window_text window = new Window_text();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public Window_text() {
initialize();
}


/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//以下全是新增代码
frame.getContentPane().setLayout(null);
panel.setBounds(0, 0, 103, 263);
frame.getContentPane().add(panel);
panel.setLayout(null);

JButton btnNewButton = new JButton("btn");
btnNewButton.setBounds(0, 95, 97, 23);
panel.add(btnNewButton);
// 绑定事件
btnNewButton.addActionListener(new B1());
}

/**
* 点击按钮触发的事件
* @author Lilbai518
*
*/
class B1 implements ActionListener{

public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根

JPanel right = new JPanel();
right.setBounds(104, 0, 332, 263);
frame.getContentPane().add(right);
right.setLayout(null);

JLabel right_label = new JLabel("成功啦");
right_label.setFont(new Font("楷体", Font.PLAIN, 28));
right_label.setBounds(91, 88, 193, 61);
right.add(right_label);
right_label.setLayout(null);
}
}
}

如何新建WindowBuilder

如何你还没安装WindowBuilder的话,先去安装再来打开