📄 main.java
字号:
jMenuBar1.add(helpMenu);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 536, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE)
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void openMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openMenuItemActionPerformed
// TODO add your handling code here:
openText();
}//GEN-LAST:event_openMenuItemActionPerformed
private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
// TODO add your handling code here:
exitapp();
}//GEN-LAST:event_exitMenuItemActionPerformed
private void stateMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stateMenuItemActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_stateMenuItemActionPerformed
private void saveMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveMenuItemActionPerformed
// TODO add your handling code here:
saveText();
}//GEN-LAST:event_saveMenuItemActionPerformed
private void newMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newMenuItemActionPerformed
// TODO add your handling code here:
newText();
}//GEN-LAST:event_newMenuItemActionPerformed
private void repealMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_repealMenuItemActionPerformed
// TODO add your handling code here:
repeal();
}//GEN-LAST:event_repealMenuItemActionPerformed
private void cutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cutMenuItemActionPerformed
// TODO add your handling code here:
cutText();
}//GEN-LAST:event_cutMenuItemActionPerformed
private void copyMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_copyMenuItemActionPerformed
// TODO add your handling code here:
copyText();
}//GEN-LAST:event_copyMenuItemActionPerformed
private void pasteMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pasteMenuItemActionPerformed
// TODO add your handling code here:
pasteText();
}//GEN-LAST:event_pasteMenuItemActionPerformed
private void selectallMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectallMenuItemActionPerformed
// TODO add your handling code here:
selectall();
}//GEN-LAST:event_selectallMenuItemActionPerformed
private void searchMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchMenuItemActionPerformed
// TODO add your handling code here:
search();
}//GEN-LAST:event_searchMenuItemActionPerformed
private void dateMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dateMenuItemActionPerformed
// TODO add your handling code here:
date();
}//GEN-LAST:event_dateMenuItemActionPerformed
private void autodownCheckBoxMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_autodownCheckBoxMenuItemActionPerformed
// TODO add your handling code here:
autodown();
}//GEN-LAST:event_autodownCheckBoxMenuItemActionPerformed
private void aboutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutMenuItemActionPerformed
// TODO add your handling code here:
JOptionPane.showConfirmDialog(this, "JNotepad 是由 Yee 开发的仿Windows记事本程序。\nQQ:924347108 (易)", "关于 JNotepad",
JOptionPane.DEFAULT_OPTION,
JOptionPane.INFORMATION_MESSAGE);
}//GEN-LAST:event_aboutMenuItemActionPerformed
private void helpMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpMenuItemActionPerformed
// TODO add your handling code here:
JOptionPane.showConfirmDialog(this, "JNotepad 是由 Yee 开发的仿Windows记事本程序。\n所以其功能及各项操作与Windows记事本程序基本一致。", "帮助",
JOptionPane.DEFAULT_OPTION,
JOptionPane.INFORMATION_MESSAGE);
}//GEN-LAST:event_helpMenuItemActionPerformed
private void fontMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fontMenuItemActionPerformed
// TODO add your handling code here:
setFont();
}//GEN-LAST:event_fontMenuItemActionPerformed
private void exchangeMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exchangeMenuItemActionPerformed
// TODO add your handling code here:
exchange();
}//GEN-LAST:event_exchangeMenuItemActionPerformed
private void delMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delMenuItemActionPerformed
// TODO add your handling code here:
del();
}//GEN-LAST:event_delMenuItemActionPerformed
private void searchnextMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchnextMenuItemActionPerformed
// TODO add your handling code here:
searchnext();
}//GEN-LAST:event_searchnextMenuItemActionPerformed
class UndoHander implements UndoableEditListener {
public void undoableEditHappened(UndoableEditEvent eundo) {
undo.addEdit(eundo.getEdit());
}
}
private void newText() {
int selection = JOptionPane.showConfirmDialog(this,
"是否保存当前文本文档?", "保存提示", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE);
if (selection == JOptionPane.OK_OPTION) {
saveText();
}
mainTextArea.setText("");
setTitle("新建文本文档.txt");
}
private void openText() {
int selection = JOptionPane.showConfirmDialog(this,
"是否保存当前文本文档?", "保存提示", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.WARNING_MESSAGE);
if (selection == JOptionPane.OK_OPTION) {
saveText();
}
try {
Frame f = new Frame();
FileDialog fd = new FileDialog(f, "打开文件", FileDialog.LOAD);
fd.setVisible(true);
String fpath = fd.getDirectory();
final String fname = fd.getFile();
BufferedReader br = new BufferedReader(new FileReader(fpath + fname));
mainTextArea.setText("");
setTitle(fname);
String s = br.readLine();
while (s != null) {
mainTextArea.append(s + "\n");
s = br.readLine();
}
br.close();
} catch (Exception ex) {
}
}
private void saveText() {
String fns = null;
Frame f = new Frame("保存");
FileDialog fd = new FileDialog(f, "保存文件", FileDialog.SAVE);
String savepath = fd.getDirectory();
String savename = fd.getFile();
fd.setFile("*.txt");
fd.setVisible(true);
try {
if (savename != null) {
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(savepath + savename)));
pw.write(mainTextArea.getText(), 0, mainTextArea.getText().length());
pw.flush();
}
} catch (Exception esave) {
}
}
private void repeal() {
try {
undo.undo();
//System.out.println(undo.canUndo());
} catch (Exception eundo) {
}
}
private void cutText() {
mainTextArea.cut();
}
private void copyText() {
mainTextArea.copy();
}
private void pasteText() {
mainTextArea.paste();
}
private void del() {
if (mainTextArea.getSelectedText() != null) {
mainTextArea.replaceSelection("");
} else {
int length = mainTextArea.getSelectionEnd();
mainTextArea.select(length - 1, length);
mainTextArea.replaceSelection("");
}
}
private void selectall() {
mainTextArea.selectAll();
}
public void search() {
try {
final JDialog jd = new JDialog(this, "查找", true);
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 0.5;
gbc.weighty = 0.5;
gbc.gridwidth = 1;
gbc.gridheight = 1;
jd.getContentPane().setLayout(gbl);
jd.setSize(320, 100);
jd.setResizable(false);
//jd.setDefaultLookAndFeelDecorated(true);
JLabel jlFind = new JLabel("查找内容:");
gbc.gridx = 0;
gbc.gridy = 0;
jd.getContentPane().add(jlFind);
JButton jbFind = new JButton("查找");
jbFind.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent efind) {
String strA = mainTextArea.getText();
String strB = jtf.getText();
if (a >= 0) {
a = strA.indexOf(strB, StartFindPos);
b = strB.length();
StartFindPos = a + b;
if (a == -1) {
JOptionPane.showMessageDialog(null, "没有您要查找的信息", "查找结果", 1);
a = 0;
StartFindPos = 0;
}
mainTextArea.select(a, StartFindPos);
}
}
});
JButton jbCancel = new JButton("取消");
jbCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ejb) {
jd.dispose();
}
});
gbc.gridx = 1;
gbc.gridy = 0;
jd.getContentPane().add(jtf, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
jd.getContentPane().add(jbFind, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
jd.getContentPane().add(jbCancel, gbc);
//jd.setResizable(false);
jd.setLocation(240, 200);
jd.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
jd.setVisible(true);
} catch (Exception efind) {
}
}
public void searchnext() {
String strA = mainTextArea.getText();
String strB = jtf.getText();
if (a >= 0) {
a = strA.indexOf(strB, StartFindPos);
b = strB.length();
StartFindPos = a + b;
if (a == -1) {
JOptionPane.showMessageDialog(null, "没有您要查找的信息", "查找结果", 1);
a = 0;
StartFindPos = 0;
}
mainTextArea.select(a, StartFindPos);
}
}
private void exchange() {
final JDialog jd = new JDialog(this, "替换", true);
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridwidth = 1;
gbc.gridheight = 1;
JLabel jlFind = new JLabel("查找:");
JLabel jp = new JLabel("替换内容:");
final JTextField jtf = new JTextField(15);
final JTextField jtf1 = new JTextField(15);
jd.getContentPane().setLayout(gbl);
jd.setSize(330, 150);
jd.setResizable(false);
final JButton jbReplaceAll = new JButton("替换");
final JButton jbCancel = new JButton("取消");
final JButton jbFind = new JButton("查找");
gbc.gridx = 0;
gbc.gridy = 0;
jd.getContentPane().add(jlFind, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
jd.getContentPane().add(jtf1, gbc);
gbc.gridx = 2;
gbc.gridy = 0;
jd.getContentPane().add(jbFind, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -