【校招VIP】用Java编写记事本

06月26日 收藏 0 评论 0 java开发

【校招VIP】用Java编写记事本

转载声明:原文链接:https://blog.csdn.net/qq_63157065/article/details/124138419

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

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() {//当选择编辑时判断可用性
@Override
public void menuSelected(MenuEvent e) {
checkMenuItemEnabled();
}

@Override
public void menuDeselected(MenuEvent e) {
checkMenuItemEnabled();
}

@Override
public 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() {
@Override
public 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);
else
k = 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() {
@Override
public 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());
else
k=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());

}else
k = 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);
}
}
}


C 0条回复 评论

帖子还没人回复快来抢沙发