diff --git a/nullnull b/nullnull new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/src/java2022spring/Test.java b/src/java2022spring/Test.java index 24deb29bfc0e5f50fdedcd21b504e77b529d48b6..11a91f7f2957c08820391f186fab363fe85ee2b7 100644 --- a/src/java2022spring/Test.java +++ b/src/java2022spring/Test.java @@ -1,7 +1,762 @@ package java2022spring; +import java.awt.BorderLayout; +import java.awt.Font; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.InputEvent; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; +import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.text.JTextComponent; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JOptionPane; +import javax.swing.JTextField; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.awt.Color; +import java.awt.GraphicsEnvironment; +import javax.swing.BorderFactory; +import javax.swing.JColorChooser; +import javax.swing.JDialog; +import javax.swing.JList; +import javax.swing.JScrollPane; +import javax.swing.JTextArea; +import javax.swing.border.Border; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; +import javax.swing.JCheckBoxMenuItem; +import javax.swing.JFileChooser; +import javax.swing.JMenu; +import javax.swing.JMenuBar; +import javax.swing.JMenuItem; +import javax.swing.JPopupMenu; +import javax.swing.KeyStroke; +import javax.swing.SwingConstants; +import javax.swing.UIManager; +import javax.swing.event.UndoableEditEvent; +import javax.swing.event.UndoableEditListener; +import javax.swing.undo.UndoManager; +public class Test extends JFrame implements ActionListener { + private static final long serialVersionUID=1L; + JMenuBar menuBar=new JMenuBar(); + JMenu file=new JMenu("文件(F)"); + JMenu edit=new JMenu("编辑(E)"); + JMenu formats=new JMenu("格式(O)"); + JMenu check=new JMenu("查看(V)"); + JMenu tool=new JMenu("工具(V)"); + JMenu help=new JMenu("帮助(H)"); + // 文件菜单 + JMenuItem create=new JMenuItem("新建(N)"); + JMenuItem open=new JMenuItem("打开(O)..."); + JMenuItem save=new JMenuItem("保存(S)"); + JMenuItem otherSave=new JMenuItem("另存为(A)..."); + JMenuItem exit=new JMenuItem("退出(X)"); + // 编辑菜单 + JMenuItem undo=new JMenuItem("撤销(U)"); + JMenuItem cut=new JMenuItem("剪切(T)"); + JMenuItem copy=new JMenuItem("复制(C)"); + JMenuItem paste=new JMenuItem("粘贴(P)"); + JMenuItem delete=new JMenuItem("删除(D)"); + JMenuItem selectAll=new JMenuItem("全选(A)"); + JMenuItem timeDate=new JMenuItem("时间/日期(D)"); + // 格式菜单 + JCheckBoxMenuItem lineWrap=new JCheckBoxMenuItem("自动换行(W)"); + JMenuItem fonts=new JMenuItem("字体(F)..."); + // 查看菜单 + JCheckBoxMenuItem status=new JCheckBoxMenuItem("状态栏"); + // 工具菜单 + JMenuItem calc=new JMenuItem("字数统计"); + // 帮助菜单 + JMenuItem about=new JMenuItem("关于编辑器(A)"); + // 右键弹出菜单 + JPopupMenu popupMenu=new JPopupMenu(); + JMenuItem MenuMouseUndo=new JMenuItem("撤销(U)"); + JMenuItem MenuMouseCut=new JMenuItem("剪切(T)"); + JMenuItem MenuMouseCopy=new JMenuItem("复制(C)"); + JMenuItem MenuMousePaste=new JMenuItem("粘贴(P)"); + JMenuItem MenuMouseDelete=new JMenuItem("删除(D)"); + JMenuItem MenuMouseselectAll=new JMenuItem("全选(A)"); + JLabel stateBar; + + JFileChooser fileChooser; + JTextArea textArea; + JScrollPane scrollPane; + boolean isSaved=true; // 判断文件是否已保存过 + boolean isDistinguish=false; // 是否区分大小写标示 + boolean isFindExist=false; // 是否已经有查找内容 + int mark=0; // 用于查找的索引 + UndoManager undoManager=new UndoManager(); + Find findFrame=new Find(this); + Replace replaceFrame=new Replace(this); + + public Test() { + init(); + isSaved=true;//刚刚打开的窗口状态为已保存 + this.setTitle("文本编辑器"); + this.setSize(800,600); + this.setVisible(true); + this.setLocationRelativeTo(null); + this.setDefaultCloseOperation(EXIT_ON_CLOSE); + } + + public void init() { + // 添加菜单栏及菜单项 + menuBar.add(file); + menuBar.add(edit); + menuBar.add(formats); + menuBar.add(check); + menuBar.add(tool); + menuBar.add(help); + // 文件菜单 + file.add(create); + file.add(open); + file.add(save); + file.add(otherSave); + file.addSeparator(); + file.add(exit); + // 编辑菜单 + edit.add(undo); + edit.addSeparator();// 分割线 + edit.add(cut); + edit.add(copy); + edit.add(paste); + edit.add(delete); + edit.addSeparator(); + edit.add(selectAll); + edit.add(timeDate); + // 查看菜单 + check.add(status); + status.setSelected(true); + // 工具菜单 + tool.add(calc); + // 格式菜单 + formats.add(lineWrap); + lineWrap.setSelected(true);// 初始化时即勾选自动换行 + formats.add(fonts); + // 帮助菜单 + help.add(about); + this.setJMenuBar(menuBar); + + textArea=new JTextArea(); + textArea.setFont(new Font("宋体",Font.PLAIN,16));//设置默认字体 + textArea.setLineWrap(true); // 设置默认为自动换行 + setLayout(new BorderLayout()); // 设置布局方式 + stateBar=new JLabel(" "); + stateBar.setHorizontalAlignment(SwingConstants.RIGHT); + stateBar.setBorder(BorderFactory.createEtchedBorder()); + this.add(stateBar,BorderLayout.SOUTH); + stateBar.setVisible(true); + + scrollPane=new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);// 实例化scrollPane + this.add(scrollPane, BorderLayout.CENTER); + + // 为控件添加快捷键 + create.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK)); + open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK)); + save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK)); + exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,InputEvent.CTRL_MASK)); + undo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,InputEvent.CTRL_MASK)); + cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK)); + copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK)); + paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK)); + delete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,InputEvent.CTRL_MASK)); + selectAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK)); + timeDate.setAccelerator(KeyStroke.getKeyStroke("F5")); + + // 添加响应 + create.addActionListener(this); + open.addActionListener(this); + save.addActionListener(this); + otherSave.addActionListener(this); + exit.addActionListener(this); + undo.addActionListener(this); + cut.addActionListener(this); + copy.addActionListener(this); + paste.addActionListener(this); + delete.addActionListener(this); + selectAll.addActionListener(this); + timeDate.addActionListener(this); + lineWrap.addActionListener(this); + fonts.addActionListener(this); + calc.addActionListener(this); + status.addActionListener(this); + about.addActionListener(this); + MenuMouseUndo.addActionListener(this); + MenuMouseCut.addActionListener(this); + MenuMouseCopy.addActionListener(this); + MenuMousePaste.addActionListener(this); + MenuMouseDelete.addActionListener(this); + MenuMouseselectAll.addActionListener(this); + fileChooser=new JFileChooser(); + textArea.addKeyListener(new KeyAdapter() { + public void keyTyped(KeyEvent e) { + isSaved=false; + } + }); + this.addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent arg0) { + Exit(); + } + }); + + // 弹出菜单 + + popupMenu.add(MenuMouseUndo); + popupMenu.add(MenuMouseCut); + popupMenu.add(MenuMouseCopy); + popupMenu.add(MenuMousePaste); + popupMenu.add(MenuMouseDelete); + popupMenu.add(MenuMouseselectAll); + textArea.addMouseListener(new MouseAdapter() { + public void mouseReleased(MouseEvent e) { + if (e.getButton()==MouseEvent.BUTTON3) + popupMenu.show(e.getComponent(),e.getX(),e.getY()); + } + + public void mouseClicked(MouseEvent e) { + if (e.getButton()==MouseEvent.BUTTON1) + popupMenu.setVisible(false); + } + }); + textArea.addKeyListener(new KeyAdapter() { + public void keyPressed(KeyEvent e){ + getStatus(); + } + }); + + //撤销方法 + textArea.getDocument().addUndoableEditListener( + new UndoableEditListener() { + public void undoableEditHappened(UndoableEditEvent e) { + undoManager.addEdit(e.getEdit()); + } + }); + } + +//状态栏 + private void getStatus() + { + int x=1,y=1; + int location=this.textArea.getSelectionStart(); + String[] str=this.textArea.getText().split("\n"); + for(int i=0;istr[i].length()) + { + location=location-str[i].length()-1; + x++; + } + else + { + if(y==1) + y=location+1; + else + y=location+2; + break; + } + } + stateBar.setText("第 " +x +" 行 , 第 "+y+" 列"); + } + // “新建”方法 + public void NewFile() { + if (isSaved||textArea.equals("")) { + textArea.setText(""); + setTitle("无标题"); + } + else { + int option=JOptionPane.showConfirmDialog(null,"是否将更改保存到"+"\""+this.getTitle()+"\"?","记事本",JOptionPane.INFORMATION_MESSAGE); + if (option==JOptionPane.YES_OPTION) + Save(); + else if (option==JOptionPane.NO_OPTION); + textArea.setText(""); + isSaved=true; + setTitle("无标题"); + } + } + + // “打开”方法 + public void Open() { + if (isSaved) // 文件是否为保存状态 + { + OpenFile(); // 打开 + } + else { + // 显示对话框 + int result=JOptionPane.showConfirmDialog(null,"是否将更改保存到"+"\""+this.getTitle()+"\"?","记事本",JOptionPane.INFORMATION_MESSAGE); + switch(result) { + case JOptionPane.YES_OPTION: // 确认文件保存 + Save(); // 保存文件 + break; + case JOptionPane.NO_OPTION: // 放弃文件保存 + OpenFile(); + break; + } + isSaved=true; + } + } + + public void OpenFile() { // 打开文件方法 + fileChooser=new JFileChooser(); + fileChooser.setDialogTitle("打开文件"); + try { + int result=fileChooser.showOpenDialog(this); + if (result==JFileChooser.APPROVE_OPTION) { + File file=fileChooser.getSelectedFile(); + setTitle(file.toString()); // 设置标题 + textArea.setText(""); // 将编辑框清空 + isSaved=true; // 刚打开的文件,保存状态即为“已保存” + FileInputStream fis=new FileInputStream(file); + byte dat[]=new byte[fis.available()]; + fis.read(dat); + textArea.setText(new String(dat)); + textArea.setCaretPosition(0); // 设置光标位置 + } + else if (result==JFileChooser.CANCEL_OPTION) + fileChooser.cancelSelection(); + } + catch(Exception e) { + e.printStackTrace(); + } + } + + // “保存”方法 + public void Save() { + File file=new File(getTitle()); // 从标题栏取得文件名称 + if (!file.exists()) { // 若文件不存在则执行另存为 + SaveAs(); + } else { + try { + BufferedWriter buf=new BufferedWriter(new FileWriter(file)); + buf.write(textArea.getText());// 将文字编辑区的文字写入文件 + buf.close(); + } catch(Exception e) { + } + } + } + + // “另存为”方法 + public void SaveAs() { + fileChooser.setDialogTitle("保存文件"); + int result=fileChooser.showSaveDialog(null);// 显示文件对话框 + if (result==JFileChooser.APPROVE_OPTION) // 如果确认选取文件 + { + File file=fileChooser.getSelectedFile(); + setTitle(file.toString()); // 在标题栏上设定文件名称 + try { + file.createNewFile(); // 建立文件 + Save(); // 进行文件保存 + } catch(IOException e) { + JOptionPane.showMessageDialog(null,e.toString(),"无法建立新文件",JOptionPane.ERROR_MESSAGE); + } + } + isSaved=true; + } + + // “退出”方法 + public void Exit() { + if (isSaved) { + dispose();// 释放资源,关闭程序 + } else { + int result=JOptionPane.showConfirmDialog(null,"是否将更改保存到"+"\""+this.getTitle()+"\"?","记事本",JOptionPane.INFORMATION_MESSAGE); + if (result==JOptionPane.YES_OPTION) { + Save(); + dispose(); + } else if (result==JOptionPane.NO_OPTION) + dispose(); + } + } + + // “撤销”方法 + public void Undo() { + if (undoManager.canUndo()) { + undoManager.undo(); + } + } + + // “时间/日期”添加 + public void TimeDate() { + Date time=new Date(); + SimpleDateFormat times=new SimpleDateFormat("yyyy年mm月dd日 hh:mm");// 格式化日期和时间 + textArea.insert(times.format(time),textArea.getCaretPosition()); + } + + // 关于记事本 + public void ShowAbout() { + JOptionPane.showMessageDialog(null,"这是一个简单的编辑器\n版本号:1.0\n日期:2022.06.10","关于\"记事本\"",JOptionPane.PLAIN_MESSAGE); + } + public void getCalc() + { + int low=textArea.getLineCount(); + int words=new String(textArea.getText()).length(); + JOptionPane.showMessageDialog(null,"总行数:"+low+"\n"+"总字数:"+words,"字数统计",JOptionPane.PLAIN_MESSAGE); + } + + public void actionPerformed(ActionEvent e) { + if (e.getSource()==create) + NewFile(); + else if (e.getSource()==open) + Open(); + else if (e.getSource()==save) + Save(); + else if (e.getSource()==otherSave) + SaveAs(); + else if (e.getSource()==exit) + Exit(); + else if (e.getSource()==undo) + Undo(); + else if (e.getSource()==cut) + textArea.cut(); + else if (e.getSource()==copy) + textArea.copy(); + else if (e.getSource()==paste) + textArea.paste(); + else if (e.getSource()==delete) + delete.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent event){ + int start,end; + start=textArea.getSelectionStart(); + end=textArea.getSelectionEnd(); + textArea.replaceRange("",start,end); + } + }); + else if (e.getSource()==selectAll) + textArea.selectAll(); + else if (e.getSource()==timeDate) + TimeDate(); + else if (e.getSource()==lineWrap) + textArea.setLineWrap(!textArea.getLineWrap()); + else if(e.getSource()==fonts) + { + Fonts f1=new Fonts(this); + f1.setVisible(true); + f1.setFont(); + } + else if(e.getSource()==calc) + { + getCalc(); + } + else if (e.getSource()==about) + ShowAbout(); + else if (e.getSource()==MenuMouseUndo) + Undo(); + else if (e.getSource()==MenuMouseCut) + textArea.cut(); + else if (e.getSource()==MenuMouseCopy) + textArea.copy(); + else if (e.getSource()==MenuMousePaste) + textArea.paste(); + else if (e.getSource()==MenuMouseselectAll) + textArea.selectAll(); + getStatus(); + } -public class Test { public static void main(String[] args) { - System.out.println("Hello world!"); + try { + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + } catch (Exception e) { + System.err.println("获取系统外观失败!!:" + e); + } + new Test(); } } +class Replace extends JFrame { + private static final long serialVersionUID = 1L; + JLabel label1=new JLabel("查找内容:"); + JLabel label2=new JLabel("替换为:"); + JCheckBox checkBox=new JCheckBox("区分大小写"); + JTextField textField=new JTextField(); + JTextField insteadField=new JTextField(); + JButton findButton=new JButton("查找下一个"); + JButton insteadButton=new JButton("替换"); + JButton insteadallButton=new JButton("全部替换"); + JButton cancelButton=new JButton("取消"); + String findword=new String(); + String text=new String(); + int index; + Replace(final Test my) + { + this.setLayout(null); + setTitle("替换"); + label1.setBounds(10,5,70,30); + label2.setBounds(10, 30, 70, 30); + textField.setBounds(70,10,160,20); + insteadField.setBounds(70,30,160,20); + findButton.setBounds(230, 10, 100, 20); + checkBox.setBounds(10,85,140,20); + insteadButton.setBounds(230,35,100,20); + insteadallButton.setBounds(230,60,100,20); + cancelButton.setBounds(230,85,100,20); + this.add(label1); + this.add(label2); + findButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + findword=textField.getText(); + text=my.textArea.getText(); + if(!checkBox.isSelected()) + { + text=text.toLowerCase(); + findword=findword.toLowerCase(); + } + index=text.indexOf(findword,my.textArea.getCaretPosition()); + if(index!=-1) + { + my.textArea.setCaretPosition(index+findword.length()); + my.textArea.select(index, findword.length()+index); + } + else{ + JOptionPane.showMessageDialog(Replace.this,"找不到"+" "+findword+" ","记事本",JOptionPane.WARNING_MESSAGE); + } + } + }); + this.setAlwaysOnTop(true); + this.add(findButton); + insteadButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + if(my.textArea.getSelectedText()!=null) + { + if(my.textArea.getSelectedText().equals(findword)) + { + text=my.textArea.getText(); + my.textArea.setText(text.substring(0, index)+insteadField.getText()+text.substring(index+findword.length())); + my.textArea.setCaretPosition(index+insteadField.getText().length()); + findButton.doClick(); + } + } + else{ + findButton.doClick(); + } + } + }); + this.add(insteadButton); + insteadallButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + my.textArea.setText(my.textArea.getText().replace(textField.getText(),insteadField.getText())); + } + }); + this.add(insteadallButton); + this.add(textField); + this.add(insteadField); + this.add(checkBox); + cancelButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + Replace.this.setVisible(false); + } + }); + this.add(cancelButton); + this.setSize(350,200); + this.setVisible(false); + this.setDefaultCloseOperation(HIDE_ON_CLOSE); + this.setLocationRelativeTo(null); + } + public void Show() { + this.setVisible(true); + } + public String Getfindword() { + return findword; + } +} +class Fonts extends JDialog { + private static final long serialVersionUID = 4042773383845024827L; + // 字体格式 + String style=null; + String bold=null; + String size=null; + Font font=null; + String[] str_style=GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); + String[] str_bold={"常规","倾斜","粗体","粗体 倾斜" }; + String[] str_size={"8","9","10","11","12","14","16","18","20","22","24","26","28","36","48","72"}; + public JLabel label1=new JLabel("字体(F):"); + public JList fontStyle=new JList(str_style); + public JScrollPane StyleJScrollPane=new JScrollPane(fontStyle); + public JLabel label2=new JLabel("字形(Y):"); + public JList fontBold=new JList(str_bold); + public JScrollPane BoldJScrollPane=new JScrollPane(fontBold); + public JLabel label3=new JLabel("大小(S):"); + public JTextField sizeField=new JTextField("23"); + public JList fontSize=new JList(str_size); + public JScrollPane SizeJScrollPane=new JScrollPane(fontSize); + public JLabel fontDisplay=new JLabel("AaBbYyZz"); + public JTextField textField=new JTextField(); + public JButton sureButton=new JButton("确定"); + public JButton cancelButton=new JButton("取消"); + public JButton colorButton=new JButton("改变颜色"); + public Color color; + Test my=null; + public Fonts(Test frame) + { + super(frame,"字体和颜色",true); + color=Color.black; + this.my=frame; + this.style="宋体"; + this.bold="常规"; + this.size="23"; + label1.setBounds(10,5,50,30); + StyleJScrollPane.setBounds(10,40,160,150); + fontStyle.addMouseListener(new MouseAdapter(){ + public void mousePressed(MouseEvent e) + { + JList source=(JList)e.getSource(); + Fonts.this.style=source.getSelectedValue().toString(); + Fonts.this.setFont(); + } + }); + label2.setBounds(190,5,50,30); + BoldJScrollPane.setBounds(190,40,90,150); + fontBold.addMouseListener(new MouseAdapter(){ + public void mousePressed(MouseEvent e) + { + JList source=(JList)e.getSource(); + Fonts.this.bold=source.getSelectedValue().toString(); + Fonts.this.setFont(); + } + }); + label3.setBounds(300,5,60,30); + SizeJScrollPane.setBounds(300,60,60,130); + fontSize.addMouseListener(new MouseAdapter(){ + public void mousePressed(MouseEvent e) + { + JList source=(JList)e.getSource(); + sizeField.setText(source.getSelectedValue().toString()); + Fonts.this.size=source.getSelectedValue().toString(); + Fonts.this.setFont(); + } + }); + sizeField.setBounds(300,40,60,20); + fontDisplay.setBorder(BorderFactory.createTitledBorder("示例")); + fontDisplay.setBounds(20,220,200,100); + fontDisplay.setHorizontalAlignment(JLabel.CENTER); + sureButton.setBounds(280,260,80,30); + sureButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + my.textArea.setFont(font); + my.textArea.setForeground(color); + Fonts.this.dispose(); + } + }); + cancelButton.setBounds(280,300,80,30); + cancelButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + Fonts.this.dispose(); + } + }); + + colorButton.setBounds(260,220,100,20); + this.add(colorButton); + colorButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent arg0) { + color=JColorChooser.showDialog(null,"改变字体颜色",Color.black); + } + }); + this.add(label1); + this.add(label2); + this.add(label3); + this.add(sizeField); + this.add(BoldJScrollPane); + this.add(StyleJScrollPane); + this.add(SizeJScrollPane); + this.add(fontDisplay); + this.add(sureButton); + this.add(cancelButton); + this.setLayout(null); + this.setSize(400,380); + int x=(int)my.getLocation().x+100; + int y=(int)my.getLocation().y+100; + this.setLocation(x,y); + } + + // 由选项确定font字体 + void setFont() { + int bold; + if (this.bold.equals("粗体")) + bold=Font.BOLD; + else if (this.bold.equals("常规")) + bold=Font.PLAIN; + else if (this.bold.equals("粗体 倾斜")) + bold=Font.BOLD+Font.ITALIC; + else if (this.bold.equals("倾斜")) + bold=Font.ITALIC; + else + bold=Font.PLAIN; + this.font=new Font(this.style,bold,Integer.parseInt(this.size)); + // 更改样本字体 + fontDisplay.setForeground(color); + this.fontDisplay.setFont(this.font); + + } + +} +class Find extends JFrame { + private static final long serialVersionUID=1L; + JLabel label1=new JLabel("查找内容"); + JCheckBox checkBox=new JCheckBox("区分大小写"); + JTextField textField=new JTextField(); + JButton findButton=new JButton("查找下一个"); + JButton cancelButton=new JButton("取消"); + String findword=new String(); + String text=new String(); + int index; + public Find(final Test my) + { + this.setLayout(null); + setTitle("查找"); + label1.setBounds(10,5,70,30); + textField.setBounds(70,10,160,20); + findButton.setBounds(230, 10, 100, 20); + checkBox.setBounds(10,35,140,20); + cancelButton.setBounds(230,35,100,20); + this.add(label1); + findButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + findword=textField.getText(); + text=my.textArea.getText(); + if(!checkBox.isSelected()) + { + text=text.toLowerCase(); + findword=findword.toLowerCase(); + } + index=text.indexOf(findword,my.textArea.getCaretPosition()); + if(index!=-1) + { + System.out.println(index+" "+findword.length()); + my.textArea.setCaretPosition(index+findword.length()); + my.textArea.select(index,findword.length()+index); + + } + else{ + JOptionPane.showMessageDialog(Find.this,"找不到"+" "+findword+" ","记事本",JOptionPane.WARNING_MESSAGE); + } + } + }); + this.setAlwaysOnTop(true); + this.add(findButton); + this.add(textField); + this.add(checkBox); + cancelButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + Find.this.setVisible(false); + } + }); + this.add(cancelButton); + this.setSize(350,100); + this.setVisible(false); + this.setDefaultCloseOperation(HIDE_ON_CLOSE); + this.setLocationRelativeTo(null); + } + public void Show() { + this.setVisible(true); + } + public String Getfindword() { + return findword; + } +} \ No newline at end of file