java记事本小白
源码:
主函数
package java实践;
import java.awt.Color;
import java.awt.Desktop;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.text.BadLocationException;
import javax.swing.undo.UndoManager;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import java.awt.Toolkit;
import javax.swing.JFileChooser;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
import java.awt.Dialog.ModalExclusionType;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JCheckBoxMenuItem;
public class Notebook extends JFrame{
/**
*
*/
private static final long serialVersionUID = 1L;
private static int linenum =1;
private static int columnnum=1;
private JPanel contentPane;
private JTextArea textArea_1;
private UndoManager undoManager=new UndoManager();
private JToolBar toolBar_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Notebook frame = new Notebook();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Notebook() {
setModalExclusionType(ModalExclusionType.TOOLKIT_EXCLUDE);
textArea_1 = new JTextArea();
textArea_1.requestFocusInWindow();
textArea_1.setFont(new Font("黑体",Font.ITALIC,25));
setIconImage(Toolkit.getDefaultToolkit().getImage("images//timg.jpg"));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(400, 200, 1293, 729);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menu = new JMenu("文件");
menu.setActionCommand("menu");
menuBar.add(menu);
JMenuItem mntmCtrln = new JMenuItem("新建 ");
mntmCtrln.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int n=JOptionPane.showConfirmDialog(null, "是否保存当前文件","提示",JOptionPane.YES_NO_OPTION);
if (n==JOptionPane.YES_OPTION) {
saveT();
}
}
});
mntmCtrln.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,ActionEvent.CTRL_MASK));
menu.add(mntmCtrln);
JMenuItem mntmCtrlo = new JMenuItem("打开 ");
mntmCtrlo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,ActionEvent.CTRL_MASK));
mntmCtrlo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO 自动生成的方法存根
openT();
}
});
menu.add(mntmCtrlo);
JMenuItem mntmCtrls = new JMenuItem("保存 ");
mntmCtrls.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.CTRL_MASK));
mntmCtrls.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
saveT();
}
});
menu.add(mntmCtrls);
JMenuItem menuItem = new JMenuItem("另村为");
menuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
saveT();
}
});
menu.add(menuItem);
JMenuItem menuItem_1 = new JMenuItem("页面设置");
menuItem_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JOptionPane jOptionPane=new JOptionPane();
jOptionPane.showMessageDialog(null, "该功能尚未开发");
}
});
menu.add(menuItem_1);
JMenuItem menuItem_2 = new JMenuItem("打印");
menuItem_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JOptionPane jOptionPane=new JOptionPane();
jOptionPane.showMessageDialog(null, "该功能尚未开发");
}
});
menuItem_2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,ActionEvent.CTRL_MASK));
menu.add(menuItem_2);
JMenuItem menuItem_3 = new JMenuItem("退出");
menuItem_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int n=JOptionPane.showConfirmDialog(null, "是否保存","提示",JOptionPane.YES_NO_OPTION);
if (n==JOptionPane.YES_OPTION) {
saveT();
System.exit(0);
}
}
});
menu.add(menuItem_3);
JMenu menu_1 = new JMenu("编辑");
menuBar.add(menu_1);
//撤销
textArea_1.setWrapStyleWord(true);
textArea_1.getDocument().addUndoableEditListener(undoManager);
JMenuItem mntmCtrlz = new JMenuItem("撤销 ");
mntmCtrlz.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (undoManager.canUndo()) {
undoManager.undo();
}
}
});
mntmCtrlz.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,ActionEvent.CTRL_MASK));
menu_1.add(mntmCtrlz);
JMenuItem mntmCtrlx = new JMenuItem("剪切 ");
mntmCtrlx.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea_1.cut();
}
});
mntmCtrlx.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,ActionEvent.CTRL_MASK));
menu_1.add(mntmCtrlx);
JMenuItem mntmCtrlc = new JMenuItem("复制 ");
mntmCtrlc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea_1.copy();
}
});
mntmCtrlc.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.CTRL_MASK));
menu_1.add(mntmCtrlc);
JMenuItem mntmCtrlv = new JMenuItem("粘贴 ");
mntmCtrlv.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea_1.paste();
}
});
mntmCtrlv.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,ActionEvent.CTRL_MASK));
menu_1.add(mntmCtrlv);
JMenuItem mntmDel = new JMenuItem("删除");
mntmDel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea_1.setText("");
}
});
mntmDel.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0));
menu_1.add(mntmDel);
JMenuItem mntmCtrlf = new JMenuItem("查找 ");
mntmCtrlf.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
new Replace(textArea_1);
}
});
mntmCtrlf.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,ActionEvent.CTRL_MASK));
menu_1.add(mntmCtrlf);
JMenuItem mntmF = new JMenuItem("查找下一个");
mntmF.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
new Replace(textArea_1);
}
});
mntmF.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3,0));
menu_1.add(mntmF);
JMenuItem mntmCtrlh = new JMenuItem("替换 ");
mntmCtrlh.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
new Replace(textArea_1);
}
});
mntmCtrlh.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,ActionEvent.CTRL_MASK));
menu_1.add(mntmCtrlh);
JMenuItem mntmCtrlg = new JMenuItem("转到 ");
mntmCtrlg.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new Goto(textArea_1);
}
});
mntmCtrlg.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G,ActionEvent.CTRL_MASK));
menu_1.add(mntmCtrlg);
JMenuItem mntmCtrla = new JMenuItem("全选 ");
mntmCtrla.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea_1.selectAll();
textArea_1.setSelectedTextColor(Color.blue);
}
});
mntmCtrla.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,ActionEvent.CTRL_MASK));
menu_1.add(mntmCtrla);
JMenuItem mntmF_1 = new JMenuItem("日期/时间 ");
mntmF_1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5,0));
mntmF_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
new Date_time(textArea_1);
}
});
menu_1.add(mntmF_1);
JMenu menu_2 = new JMenu("格式");
menuBar.add(menu_2);
JMenuItem menuItem_5 = new JMenuItem("字体");
menuItem_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Window_Font ziti=new Window_Font(textArea_1);
ziti.setVisible(true);
}
});
JCheckBoxMenuItem checkBoxMenuItem_1 = new JCheckBoxMenuItem("自动换行");
checkBoxMenuItem_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (checkBoxMenuItem_1.isSelected()) {
textArea_1.setLineWrap(true);
}else {
textArea_1.setLineWrap(false);
}
}
});
menu_2.add(checkBoxMenuItem_1);
menu_2.add(menuItem_5);
JMenu menu_3 = new JMenu("查看");
menu_3.setHorizontalAlignment(SwingConstants.TRAILING);
menuBar.add(menu_3);
JLabel jLabel=new JLabel(" 第 " + linenum + " 行, 第 " + columnnum+" 列 ");
toolBar_1 = new JToolBar();
toolBar_1.add(jLabel);
toolBar_1.setVisible(false);
textArea_1.addCaretListener(new CaretListener() {
@Override
public void caretUpdate(CaretEvent e) {
// TODO 自动生成的方法存根
JTextArea editArea = (JTextArea)e.getSource();
try {
int caretpos = textArea_1.getCaretPosition();
linenum = editArea.getLineOfOffset(caretpos);
columnnum = caretpos - textArea_1.getLineStartOffset(linenum);
linenum += 1;
jLabel.setText(" 第 " + linenum + " 行, 第 " + (columnnum+1)+" 列 ");
} catch (BadLocationException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
}
});
JCheckBoxMenuItem checkBoxMenuItem = new JCheckBoxMenuItem("状态栏");
checkBoxMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (checkBoxMenuItem.isSelected()) {
toolBar_1.setVisible(true);
}else {
toolBar_1.setVisible(false);
}
}
});
menu_3.add(checkBoxMenuItem);
JMenu menu_4 = new JMenu("帮助");
menuBar.add(menu_4);
JMenuItem mntmh = new JMenuItem("查看帮助(H)");
mntmh.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Desktop desktop = Desktop.getDesktop();
try {
desktop.browse(new URI("https://cn.bing.com/search?q"
+ "=%E8%8E%B7%E5%8F%96%E6%9C%89%E5%85%B3+windows+10+%E4%B8%AD"
+ "%E7%9A%84%E8%AE%B0%E4%BA%8B%E6%9C%AC%E7%9A%84%E5%B8%AE%E5%"
+ "8A%A9&filters=guid:%224466414-zh-hans-dia%22%20lang:%22zh-h"
+ "ans%22&form=T00032&ocid=HelpPane-BingIA"));
} catch (IOException | URISyntaxException e1) {
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
}
});
menu_4.add(mntmh);
JMenuItem mntma = new JMenuItem("关于记事本(A)");
mntma.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// AboutNote aboutNote=new AboutNote();
// aboutNote.setVisible(true);
// new Turtle();
new ShowPicture();
}
});
menu_4.add(mntma);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JToolBar toolBar = new JToolBar();
toolBar.setForeground(Color.WHITE);
JButton button = new JButton("");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int n=JOptionPane.showConfirmDialog(null, "是否保存当前文件","提示",JOptionPane.YES_NO_OPTION);
if (n==JOptionPane.YES_OPTION) {
saveT();
}
}
});
button.setIcon(new ImageIcon("images\\8.png"));
toolBar.add(button);
JButton button_1 = new JButton("");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
openT();
}
});
button_1.setIcon(new ImageIcon("images\\9.png"));
toolBar.add(button_1);
JButton button_2 = new JButton("");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
saveT();
}
});
button_2.setIcon(new ImageIcon("images\\10.png"));
toolBar.add(button_2);
JButton button_3 = new JButton("");
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea_1.copy();
}
});
button_3.setIcon(new ImageIcon("images\\11.png"));
toolBar.add(button_3);
JButton button_4 = new JButton("");
button_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea_1.cut();
}
});
button_4.setIcon(new ImageIcon("images\\12.png"));
toolBar.add(button_4);
JButton button_5 = new JButton("");
button_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textArea_1.paste();
}
});
button_5.setIcon(new ImageIcon("images\\13.png"));
toolBar.add(button_5);
contentPane.setLayout(new BorderLayout(0, 0));
JScrollPane jScrollPane=new JScrollPane(textArea_1);
contentPane.add(jScrollPane);
contentPane.add(toolBar, BorderLayout.NORTH);
contentPane.add(toolBar_1, BorderLayout.SOUTH);
}
//保存
public void saveT() {
new Writefile(this,textArea_1);
}
//打开
public void openT() {
new Openfile(this,textArea_1);
}
}