⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 finddialog.java~43~

📁 我自己用java写的记事本
💻 JAVA~43~
字号:
package texteditor;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * <p>Title: 文本编辑器</p>
 * <p>Description: 本软件为面向对象程序设计期末作业,不得擅自修改</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: 边缘开发室</p>
 * @author 第三组 ,组长:谭XX
 * @version 1.0
 */

public class FindDialog
    extends JDialog
{
  JPanel panel1 = new JPanel();
  JLabel jLabel1 = new JLabel();
  JTextField findStringText = new JTextField();
  JButton jButton1 = new JButton();
  JButton jButton2 = new JButton();
  JButton jButton3 = new JButton();
  static int index = -1; //查找到的位置
  String subString; //要查找的字符串
  String content = Application1.frame.contentText.getText(); //文本区的字符

  public FindDialog(Frame frame, String title, boolean modal)
  {
    super(frame, title, modal);
    try
    {
      jbInit();
      //pack();
    }
    catch (Exception ex)
    {
      ex.printStackTrace();
    }
  }

  public FindDialog()
  {
    this(null, "", false);
  }

  private void jbInit() throws Exception
  {
    this.setSize(new Dimension(327, 163));
    //Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    //int h = this.getHeight();
    int w = this.getWidth();
    //this.setLocation( (d.width - w) / 2, (d.height - h) / 2);
    //将窗口定位到主窗口同高,而在宽方向在主窗口的中央
    this.setLocation(Application1.frame.getX() +
                     (Application1.frame.getWidth() - w) / 2,
                     Application1.frame.getY());

    panel1.setLayout(null);
    jLabel1.setText("输入字符串");
    jLabel1.setBounds(new Rectangle(17, 19, 79, 33));
    findStringText.setText("");
    findStringText.setBounds(new Rectangle(101, 24, 186, 23));
    jButton1.setBounds(new Rectangle(16, 80, 79, 28));
    jButton1.setText("查找");
    jButton1.addActionListener(new FindDialog_jButton1_actionAdapter(this));
    jButton2.setBounds(new Rectangle(207, 80, 79, 28));
    jButton2.setText("取消");
    jButton2.addActionListener(new FindDialog_jButton2_actionAdapter(this));
    jButton3.setBounds(new Rectangle(111, 80, 79, 28));
    jButton3.setText("下一个");
    jButton3.addActionListener(new FindDialog_jButton3_actionAdapter(this));
    getContentPane().add(panel1);
    panel1.add(findStringText, null);
    panel1.add(jButton1, null);
    panel1.add(jButton2, null);
    panel1.add(jLabel1, null);
    panel1.add(jButton3, null);
  }

  void jButton2_actionPerformed(ActionEvent e)
  {
    dispose();
  }

  void jButton1_actionPerformed(ActionEvent e)
  {
    findStringHandle();
  }

  //查找字符串方法
  void findStringHandle()
  {
    subString = findStringText.getText().trim(); //获得要查找的字符串
    index = content.indexOf(subString);
    /*    Application1.frame.contentText.setSelectionStart(index);
         Application1.frame.contentText.setSelectionEnd(index + subString.length());*/
    setStringIsSelection(); //调用查找到的字体为选择状态方法
  }

  //将查找到的字符串设置为选择状态
  private void setStringIsSelection()
  {

    if (index != -1)
    {
      Application1.frame.contentText.setSelectionStart(index);
      Application1.frame.contentText.setSelectionEnd(index + subString.length());
    }
    else
    {
      Toolkit.getDefaultToolkit().beep();
    }
    //index = -1; //重新设置index
  }

  //查找下一个
  void jButton3_actionPerformed(ActionEvent e)
  {
        subString = findStringText.getText().trim(); //获得要查找的字符串
         index = content.indexOf(subString);//查找
         if (index != -1)
         {
      String str = content.substring(index+1);
      index = str.indexOf(subString);
      setStringIsSelection();
         }
         else
         {
      return;
         }
  }
}

class FindDialog_jButton2_actionAdapter
    implements java.awt.event.ActionListener
{
  FindDialog adaptee;

  FindDialog_jButton2_actionAdapter(FindDialog adaptee)
  {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e)
  {
    adaptee.jButton2_actionPerformed(e);
  }
}

class FindDialog_jButton1_actionAdapter
    implements java.awt.event.ActionListener
{
  FindDialog adaptee;

  FindDialog_jButton1_actionAdapter(FindDialog adaptee)
  {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e)
  {
    adaptee.jButton1_actionPerformed(e);
  }
}

class FindDialog_jButton3_actionAdapter
    implements java.awt.event.ActionListener
{
  FindDialog adaptee;

  FindDialog_jButton3_actionAdapter(FindDialog adaptee)
  {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e)
  {
    adaptee.jButton3_actionPerformed(e);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -