(一)简介

来自一大学的大一学生,小白上路第一次写博客,还有许多不足,请大家多多指出问题和看法!欢迎交流!代码最后有邮箱联系~

(二)记事本

实现了一些简单基础的功能,代码自认为还比较整洁(狗头),有在网上参考各路大神的代码,如有侵权请联系我删除~

话不多说,直接上代码!!!!!!!!!!!!!!!!!!


import javax.swing.*;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;
import javax.swing.undo.UndoManager;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Calendar;public class Mytext extends JFrame implements ActionListener {JTextArea myTextArea;JScrollPane my;JMenuBar myMenuBar;JMenu file, edit, source, help;JMenuItem New, Open, Save, NewWindow, exit, SaveAS;JMenuItem Cut, Copy, Paste, Delete, Time, Brow, Find, Replace, AllSelectd, revoke;JMenuItem autoLine, FontSet;JMenuItem Edition,Author;Highlighter highlighter;UndoManager und;boolean flag = false;int cl = 1;public static void main(String[] args) {new Mytext();}public Mytext() {myTextArea = new JTextArea();my = new JScrollPane(myTextArea);myMenuBar = new JMenuBar();file = new JMenu("文件(F)");edit = new JMenu("编辑(E)");source = new JMenu("格式(O)");help = new JMenu("帮助(H)");//设置ALT快捷键file.setMnemonic('F');edit.setMnemonic('E');source.setMnemonic('O');help.setMnemonic('H');New = new JMenuItem("新建(N)");New.setActionCommand("create");New.addActionListener(this);New.setAccelerator(KeyStroke.getKeyStroke((char) KeyEvent.VK_N,InputEvent.CTRL_MASK));Open = new JMenuItem("打开(O)");Open.setActionCommand("open");Open.addActionListener(this);Open.setAccelerator(KeyStroke.getKeyStroke((char) KeyEvent.VK_O,InputEvent.CTRL_MASK));Save = new JMenuItem("保存(S)");Save.setActionCommand("save");Save.addActionListener(this);Save.setAccelerator(KeyStroke.getKeyStroke((char) KeyEvent.VK_S,InputEvent.CTRL_MASK));NewWindow = new JMenuItem("新窗口(W)");NewWindow.setActionCommand("new_window");NewWindow.addActionListener(this);NewWindow.setAccelerator(KeyStroke.getKeyStroke((char) KeyEvent.VK_W,InputEvent.CTRL_MASK));SaveAS = new JMenuItem("另存为(A)");SaveAS.setActionCommand("SaveAs");SaveAS.addActionListener(this);SaveAS.setAccelerator(KeyStroke.getKeyStroke((char) KeyEvent.VK_A,InputEvent.CTRL_MASK));exit = new JMenuItem("退出(X)");exit.setActionCommand("exit");exit.addActionListener(this);exit.setAccelerator(KeyStroke.getKeyStroke((char) KeyEvent.VK_X,InputEvent.CTRL_MASK));Cut = new JMenuItem("剪切(T)");Cut.setActionCommand("cut");Cut.addActionListener(this);Cut.setAccelerator(KeyStroke.getKeyStroke((char) KeyEvent.VK_T,InputEvent.CTRL_MASK));Copy = new JMenuItem("复制(C)");Copy.setActionCommand("copy");Copy.addActionListener(this);Copy.setAccelerator(KeyStroke.getKeyStroke((char) KeyEvent.VK_C,InputEvent.CTRL_MASK));Paste = new JMenuItem("粘贴(P)");Paste.setActionCommand("paste");Paste.addActionListener(this);Paste.setAccelerator(KeyStroke.getKeyStroke((char) KeyEvent.VK_P,InputEvent.CTRL_MASK));Time = new JMenuItem("时间/日期(D)");Time.setActionCommand("time");Time.addActionListener(this);Time.setAccelerator(KeyStroke.getKeyStroke((char) KeyEvent.VK_D,InputEvent.CTRL_MASK));Delete = new JMenuItem("删除(L)");Delete.setActionCommand("delete");Delete.addActionListener(this);Delete.setAccelerator(KeyStroke.getKeyStroke((char) KeyEvent.VK_L,InputEvent.CTRL_MASK));Brow = new JMenuItem("使用百度搜索(E)");Brow.setActionCommand("brow");Brow.addActionListener(this);Brow.setAccelerator(KeyStroke.getKeyStroke((char) KeyEvent.VK_E,InputEvent.CTRL_MASK));Find = new JMenuItem("查找(F)");Find.setActionCommand("find");Find.addActionListener(this);Find.setAccelerator(KeyStroke.getKeyStroke((char) KeyEvent.VK_F,InputEvent.CTRL_MASK));Replace = new JMenuItem("替换(R)");Replace.setActionCommand("replace");Replace.addActionListener(this);Replace.setAccelerator(KeyStroke.getKeyStroke((char) KeyEvent.VK_R,InputEvent.CTRL_MASK));AllSelectd = new JMenuItem("全选(Q)");AllSelectd.setActionCommand("all_selected");AllSelectd.addActionListener(this);AllSelectd.setAccelerator(KeyStroke.getKeyStroke((char) KeyEvent.VK_Q,InputEvent.CTRL_MASK));revoke = new JMenuItem("撤销(Z)");revoke.setActionCommand("revoke");revoke.setAccelerator(KeyStroke.getKeyStroke((char)KeyEvent.VK_Z,InputEvent.CTRL_MASK));autoLine = new JCheckBoxMenuItem("自动换行(W)");autoLine.setActionCommand("autoLine");autoLine.setAccelerator(KeyStroke.getKeyStroke((char) KeyEvent.VK_W,InputEvent.CTRL_MASK));FontSet = new JMenuItem("字体(I)");FontSet.setActionCommand("FontSet");FontSet.addActionListener(this);FontSet.setAccelerator(KeyStroke.getKeyStroke((char) KeyEvent.VK_I,InputEvent.CTRL_MASK));Edition = new JMenuItem("版本");Edition.setActionCommand("Edition");Edition.addActionListener(this);Author = new JMenuItem("作者");Author.setActionCommand("Author");Author.addActionListener(this);myMenuBar.add(file);myMenuBar.add(edit);myMenuBar.add(source);myMenuBar.add(help);file.add(New);file.addSeparator();file.add(Open);file.addSeparator();file.add(Save);file.addSeparator();file.add(NewWindow);file.addSeparator();file.add(SaveAS);file.addSeparator();file.add(exit);edit.add(Cut);edit.addSeparator();edit.add(Copy);edit.addSeparator();edit.add(Paste);edit.addSeparator();edit.add(Delete);edit.addSeparator();edit.add(Time);edit.addSeparator();edit.add(Brow);edit.addSeparator();edit.add(AllSelectd);edit.addSeparator();edit.add(Find);edit.addSeparator();edit.add(Replace);edit.addSeparator();edit.add(revoke);source.add(autoLine);source.addSeparator();source.add(FontSet);help.add(Edition);help.addSeparator();help.add(Author);this.add(my);this.setTitle("记事本");this.setIconImage(new ImageIcon("C:\\Users\\13513\\Pictures\\1707319142.jpeg").getImage());this.setJMenuBar(myMenuBar);this.setSize(500, 500);this.setLocation(100, 200);this.setResizable(true);this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);this.setVisible(true);// 设置自动换行autoLine.addActionListener(e -> {cl++;flag = cl % 2 == 0;myTextArea.setLineWrap(flag);});und = new UndoManager();// 实现撤销功能myTextArea.getDocument().addUndoableEditListener(und);revoke.addActionListener(e -> {if (und.canUndo())und.undo();});setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);    //置窗口是否可以关闭edit.addMenuListener(new MenuListener() {//当选择编辑时判断可用性@Overridepublic void menuSelected(MenuEvent e) {checkMenuItemEnabled();}@Overridepublic void menuDeselected(MenuEvent e) {checkMenuItemEnabled();}@Overridepublic void menuCanceled(MenuEvent e) {checkMenuItemEnabled();}});addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e){checkFileIsSave();}});}//设置菜单项的可用性:剪切,复制,删除功能public void checkMenuItemEnabled() {String selectText=myTextArea.getSelectedText();if(selectText==null) {Cut.setEnabled(false);Copy.setEnabled(false);Delete.setEnabled(false);}else {Cut.setEnabled(true);Copy.setEnabled(true);Delete.setEnabled(true);}}public void checkFileIsSave(){if(myTextArea.getText().equals("") || myTextArea==null){this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);}else{int exitChoose=JOptionPane.showConfirmDialog(this,"您的文件是否保存?","退出提示",JOptionPane.YES_NO_OPTION);if(exitChoose==JOptionPane.YES_OPTION) {saveFile();this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);}else if(exitChoose==JOptionPane.NO_OPTION){this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);}}}//关闭窗口时调用方法结束public void saveFile() {//保存文件方法JFileChooser jFileChooser = new JFileChooser();jFileChooser.setDialogTitle("记事本");jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);//设置模式为只可以选择jFileChooser.showOpenDialog(null);//默认值jFileChooser.setVisible(true);String abs = jFileChooser.getSelectedFile().getAbsolutePath();FileWriter fw = null;BufferedWriter bw = null;File file = new File(abs);try {fw = new FileWriter(file);bw = new BufferedWriter(fw);String[] s = myTextArea.getText().split("\n");for (String value : s) {bw.write(value + "\n");bw.flush();}} catch (Exception e) {e.printStackTrace();} finally {try {assert bw != null;bw.close();fw.close();} catch (IOException e) {e.printStackTrace();}}}public String readFile() throws IOException {//读取文件方法FileReader fileReader = null;BufferedReader bufferedReader = null;JFileChooser jFileChooser = new JFileChooser();jFileChooser.setDialogTitle("记事本");jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);//设置模式为只可以选择jFileChooser.showOpenDialog(null);//默认值jFileChooser.setVisible(true);String abs = jFileChooser.getSelectedFile().getAbsolutePath();try {fileReader = new FileReader(abs);bufferedReader = new BufferedReader(fileReader);String now;String all = "";while ((now = bufferedReader.readLine()) != null) {all += now + "\n";return all;}} catch (Exception e) {e.printStackTrace();} finally {try {assert bufferedReader != null;bufferedReader.close();fileReader.close();} catch (IOException e) {e.printStackTrace();}}return null;}@Override // 所有监听事件public void actionPerformed(ActionEvent e) {if (e.getActionCommand().equals("create")) { // 新建if (myTextArea.getText() == null || "".equals(myTextArea.getText())) {return;} else {int num = JOptionPane.showConfirmDialog(null, "你确定保存吗?", "记事本",JOptionPane.YES_NO_CANCEL_OPTION);if (num == 0) {saveFile();this.dispose();new Mytext();} else if (num == 1) {this.dispose();new Mytext();}}}if (e.getActionCommand().equals("save")) {//保存saveFile();}if (e.getActionCommand().equals("open")) {//打开if(myTextArea.getText() == null || "".equals(myTextArea.getText())) {try {myTextArea.append(readFile());} catch (IOException ex) {ex.printStackTrace();}}else {int num = JOptionPane.showConfirmDialog(null, "你是否要保存?", "保存提示",JOptionPane.YES_NO_CANCEL_OPTION);if (num == 0) {saveFile();myTextArea.replaceRange("", 0, myTextArea.getText().length());try {myTextArea.append(readFile());} catch (IOException ex) {ex.printStackTrace();}} else if (num == 1) {myTextArea.replaceRange("", 0, myTextArea.getText().length());try {myTextArea.append(readFile());} catch (IOException ex) {ex.printStackTrace();}}}}if (e.getActionCommand().equals("exit")) {//退出this.dispose();}if (e.getActionCommand().equals("new_window")) {//新窗口new Mytext();}if (e.getActionCommand().equals("SaveAs")) {//另存为saveFile();}if (e.getActionCommand().equals("copy")) {//复制myTextArea.copy();}if (e.getActionCommand().equals("paste")) {//粘贴myTextArea.paste();}if (e.getActionCommand().equals("cut")) {//剪切myTextArea.copy();myTextArea.cut();}if (e.getActionCommand().equals("all_selected")) {//全选myTextArea.selectAll();}if (e.getActionCommand().equals("FontSet")) {//设置字体JDialog jDialog = new JDialog(this,"字体",false);JPanel jp = new JPanel();JPanel jp2 = new JPanel();JPanel jp3 = new JPanel();jDialog.setLayout(new BorderLayout());//字体面板和标签JLabel label1 = new JLabel("字体:");GraphicsEnvironment eee = GraphicsEnvironment.getLocalGraphicsEnvironment();String[] ziti = eee.getAvailableFontFamilyNames();JComboBox<Object> cmb1 = new JComboBox<>(ziti);jp.add(label1);jp.add(cmb1);//字体面板和标签JLabel label2 = new JLabel("字体大小:");String[] nm = {"8","9","10","11","12","14","16","18", "20", "24", "26","28","36","48","72"};JComboBox<Object> cmb2 = new JComboBox<>(nm);jp2.add(label2);jp2.add(cmb2);JLabel label3 = new JLabel("字形");String[] zixing = {"常规","粗体","倾斜","倾斜 粗体"};JComboBox<Object> cmb3 = new JComboBox<>(zixing);jp2.add(label3);jp2.add(cmb3);//面板加入窗口jDialog.add(jp, BorderLayout.NORTH);jDialog.add(jp2, BorderLayout.CENTER);//  按钮设置JButton jButton = new JButton("确定");JButton exit_jButton = new JButton("取消");jButton.setSize(50, 20);jButton.setSize(50, 20);jp3.add(jButton);jp3.add(exit_jButton);jDialog.add(jp3, BorderLayout.SOUTH);jDialog.setBounds(400, 400, 300, 150);jDialog.setVisible(true);jDialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);// 按钮监听jButton.addActionListener(e1 -> {int s1 = cmb1.getSelectedIndex();//返回字体和大小的索引int s2 = cmb2.getSelectedIndex();int s3 = cmb3.getSelectedIndex();jDialog.add(jp, BorderLayout.WEST);jDialog.add(jp2, BorderLayout.EAST);String name = ziti[s1];String num = nm[s2];int x = Integer.parseInt(String.valueOf(num));//把num转化为int型if(s3 == 0){myTextArea.setFont(new Font(name, Font.PLAIN, x)); //设置文本域}else if(s3 == 1){myTextArea.setFont(new Font(name, Font.BOLD, x)); //设置文本域}else if(s3 == 2){myTextArea.setFont(new Font(name, Font.ITALIC, x)); //设置文本域}else if(s3 == 3){myTextArea.setFont(new Font(name, Font.BOLD+ Font.ITALIC, x)); //设置文本域}});exit_jButton.addActionListener(e16 -> jDialog.dispose());}if (e.getActionCommand().equals("delete")) {myTextArea.replaceRange("", myTextArea.getSelectionStart(), myTextArea.getSelectionEnd());}if (e.getActionCommand().equals("time")) {Calendar calendar = Calendar.getInstance();SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm yyyy/MM/dd");myTextArea.append(dateFormat.format(calendar.getTime()));}if (e.getActionCommand().equals("brow")) {String str = myTextArea.getSelectedText();String web;if (str == null) {web = "https://www.baidu.com/?";}else {web = "https://www.baidu.com/s?wd=" + str;}try {URI uri = new URI(web);Desktop.getDesktop().browse(uri);} catch(Exception ee){ee.printStackTrace();}}if (e.getActionCommand().equals("replace")) {JDialog replaceDialog=new JDialog(this,"替换",false);//false时允许其他窗口同时处于激活状态(即无模式)Container con=replaceDialog.getContentPane();//返回此对话框的contentPane对象con.setLayout(new FlowLayout(FlowLayout.CENTER));JLabel findContentLabel=new JLabel("查找内容");JLabel replaceLabel=new JLabel("替换为");JTextField findText=new JTextField(15);JTextField replaceText=new JTextField(15);JButton findNextButton=new JButton("查找下一个");JButton replaceButton=new JButton("替换");JButton replaceAllButton=new JButton("全部替换");JButton cancel=new JButton("取消");JCheckBox matchCheckBox=new JCheckBox("区分大小写");JCheckBox circleButton = new JCheckBox("循环",true);JPanel panel1=new JPanel();JPanel panel2=new JPanel();JPanel panel3=new JPanel();JPanel panel4=new JPanel();panel4.setLayout(new GridLayout(2,1));panel1.add(findContentLabel);panel1.add(findText);panel1.add(findNextButton);panel4.add(replaceButton);panel4.add(replaceAllButton);panel2.add(replaceLabel);panel2.add(replaceText);panel2.add(panel4);panel3.add(matchCheckBox);panel3.add(circleButton);panel3.add(cancel);con.add(panel1);con.add(panel2);con.add(panel3);replaceDialog.setSize(420,220);replaceDialog.setResizable(false);//不可调整大小replaceDialog.setLocation(230,280);replaceDialog.setVisible(true);DefaultHighlighter.DefaultHighlightPainter p = new DefaultHighlighter.DefaultHighlightPainter(Color.blue);highlighter = myTextArea.getHighlighter();replaceDialog.addComponentListener(new ComponentAdapter() {@Overridepublic void componentHidden(ComponentEvent e) {highlighter.removeAllHighlights();}});cancel.addActionListener(e12 -> {highlighter.removeAllHighlights();replaceDialog.dispose();});findNextButton.addActionListener(e3 -> {highlighter.removeAllHighlights();int k;String str1,str2,str3,str4,strA,strB;str1=myTextArea.getText();str2=findText.getText();str3=str1.toUpperCase();str4=str2.toUpperCase();if(matchCheckBox.isSelected()){strA=str1;strB=str2;}else{strA=str3;strB=str4;}//都转成大写if (circleButton.isSelected()) { // 循环按钮实现if (myTextArea.getSelectedText() == null)k=strA.indexOf(strB,myTextArea.getCaretPosition()+1);else{k=strA.indexOf(strB, myTextArea.getCaretPosition()-findText.getText().length()+1);}if(k==-1 &&(strA.contains(strB))) {k = strA.indexOf(strB);}if(k>-1){myTextArea.setCaretPosition(k);try {//高光highlighter.addHighlight(k, k+strB.length(), p);} catch (BadLocationException ex) {ex.printStackTrace();}myTextArea.select(k,k+strB.length());}else{JOptionPane.showMessageDialog(null,"找不到查找的内容!","查找",JOptionPane.INFORMATION_MESSAGE);}}else { //否则向下循环if (myTextArea.getSelectedText() == null)k = strA.indexOf(strB, myTextArea.getCaretPosition() + 1);elsek = strA.indexOf(strB, myTextArea.getCaretPosition() - findText.getText().length() + 1);if (k > -1) {myTextArea.setCaretPosition(k);try {//高光highlighter.addHighlight(k, k+strB.length(), p);} catch (BadLocationException ex) {ex.printStackTrace();}myTextArea.select(k, k + strB.length());} else {JOptionPane.showMessageDialog(null, "找不到查找的内容!", "代替", JOptionPane.INFORMATION_MESSAGE);}}});replaceButton.addActionListener(e99 -> {highlighter.removeAllHighlights();int m = myTextArea.getSelectionStart();if(replaceText.getText().length()==0 && myTextArea.getSelectedText() != null)myTextArea.replaceSelection("");if(replaceText.getText().length()>0 && myTextArea.getSelectedText() !=null) {myTextArea.replaceSelection(replaceText.getText());}myTextArea.setCaretPosition(m);});replaceAllButton.addActionListener(e14 -> {highlighter.removeAllHighlights();if (matchCheckBox.isSelected()) {if(replaceText.getText().length()>0) {String a = myTextArea.getText().replace(findText.getText(), replaceText.getText());int txtAreaLength = myTextArea.getText().length();myTextArea.replaceRange(a, 0, txtAreaLength);}else{String a = myTextArea.getText().replace(findText.getText(),"");int txtAreaLength = myTextArea.getText().length();myTextArea.replaceRange(a, 0, txtAreaLength);}} else {String str1, str2, str3, str4;str1 = myTextArea.getText();str2 = findText.getText();str3 = str1.toUpperCase();str4 = str2.toUpperCase();String str = str3;//第一种方法int frontLength = 0;while (str.contains(str4)) {int index = str.indexOf(str4);myTextArea.replaceRange(replaceText.getText(),index+frontLength,index+frontLength+replaceText.getText().length());frontLength += (index + str4.length());str = str.substring(index + str4.length());}}});}if(e.getActionCommand().equals("find")){JDialog findDialog=new JDialog(this,"查找",false);//false时允许其他窗口同时处于激活状态(即无模式)Container con=findDialog.getContentPane();//返回此对话框的contentPane对象con.setLayout(new FlowLayout(FlowLayout.LEFT));JLabel findContentLabel=new JLabel("查找内容:");JTextField findText=new JTextField(15);JButton findNextButton=new JButton("查找下一个");JCheckBox matchCheckBox=new JCheckBox("区分大小写");JCheckBox circleButton=new JCheckBox("循环");ButtonGroup bGroup=new ButtonGroup();JRadioButton upButton=new JRadioButton("向上",true);JRadioButton downButton=new JRadioButton("向下");downButton.setSelected(true);bGroup.add(upButton);bGroup.add(downButton);JButton cancel=new JButton("取消");JPanel panel1=new JPanel();JPanel panel2=new JPanel();JPanel panel3=new JPanel();JPanel directionPanel=new JPanel();directionPanel.setBorder(BorderFactory.createTitledBorder("方 向"));directionPanel.add(upButton);directionPanel.add(downButton);panel1.setLayout(new GridLayout(2,1));panel1.add(findNextButton);panel1.add(cancel);panel2.add(findContentLabel);panel2.add(findText);panel2.add(panel1);panel3.add(matchCheckBox);panel3.add(circleButton);panel3.add(directionPanel);con.add(panel2);con.add(panel3);findDialog.setSize(410,180);findDialog.setResizable(false);//不可调整大小findDialog.setLocation(230,280);findDialog.setVisible(true);DefaultHighlighter.DefaultHighlightPainter p = new DefaultHighlighter.DefaultHighlightPainter(Color.blue);highlighter = myTextArea.getHighlighter();findDialog.addComponentListener(new ComponentAdapter() {@Overridepublic void componentHidden(ComponentEvent e) {highlighter.removeAllHighlights();}});//取消按钮事件处理cancel.addActionListener(e12 -> {highlighter.removeAllHighlights();findDialog.dispose();});findNextButton.addActionListener(e13 -> {highlighter.removeAllHighlights();String str1,str2,str3,str4,strA,strB;str1=myTextArea.getText();str2=findText.getText();str3=str1.toUpperCase();str4=str2.toUpperCase();if(matchCheckBox.isSelected()){strA=str1;strB=str2;}else{strA=str3;strB=str4;}if(upButton.isSelected()){ //向上开始int k;if(myTextArea.getSelectedText()==null)k=strA.lastIndexOf(strB,myTextArea.getCaretPosition());elsek=strA.lastIndexOf(strB, myTextArea.getCaretPosition()-findText.getText().length()-1);if(k>-1){myTextArea.setCaretPosition(k);try {//高光highlighter.addHighlight(k, k+strB.length(), p);myTextArea.select(k,k+strB.length());} catch (BadLocationException ex) {ex.printStackTrace();}}else if(circleButton.isSelected()&& k==-1 &&(strA.contains(strB))){k = strA.lastIndexOf(strB);if(k>-1){myTextArea.setCaretPosition(k);
//                        myTextArea.select(k,k+strB.length());try {//高光highlighter.addHighlight(k, k+strB.length(), p);myTextArea.select(k,k+strB.length());} catch (BadLocationException ex) {ex.printStackTrace();}}else{myTextArea.setCaretPosition(0);String s = findText.getText();String ss = "找不到" + s;JOptionPane.showMessageDialog(null,ss,"查找",JOptionPane.INFORMATION_MESSAGE);}}else{myTextArea.setCaretPosition(0);String s = findText.getText();String ss = "找不到" + s;JOptionPane.showMessageDialog(null,ss,"查找",JOptionPane.INFORMATION_MESSAGE);}}else if(downButton.isSelected()) {int k;if (myTextArea.getSelectedText() == null) {k = strA.indexOf(strB, myTextArea.getCaretPosition());}elsek = strA.indexOf(strB, myTextArea.getCaretPosition() - findText.getText().length() + 1);if (k > -1) {myTextArea.setCaretPosition(k);try {//高光highlighter.addHighlight(k, k + strB.length(), p);myTextArea.select(k,k+strB.length());} catch (BadLocationException ex) {ex.printStackTrace();}}else if (circleButton.isSelected() && k == -1 && (strA.contains(strB))) {k = strA.indexOf(strB);if (k > -1) {myTextArea.setCaretPosition(k);
//                        myTextArea.select(k,k+strB.length());try {//高光highlighter.addHighlight(k, k + strB.length(), p);myTextArea.select(k,k+strB.length());} catch (BadLocationException ex) {ex.printStackTrace();}} else {String s = findText.getText();String ss = "找不到" + s;JOptionPane.showMessageDialog(null, ss, "查找", JOptionPane.INFORMATION_MESSAGE);}}else{String s = findText.getText();String ss = "找不到" + s;myTextArea.setCaretPosition(myTextArea.getText().length());JOptionPane.showMessageDialog(null, ss, "查找", JOptionPane.INFORMATION_MESSAGE);}System.out.println(" ");}/*可以不用管这部分*/
//                }else if (circleButton.isSelected()) {// 选择循环
//                    if (myTextArea.getSelectedText() == null)
//                        k=strA.indexOf(strB,myTextArea.getCaretPosition()+1);
//                    else{
//                        k=strA.indexOf(strB, myTextArea.getCaretPosition()-findText.getText().length()+1);
//                    }
//                    if(k==-1 &&(strA.contains(strB))) {
//                        k = strA.indexOf(strB);
//                    }
//                    if(k>-1){
//
//                        myTextArea.setCaretPosition(k);
//                        myTextArea.select(k,k+strB.length());
//                        try {//高光
//                            highlighter.addHighlight(k, k+strB.length(), p);
//                        } catch (BadLocationException ex) {
//                            ex.printStackTrace();
//                        }
//                    }else{
//                        JOptionPane.showMessageDialog(null,"找不到查找的内容!","查找",JOptionPane.INFORMATION_MESSAGE);
//                    }
//                }});}if(e.getActionCommand().equals("Edition")){JOptionPane.showMessageDialog(null,"版本1.0\n"+"有问题和建议可以联系我o","版本3.0",JOptionPane.PLAIN_MESSAGE);}if(e.getActionCommand().equals("Author")){JOptionPane.showMessageDialog(null, """积分安联系邮箱:2021132070@stu.cuit.edu.cn""","作者",JOptionPane.PLAIN_MESSAGE);}}
}

--------------------------------------------------------------------------------------------------------------------------------

更新2022.12.1

二代版本很早写出来但是一直没发布

有人问我要源码才想起来更新哈哈哈哈哈哈sorry,现在为记事本第二代,基本功能都实现了。

用Java编写记事本相关推荐

  1. java编写记事本程序出现图形,高手帮忙啊,老师布置了一个作业,要用java编写一个记事本程序...

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 super(arg0); // TODO Auto-generated constructor stub initialize(); } /** * @p ...

  2. java applet 记事本_jsb java编写的安卓app记事本程序,适合app初学者 Applet 243万源代码下载- www.pudn.com...

    文件名称: jsb下载  收藏√  [ 5  4  3  2  1 ] 开发工具: Java 文件大小: 86 KB 上传时间: 2015-08-25 下载次数: 0 提 供 者: dr 详细说明:j ...

  3. 用Java编写有读取和保存文本功能的记事本程序

    用Java编写有读取和保存文本功能的记事本程序 import java.awt.*; import java.awt.event.*;import javax.naming.InitialContex ...

  4. java 日历记事本_calendar 一个用java编写的日历记事本. 具有正常日历功能;也可以用于在不同日期记录下当日重要的事情 - 下载 - 搜珍网...

    日历记事本/日历记事本/build/classes/日历记事本/CalendarPad$1.class 日历记事本/日历记事本/build/classes/日历记事本/CalendarPad.clas ...

  5. Java简单记事本设计实验报告_基于JAVA的记事本设计报告.doc

    基于JAVA的记事本设计报告 华北科技学院计算机系综合性实验报告 PAGE 第 PAGE 11 页 XX学校计算机系综合性实验 实 验 报 告 课程名称 Java程序设计 实验学期 至 学年 第 学期 ...

  6. java程序步骤_java编写程序的步骤是什么?java编写程序步骤实例讲解

    写java编程就是一步一步的来,这样才会写好一个编程,之后也才能正常的运行,那么java编写程序的步骤是什么?今天我们就来给大家讲解一下这方面的内容.大家可以参考以下文章! 1.编写源文件 使用文本编 ...

  7. java怎么用_如何使用Java编写程序

    步骤1:您需要什么: 1)一台运行Windows的PC(任何Windows软件将起作用:XP以外的其他软件可能需要稍作修改. 请参见下面的链接.) 2)Internet连接 3)管理能力 为了开始编程 ...

  8. java编写代码用什么_如何学习用Java编写代码:为什么要学习以及从哪里开始

    java编写代码用什么 by John Selawsky 约翰·塞劳斯基(John Selawsky) 如何学习用Java编写代码:为什么要学习以及从哪里开始 (How to learn to cod ...

  9. java培训教程分享:Java编写软件代码自动提示功能

    本期的java培训教程分享主要是介绍的java编写软件代码的一个自动提示功能,很多零基础和初学java的同学们对这一块还不是很了解,Eclipse for android 实现代码自动提示智能提示功能 ...

  10. 【项目展示】一个有点难度的猜数字小游戏(Java编写)

    (声明:本文部分图片来自网络,如有侵权请联系,将第一时间删除或更换图片) 本文目录 1.前言 2.规格说明 3.源代码 4.运行结果 5.感想 1.前言 我在自己的博客里上传了一些本科时编过.最近又改 ...

最新文章

  1. SAP IDoc Post不成功,报错 - Conventional invoice verification no longer maintained as of Release 4.6-
  2. AWS无服务开发Lambda系列之本地上传包至Lambda
  3. 批量从网上下载图片、zip等文件到本地[java爬虫]
  4. WeCenter3.1.7 blind xxe 分析
  5. SpringSecurity动态加载用户角色权限实现登录及鉴权
  6. unit 12 文档练习
  7. 阿里架构师必学的2019最新资料!首次公布
  8. 计算机多媒体处理的是什么意思,多媒体处理的是什么信号
  9. NPM ----快速删除node_modules
  10. RHEL/Centos下VSFTPD服务器搭建
  11. 在中标麒麟上基于源码安装第二个gcc编译器
  12. 内网渗透之隐藏通信隧道技术
  13. 电子商务概论(农)之形考作业三
  14. 计算机盘快捷键,电脑键盘快捷键全解
  15. 如何理解前后端分离HTML5,Web前后端分离开发思路
  16. JDBC 和数据库连接池
  17. wlan消失 网络适配器文件夹空了 设备管理器黄色感叹号 wifi那里看不到任何WiFi解决
  18. PB 导出的Excel,打开会提示文件格式和扩展名不匹配问题
  19. 【ValueError: could not convert string to float: ‘young‘】python利用pandas对string类型的数据序列化
  20. 设计PCB螺旋线圈、电感线圈

热门文章

  1. pytorch.chunk
  2. web如何伪装自己的IP地址
  3. photoshop涂抹工具
  4. 行尸走肉第一季/全集The Walking Dead迅雷下载
  5. 人民的名义关系可视化展示
  6. excel表格打印每页都有表头_EXCEL打印小技巧:如何打印出每张纸上都有表头标题的表格?...
  7. java压_JAVA背压
  8. Java具有哪些语言特点
  9. Canvas绘制六边形网格
  10. 神武服务器维护打副本,9月9日服务器例行维护公告