package demo2;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.sql.Date;
import java.text.SimpleDateFormat;
import javax.management.RuntimeErrorException;
import javax.swing.BorderFactory;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;
import javax.xml.crypto.Data;
public class Menu1 extends JFrame
{
private JFrame f,d;
private JMenu m1,bianji,geshi,chakan,bangzhu;//菜单
private JMenuBar mb; //可添加菜单栏
private JMenuItem tm1,tm2,tm3,xingjian,yemianshezhi,dayin,shijian,quanxuan,lingcunwei,tihuan;//条目
private JMenuItem chexiao,jianqie,fuzhi,zhantie,chazhao,shanchu,ziti,zhuangtailan,chakanbangzhu,guanyujishiben;
private JCheckBoxMenuItem zidonghuanhang;
private File file;
private String jianqieban=null;
private Label la;
private Button okButton = new Button("ok");
private UndoManager undo;
private JScrollPane jsp;
private int s;
FileDialog op;
FileDialog sav;
private JTextArea ta;
Menu1(){
init();
}
public void init(){
f=new JFrame("杨氏笔记本");
f.setBounds(250, 60, 1068, 590);
d = new JFrame("提示");
d.setBounds(600,300,180,120);
d.setLayout(new FlowLayout());
la = new Label();
d.add(la);
okButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
d.setVisible(false);
}
});
d.add(okButton);
mb=new JMenuBar();
bianji = new JMenu("编辑");
m1=new JMenu("文件");
tm3=new JMenuItem("保存 Ctr+s");
tm1=new JMenuItem("退出 ");
tm2=new JMenuItem("打开 ");
geshi = new JMenu("格式 ");
lingcunwei =new JMenuItem("另存为");
xingjian = new JMenuItem("新建");
yemianshezhi = new JMenuItem("页面设置");
dayin = new JMenuItem("打印");
shijian = new JMenuItem("时间");
quanxuan = new JMenuItem("全选");
ziti = new JMenuItem("字体");
tihuan = new JMenuItem("替换");
zidonghuanhang = new JCheckBoxMenuItem("自动换行");
ta=new JTextArea();
jsp =new JScrollPane(ta);
op=new FileDialog(f, "我要打开", FileDialog.LOAD);
sav=new FileDialog(f, "我要保存", FileDialog.SAVE);
m1.add(xingjian);
m1.add(tm2);
m1.add(tm3);
geshi.add(zidonghuanhang);
geshi.add(ziti);
m1.add(lingcunwei);
chexiao = new JMenuItem("撤销 Ctr+z");
bianji.add(chexiao);
shanchu = new JMenuItem("删除 Ctr+x");
bianji.add(shanchu);
fuzhi = new JMenuItem("复制 Ctr+c");
bianji.add(fuzhi);
zhantie = new JMenuItem("粘贴 Ctr+v");
bianji.add(zhantie);
jianqie = new JMenuItem("剪切");
bianji.add(jianqie);
chazhao = new JMenuItem("查找");
bianji.add(chazhao);
bianji.add(tihuan);
bianji.add(quanxuan);
bianji.add(shijian);
chakan = new JMenu("查看");
zhuangtailan = new JMenuItem("状态栏");
zhuangtailan.setEnabled(false);
chakan.add(zhuangtailan);
bangzhu = new JMenu("帮助");
chakanbangzhu = new JMenuItem("查看帮助");
guanyujishiben = new JMenuItem("关于记事本");
bangzhu.add(chakanbangzhu);
bangzhu.add(guanyujishiben);
mb.add(m1);
mb.add(bianji);
mb.add(geshi);
mb.add(chakan);
mb.add(bangzhu);
m1.add(yemianshezhi);
m1.add(dayin);
m1.add(tm1);
undo = new UndoManager();
ta.getDocument().addUndoableEditListener(new UndoableEditListener() {
@Override
public void undoableEditHappened(UndoableEditEvent e) {
// TODO 自动生成的方法存根
undo.addEdit(e.getEdit());
}
});
f.setJMenuBar(mb);
ta.setFont(new Font("宋体",Font.BOLD,32));
f.add(jsp);
Event();
f.setVisible(true);
}
public void Event(){
//添加替换
tihuan.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
replace();
}
});
//添加帮助条中的查看帮助
chakanbangzhu.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
JOptionPane.showMessageDialog(null,"尽管玩,玩坏了找小强","提示",JOptionPane.INFORMATION_MESSAGE);
}
});
guanyujishiben.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
JOptionPane.showMessageDialog(f, "******************************************************\n"
+"******************************************************\n"
+"** \n"
+"** 制作人:杨强 \n"
+"** 特别致谢:网上各大神 \n"
+"** 温馨提示:以上各代码由小强独立打出 \n"
+"** 如有雷同纯属巧合 \n"
+"** 谢谢您的使用 \n"
+"** 2017.2.28 \n"
+"******************************************************\n"
+"******************************************************\n"
,"提示",JOptionPane.INFORMATION_MESSAGE);
}
});
//添加字体事件
ziti.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
font();
}
});
//添加查找事件
chazhao.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
find();
}
});
//添加撤销事件
chexiao.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
//String ss = ta.getSelectedText();
if(undo.canUndo())
{ try
{
undo.undo();
}
catch (CannotUndoException ex)
{
System.out.println("Unable to undo:" + ex);
}
}
}
});
//添加删除事件
shanchu.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
ta.replaceRange("", ta.getSelectionStart(), ta.getSelectionEnd());
}
});
//添加剪切事件
jianqie.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
jianqieban = ta.getSelectedText();
ta.replaceRange("", ta.getSelectionStart(), ta.getSelectionEnd());
}
});
//添加复制事件
fuzhi.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
jianqieban = ta.getSelectedText();
}
});
//添加粘贴事件
zhantie.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
// ta.insert(jianqieban, ta.getCaretPosition());
ta.replaceRange(jianqieban,ta.getSelectionStart(),ta.getSelectionEnd());
}
});
//添加自动换行事件
zidonghuanhang.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
if(zidonghuanhang.getState()){
ta.setLineWrap(true);
}
else{
ta.setLineWrap(false);
}
}
});
//退出按钮
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){String ss;
if((ss= ta.getText()).equals("")){
System.exit(0);
}
else{
Button baocuo = new Button("保存");
Button tuichu = new Button("退出");
Button quxiao = new Button("取消");
Label laa = new Label("是否保存文件?");
Dialog frr = new Dialog(f,"提示");
frr.setBounds(600,300,148,108);
frr.setLayout(new FlowLayout());
frr.add(laa);
frr.add(baocuo);
frr.add(quxiao);
frr.add(tuichu);
frr.setVisible(true);
frr.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
frr.setVisible(false);
}
});
quxiao.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
frr.setVisible(false);
}
});
tuichu.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
System.exit(0);
}
});
baocuo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
if(file==null)
{
sav.setVisible(true);
String dirpath=sav.getDirectory();
String filename=sav.getFile();
if(dirpath==null || filename == null)return;
file=new File(dirpath,filename);
}
try{
BufferedWriter bufw=new BufferedWriter(new FileWriter(file));
String text=ta.getText();
bufw.write(text);
bufw.flush();
bufw.close();
}
catch(Exception ex){
throw new RuntimeException();
}
// TODO 自动生成的方法存根
}
});
}
}
});
//添加提示框的关闭事件
d.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
d.setVisible(false);
}
});
//添加另存为事件
lingcunwei.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
String s ;
if(!(s = ta.getText()).equals(""))
{
sav.setVisible(true);
String dirpath=sav.getDirectory();
String filename=sav.getFile();
if(dirpath==null || filename == null)return;
file=new File(dirpath,filename);
try{
BufferedWriter bufw=new BufferedWriter(new FileWriter(file));
String text=ta.getText();
bufw.write(text);
bufw.flush();
bufw.close();
}
catch(Exception ex){
throw new RuntimeException();
}
}
else{
la.setText("文件为空,别想搞事情");
d.setVisible(true);
}
}
});
//添加结束程序事件
tm1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
System.exit(0);
}
});
//添加编辑中的全选事件
quanxuan.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
ta.selectAll();
}
});
//添加文件中的保存文件
tm3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(file==null)
{
sav.setVisible(true);
String dirpath=sav.getDirectory();
String filename=sav.getFile();
if(dirpath==null || filename == null)return;
file=new File(dirpath,filename);
}
try{
BufferedWriter bufw=new BufferedWriter(new FileWriter(file));
String text=ta.getText();
bufw.write(text);
bufw.flush();
bufw.close();
}
catch(Exception ex){
throw new RuntimeException();
}
// TODO 自动生成的方法存根
}
});
//添加文件条中的打开文件
tm2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
op.setVisible(true);
String dirpath=op.getDirectory();
String filename=op.getFile();
//System.out.println(dirpath);
if(dirpath==null || filename == null)return;
ta.setText("");
file=new File(dirpath,filename);
try{
BufferedReader bufr=new BufferedReader(new FileReader(file));
String line=null;
while((line = bufr.readLine())!=null)
{
ta.append(line+"\r\n");
}
bufr.close();
}
catch(Exception e1){
throw new RuntimeException("读取失败");
}
}
});
//添加文件条中的页面设置文件
yemianshezhi.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
//Frame fr = new Frame("提示");
la.setText("版本太低,你想多了!!");
d.setVisible(true);
}
});
//添加文件条中的打印事件
dayin.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
la.setText("版本太低,你想多了!!");
d.setVisible(true);
}
});
//添加文件条中的新建事件
xingjian.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
if(ta.getText().equals("")){
la.setText("文本框为空,无法新建");
d.setVisible(true);
}
else{
ta.setText("");
}
}
});
//添加编辑中的显示时间时间
shijian.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
java.util.Date date = new java.util.Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM dd hh:mm:ss");
String time = sdf.format(date)+"\r\n";
ta.append(time);
}
});
}
public void find(){
JDialog jfr = new JDialog(f,"查找页面");
jfr.setBounds(400,200,410, 180);
JButton bu1 = new JButton("查找");
JButton bu3 =new JButton("取消");
bu3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
jfr.setVisible(false);
}
});
JButton bu2 = new JButton("查找下一个");
JTextField jt = new JTextField(20);
JLabel jl = new JLabel("请输入查找数据:");
jfr.setLayout(new FlowLayout());
jfr.add(jl);
jfr.add(jt);
jfr.add(bu1);
jfr.add(bu2);
jfr.add(bu3);
bu2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
String str1 = ta.getText();
String str2 = jt.getText();
if(ta.getSelectedText() == null)
{
s = str1.indexOf(str2,ta.getCaretPosition()+1);
}
else
s = str1.indexOf(str2, ta.getCaretPosition()-str2.length()+1);
if(s>-1)
{
ta.setCaretPosition(s);
ta.select(s, s+str2.length());
}
else
{
JOptionPane.showMessageDialog(null,"找不到您查找的内容!","查找",JOptionPane.INFORMATION_MESSAGE);
}
}
});
bu1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
String str1 = ta.getText();
String str2 = jt.getText();
if(ta.getSelectedText() == null)
{
s = str1.indexOf(str2,ta.getCaretPosition()+1);
}
else
s = str1.indexOf(str2, ta.getCaretPosition()-str2.length()+1);
if(s>-1)
{
ta.setCaretPosition(s);
ta.select(s, s+str2.length());
}
else
{
JOptionPane.showMessageDialog(null,"找不到您查找的内容!","查找",JOptionPane.INFORMATION_MESSAGE);
}
}
});
jfr.setVisible(true);
}
public void font(){
JDialog jdr = new JDialog(f, "字体设置");
//定义对话框按照流失布局,向左对齐
jdr.setLayout(new FlowLayout(FlowLayout.LEFT));
//设置字体,字形(风格),字的大小三个文本条
JTextField ziti = new JTextField(17);
JTextField zixing = new JTextField(4);
JTextField daxiao = new JTextField(5);
//字的风格虽然是中文但是计算机值认识(font。……)型,他们代表的都是int型,设置font时只需要将style【i】传进去即可
int style[] = {Font.PLAIN,Font.BOLD,Font.ITALIC,Font.BOLD+Font.ITALIC};
JButton okbu = new JButton("确定");
JButton cancel = new JButton("取消");
cancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
jdr.dispose();
}
});
//找出当前文件格式
Font currentfont = ta.getFont();
ziti.setText(currentfont.getFontName());
ziti.selectAll();
//判断字体风格中的int转换成与之对应的string并写到文本条上
if(currentfont.getStyle()==Font.PLAIN)
{
zixing.setText("常规");
}
else if (currentfont.getStyle() == Font.BOLD)
{
zixing.setText("粗体");
}
else if (currentfont.getStyle() == Font.ITALIC)
{
zixing.setText("斜体");
}
else if (currentfont.getStyle() == Font.BOLD+Font.ITALIC)
{
zixing.setText("粗斜体");
}
zixing.selectAll();
String strr = String.valueOf(currentfont.getSize());
daxiao.setText(strr);
daxiao.selectAll();
//定义可选择列表
JList zixinglist,zitilist,daxiaolist;
//获得各种字体
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
String fontName[]=ge.getAvailableFontFamilyNames();
zitilist = new JList(fontName);
zitilist.setSize(70, 20);
zitilist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//定义一次的索引个数
String zixingstyle[] = {"常规","粗体","斜体","粗斜体"};
zixinglist = new JList(zixingstyle);
zixinglist.setSize(60, 20);
zixinglist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//定义一次的索引个数
String fontSize[]={"8","9","10","11","12","14","16","18","20","22","24","26","28","36","48","72"};
daxiaolist = new JList(fontSize);
daxiaolist.setSize(60, 20);
daxiaolist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//定义一次的索引个数
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
jp1.add(ziti);
jp1.add(zixing);
jp1.add(daxiao);
jp1.add(okbu);
jp2.add(new JScrollPane(zitilist));
jp2.add(new JScrollPane(zixinglist));
jp2.add(new JScrollPane(daxiaolist));
jp2.add(cancel);
jdr.add(jp1);
jdr.add(jp2);
JLabel simple = new JLabel("杨氏记事本-HAha");
JPanel simplepane = new JPanel();
simple.setBorder(BorderFactory.createTitledBorder("像我这样"));
simplepane.add(simple);
jdr.add(simplepane);
//初始化选中相应的风格,免得之后如果不点风格就会报错
if(currentfont.getStyle()==Font.PLAIN)
zixinglist.setSelectedIndex(0);
else if(currentfont.getStyle()==Font.BOLD)
zixinglist.setSelectedIndex(1);
else if(currentfont.getStyle()==Font.ITALIC)
zixinglist.setSelectedIndex(2);//获得列表中的位置
else if(currentfont.getStyle()==(Font.BOLD+Font.ITALIC))
zixinglist.setSelectedIndex(3);
zitilist.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
// TODO 自动生成的方法存根
ziti.setText(fontName[zitilist.getSelectedIndex()]);
ziti.selectAll();
Font font1 = new Font(ziti.getText(), style[zixinglist.getSelectedIndex()], Integer.parseInt(daxiao.getText()));
//font (string,int,int);
//字型为int型,计算机不识别粗体这种人类语言
simple.setFont(font1);
}
});
okbu.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
Font font1 = new Font(ziti.getText(), style[zixinglist.getSelectedIndex()], Integer.parseInt(daxiao.getText()));
ta.setFont(font1);
jdr.dispose();
}
});
zixinglist.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
// TODO 自动生成的方法存根
zixing.setText(zixingstyle[zixinglist.getSelectedIndex()]);
zixing.selectAll();
Font font1 = new Font(ziti.getText(), style[zixinglist.getSelectedIndex()], Integer.parseInt(daxiao.getText()));
simple.setFont(font1);
}
});
daxiaolist.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
// TODO 自动生成的方法存根
daxiao.setText(fontSize[daxiaolist.getSelectedIndex()]);
daxiao.selectAll();
Font font1 = new Font(ziti.getText(), style[zixinglist.getSelectedIndex()], Integer.parseInt(daxiao.getText()));
simple.setFont(font1);
}
});
// jdr.add(jp3);
jdr.setBounds(200, 200, 400, 360);
// jdr.setResizable(false);
jdr.setVisible(true);
}
//添加替换的方法
public void replace(){
JDialog jfr = new JDialog(f,"查找页面");
jfr.setBounds(400,200,508, 180);
JButton bu1 = new JButton("替换");
JButton bu4 = new JButton("全部替换");
JButton bu5 = new JButton("确定");
JButton bu3 =new JButton("取消");
bu3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
jfr.setVisible(false);
}
});
JButton bu2 = new JButton("查找下一个");
JTextField jt = new JTextField(20);
JTextField jt2 = new JTextField(20);
JLabel jl = new JLabel("请输入要替换数据:");
JLabel j2 = new JLabel("请输入替换后数据:");
jfr.setLayout(new FlowLayout());
jfr.add(jl);
jfr.add(jt);
jfr.add(bu2);
jfr.add(j2);
jfr.add(jt2);
jfr.add(bu1);
jfr.add(bu4);
jfr.add(bu5);
bu5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
jfr.dispose();
}
});
jfr.add(bu3);
//利用while语句给循环查找与替换话的过程
//通过找到和需要查找一样的字符串的index,通过操作index达到替换的效果
bu4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
int flag = 0;
String str1 = ta.getText();
String str2 = jt.getText();
String str3 = jt2.getText();
if(ta.getSelectedText() == null)
{
s = str1.indexOf(str2,ta.getCaretPosition()+1);
}
else
s = str1.indexOf(str2, ta.getCaretPosition()-str2.length()+1);
while(s>-1){
if(s>-1)
{
ta.select(s, s+str2.length());
ta.replaceRange(str3, ta.getSelectionStart(), ta.getSelectionEnd());
flag++;
}
if(ta.getSelectedText() == null)
{
s = str1.indexOf(str2,ta.getCaretPosition()+1);
}
else
s = str1.indexOf(str2, ta.getCaretPosition()-str2.length()+1);
}
if(s<=-1 && flag==0)
{
JOptionPane.showMessageDialog(null,"找不到您替换的内容!","替换",JOptionPane.INFORMATION_MESSAGE);
}
else{
JOptionPane.showMessageDialog(null,"替换成功!","替换",JOptionPane.INFORMATION_MESSAGE);
}
}
});
bu2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
String str1 = ta.getText();
String str2 = jt.getText();
if(ta.getSelectedText() == null)
{
s = str1.indexOf(str2,ta.getCaretPosition()+1);
}
else
s = str1.indexOf(str2, ta.getCaretPosition()-str2.length()+1);
if(s>-1)
{
ta.setCaretPosition(s);
ta.select(s, s+str2.length());
}
else
{
JOptionPane.showMessageDialog(null,"找不到您查找的内容!","查找",JOptionPane.INFORMATION_MESSAGE);
}
}
});
bu1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
String str3 = jt2.getText();
if(ta.getSelectedText()==null){
JOptionPane.showMessageDialog(null,"找不到您要替换的内容!","替换",JOptionPane.INFORMATION_MESSAGE);
}
else
{
ta.replaceRange(str3, ta.getSelectionStart(), ta.getSelectionEnd());
JOptionPane.showMessageDialog(null,"替换成功!","替换",JOptionPane.INFORMATION_MESSAGE);
}
}
});
jfr.setVisible(true);
}
public static void main(String[] args){
new Menu1();
}
}
package demo2;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.sql.Date;
import java.text.SimpleDateFormat;
import javax.management.RuntimeErrorException;
import javax.swing.BorderFactory;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.UndoableEditEvent;
import javax.swing.event.UndoableEditListener;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;
import javax.xml.crypto.Data;
public class Menu1 extends JFrame
{
private JFrame f,d;
private JMenu m1,bianji,geshi,chakan,bangzhu;//菜单
private JMenuBar mb; //可添加菜单栏
private JMenuItem tm1,tm2,tm3,xingjian,yemianshezhi,dayin,shijian,quanxuan,lingcunwei,tihuan;//条目
private JMenuItem chexiao,jianqie,fuzhi,zhantie,chazhao,shanchu,ziti,zhuangtailan,chakanbangzhu,guanyujishiben;
private JCheckBoxMenuItem zidonghuanhang;
private File file;
private String jianqieban=null;
private Label la;
private Button okButton = new Button("ok");
private UndoManager undo;
private JScrollPane jsp;
private int s;
FileDialog op;
FileDialog sav;
private JTextArea ta;
Menu1(){
init();
}
public void init(){
f=new JFrame("杨氏笔记本");
f.setBounds(250, 60, 1068, 590);
d = new JFrame("提示");
d.setBounds(600,300,180,120);
d.setLayout(new FlowLayout());
la = new Label();
d.add(la);
okButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
d.setVisible(false);
}
});
d.add(okButton);
mb=new JMenuBar();
bianji = new JMenu("编辑");
m1=new JMenu("文件");
tm3=new JMenuItem("保存 Ctr+s");
tm1=new JMenuItem("退出 ");
tm2=new JMenuItem("打开 ");
geshi = new JMenu("格式 ");
lingcunwei =new JMenuItem("另存为");
xingjian = new JMenuItem("新建");
yemianshezhi = new JMenuItem("页面设置");
dayin = new JMenuItem("打印");
shijian = new JMenuItem("时间");
quanxuan = new JMenuItem("全选");
ziti = new JMenuItem("字体");
tihuan = new JMenuItem("替换");
zidonghuanhang = new JCheckBoxMenuItem("自动换行");
ta=new JTextArea();
jsp =new JScrollPane(ta);
op=new FileDialog(f, "我要打开", FileDialog.LOAD);
sav=new FileDialog(f, "我要保存", FileDialog.SAVE);
m1.add(xingjian);
m1.add(tm2);
m1.add(tm3);
geshi.add(zidonghuanhang);
geshi.add(ziti);
m1.add(lingcunwei);
chexiao = new JMenuItem("撤销 Ctr+z");
bianji.add(chexiao);
shanchu = new JMenuItem("删除 Ctr+x");
bianji.add(shanchu);
fuzhi = new JMenuItem("复制 Ctr+c");
bianji.add(fuzhi);
zhantie = new JMenuItem("粘贴 Ctr+v");
bianji.add(zhantie);
jianqie = new JMenuItem("剪切");
bianji.add(jianqie);
chazhao = new JMenuItem("查找");
bianji.add(chazhao);
bianji.add(tihuan);
bianji.add(quanxuan);
bianji.add(shijian);
chakan = new JMenu("查看");
zhuangtailan = new JMenuItem("状态栏");
zhuangtailan.setEnabled(false);
chakan.add(zhuangtailan);
bangzhu = new JMenu("帮助");
chakanbangzhu = new JMenuItem("查看帮助");
guanyujishiben = new JMenuItem("关于记事本");
bangzhu.add(chakanbangzhu);
bangzhu.add(guanyujishiben);
mb.add(m1);
mb.add(bianji);
mb.add(geshi);
mb.add(chakan);
mb.add(bangzhu);
m1.add(yemianshezhi);
m1.add(dayin);
m1.add(tm1);
undo = new UndoManager();
ta.getDocument().addUndoableEditListener(new UndoableEditListener() {
@Override
public void undoableEditHappened(UndoableEditEvent e) {
// TODO 自动生成的方法存根
undo.addEdit(e.getEdit());
}
});
f.setJMenuBar(mb);
ta.setFont(new Font("宋体",Font.BOLD,32));
f.add(jsp);
Event();
f.setVisible(true);
}
public void Event(){
//添加替换
tihuan.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
replace();
}
});
//添加帮助条中的查看帮助
chakanbangzhu.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
JOptionPane.showMessageDialog(null,"尽管玩,玩坏了找小强","提示",JOptionPane.INFORMATION_MESSAGE);
}
});
guanyujishiben.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
JOptionPane.showMessageDialog(f, "******************************************************\n"
+"******************************************************\n"
+"** \n"
+"** 制作人:杨强 \n"
+"** 特别致谢:网上各大神 \n"
+"** 温馨提示:以上各代码由小强独立打出 \n"
+"** 如有雷同纯属巧合 \n"
+"** 谢谢您的使用 \n"
+"** 2017.2.28 \n"
+"******************************************************\n"
+"******************************************************\n"
,"提示",JOptionPane.INFORMATION_MESSAGE);
}
});
//添加字体事件
ziti.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
font();
}
});
//添加查找事件
chazhao.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
find();
}
});
//添加撤销事件
chexiao.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
//String ss = ta.getSelectedText();
if(undo.canUndo())
{ try
{
undo.undo();
}
catch (CannotUndoException ex)
{
System.out.println("Unable to undo:" + ex);
}
}
}
});
//添加删除事件
shanchu.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
ta.replaceRange("", ta.getSelectionStart(), ta.getSelectionEnd());
}
});
//添加剪切事件
jianqie.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
jianqieban = ta.getSelectedText();
ta.replaceRange("", ta.getSelectionStart(), ta.getSelectionEnd());
}
});
//添加复制事件
fuzhi.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
jianqieban = ta.getSelectedText();
}
});
//添加粘贴事件
zhantie.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
// ta.insert(jianqieban, ta.getCaretPosition());
ta.replaceRange(jianqieban,ta.getSelectionStart(),ta.getSelectionEnd());
}
});
//添加自动换行事件
zidonghuanhang.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
if(zidonghuanhang.getState()){
ta.setLineWrap(true);
}
else{
ta.setLineWrap(false);
}
}
});
//退出按钮
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){String ss;
if((ss= ta.getText()).equals("")){
System.exit(0);
}
else{
Button baocuo = new Button("保存");
Button tuichu = new Button("退出");
Button quxiao = new Button("取消");
Label laa = new Label("是否保存文件?");
Dialog frr = new Dialog(f,"提示");
frr.setBounds(600,300,148,108);
frr.setLayout(new FlowLayout());
frr.add(laa);
frr.add(baocuo);
frr.add(quxiao);
frr.add(tuichu);
frr.setVisible(true);
frr.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
frr.setVisible(false);
}
});
quxiao.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
frr.setVisible(false);
}
});
tuichu.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
System.exit(0);
}
});
baocuo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
if(file==null)
{
sav.setVisible(true);
String dirpath=sav.getDirectory();
String filename=sav.getFile();
if(dirpath==null || filename == null)return;
file=new File(dirpath,filename);
}
try{
BufferedWriter bufw=new BufferedWriter(new FileWriter(file));
String text=ta.getText();
bufw.write(text);
bufw.flush();
bufw.close();
}
catch(Exception ex){
throw new RuntimeException();
}
// TODO 自动生成的方法存根
}
});
}
}
});
//添加提示框的关闭事件
d.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
d.setVisible(false);
}
});
//添加另存为事件
lingcunwei.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
String s ;
if(!(s = ta.getText()).equals(""))
{
sav.setVisible(true);
String dirpath=sav.getDirectory();
String filename=sav.getFile();
if(dirpath==null || filename == null)return;
file=new File(dirpath,filename);
try{
BufferedWriter bufw=new BufferedWriter(new FileWriter(file));
String text=ta.getText();
bufw.write(text);
bufw.flush();
bufw.close();
}
catch(Exception ex){
throw new RuntimeException();
}
}
else{
la.setText("文件为空,别想搞事情");
d.setVisible(true);
}
}
});
//添加结束程序事件
tm1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
System.exit(0);
}
});
//添加编辑中的全选事件
quanxuan.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
ta.selectAll();
}
});
//添加文件中的保存文件
tm3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(file==null)
{
sav.setVisible(true);
String dirpath=sav.getDirectory();
String filename=sav.getFile();
if(dirpath==null || filename == null)return;
file=new File(dirpath,filename);
}
try{
BufferedWriter bufw=new BufferedWriter(new FileWriter(file));
String text=ta.getText();
bufw.write(text);
bufw.flush();
bufw.close();
}
catch(Exception ex){
throw new RuntimeException();
}
// TODO 自动生成的方法存根
}
});
//添加文件条中的打开文件
tm2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
op.setVisible(true);
String dirpath=op.getDirectory();
String filename=op.getFile();
//System.out.println(dirpath);
if(dirpath==null || filename == null)return;
ta.setText("");
file=new File(dirpath,filename);
try{
BufferedReader bufr=new BufferedReader(new FileReader(file));
String line=null;
while((line = bufr.readLine())!=null)
{
ta.append(line+"\r\n");
}
bufr.close();
}
catch(Exception e1){
throw new RuntimeException("读取失败");
}
}
});
//添加文件条中的页面设置文件
yemianshezhi.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
//Frame fr = new Frame("提示");
la.setText("版本太低,你想多了!!");
d.setVisible(true);
}
});
//添加文件条中的打印事件
dayin.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
la.setText("版本太低,你想多了!!");
d.setVisible(true);
}
});
//添加文件条中的新建事件
xingjian.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
if(ta.getText().equals("")){
la.setText("文本框为空,无法新建");
d.setVisible(true);
}
else{
ta.setText("");
}
}
});
//添加编辑中的显示时间时间
shijian.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
java.util.Date date = new java.util.Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM dd hh:mm:ss");
String time = sdf.format(date)+"\r\n";
ta.append(time);
}
});
}
public void find(){
JDialog jfr = new JDialog(f,"查找页面");
jfr.setBounds(400,200,410, 180);
JButton bu1 = new JButton("查找");
JButton bu3 =new JButton("取消");
bu3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
jfr.setVisible(false);
}
});
JButton bu2 = new JButton("查找下一个");
JTextField jt = new JTextField(20);
JLabel jl = new JLabel("请输入查找数据:");
jfr.setLayout(new FlowLayout());
jfr.add(jl);
jfr.add(jt);
jfr.add(bu1);
jfr.add(bu2);
jfr.add(bu3);
bu2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
String str1 = ta.getText();
String str2 = jt.getText();
if(ta.getSelectedText() == null)
{
s = str1.indexOf(str2,ta.getCaretPosition()+1);
}
else
s = str1.indexOf(str2, ta.getCaretPosition()-str2.length()+1);
if(s>-1)
{
ta.setCaretPosition(s);
ta.select(s, s+str2.length());
}
else
{
JOptionPane.showMessageDialog(null,"找不到您查找的内容!","查找",JOptionPane.INFORMATION_MESSAGE);
}
}
});
bu1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
String str1 = ta.getText();
String str2 = jt.getText();
if(ta.getSelectedText() == null)
{
s = str1.indexOf(str2,ta.getCaretPosition()+1);
}
else
s = str1.indexOf(str2, ta.getCaretPosition()-str2.length()+1);
if(s>-1)
{
ta.setCaretPosition(s);
ta.select(s, s+str2.length());
}
else
{
JOptionPane.showMessageDialog(null,"找不到您查找的内容!","查找",JOptionPane.INFORMATION_MESSAGE);
}
}
});
jfr.setVisible(true);
}
public void font(){
JDialog jdr = new JDialog(f, "字体设置");
//定义对话框按照流失布局,向左对齐
jdr.setLayout(new FlowLayout(FlowLayout.LEFT));
//设置字体,字形(风格),字的大小三个文本条
JTextField ziti = new JTextField(17);
JTextField zixing = new JTextField(4);
JTextField daxiao = new JTextField(5);
//字的风格虽然是中文但是计算机值认识(font。……)型,他们代表的都是int型,设置font时只需要将style【i】传进去即可
int style[] = {Font.PLAIN,Font.BOLD,Font.ITALIC,Font.BOLD+Font.ITALIC};
JButton okbu = new JButton("确定");
JButton cancel = new JButton("取消");
cancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
jdr.dispose();
}
});
//找出当前文件格式
Font currentfont = ta.getFont();
ziti.setText(currentfont.getFontName());
ziti.selectAll();
//判断字体风格中的int转换成与之对应的string并写到文本条上
if(currentfont.getStyle()==Font.PLAIN)
{
zixing.setText("常规");
}
else if (currentfont.getStyle() == Font.BOLD)
{
zixing.setText("粗体");
}
else if (currentfont.getStyle() == Font.ITALIC)
{
zixing.setText("斜体");
}
else if (currentfont.getStyle() == Font.BOLD+Font.ITALIC)
{
zixing.setText("粗斜体");
}
zixing.selectAll();
String strr = String.valueOf(currentfont.getSize());
daxiao.setText(strr);
daxiao.selectAll();
//定义可选择列表
JList zixinglist,zitilist,daxiaolist;
//获得各种字体
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
String fontName[]=ge.getAvailableFontFamilyNames();
zitilist = new JList(fontName);
zitilist.setSize(70, 20);
zitilist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//定义一次的索引个数
String zixingstyle[] = {"常规","粗体","斜体","粗斜体"};
zixinglist = new JList(zixingstyle);
zixinglist.setSize(60, 20);
zixinglist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//定义一次的索引个数
String fontSize[]={"8","9","10","11","12","14","16","18","20","22","24","26","28","36","48","72"};
daxiaolist = new JList(fontSize);
daxiaolist.setSize(60, 20);
daxiaolist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//定义一次的索引个数
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
jp1.add(ziti);
jp1.add(zixing);
jp1.add(daxiao);
jp1.add(okbu);
jp2.add(new JScrollPane(zitilist));
jp2.add(new JScrollPane(zixinglist));
jp2.add(new JScrollPane(daxiaolist));
jp2.add(cancel);
jdr.add(jp1);
jdr.add(jp2);
JLabel simple = new JLabel("杨氏记事本-HAha");
JPanel simplepane = new JPanel();
simple.setBorder(BorderFactory.createTitledBorder("像我这样"));
simplepane.add(simple);
jdr.add(simplepane);
//初始化选中相应的风格,免得之后如果不点风格就会报错
if(currentfont.getStyle()==Font.PLAIN)
zixinglist.setSelectedIndex(0);
else if(currentfont.getStyle()==Font.BOLD)
zixinglist.setSelectedIndex(1);
else if(currentfont.getStyle()==Font.ITALIC)
zixinglist.setSelectedIndex(2);//获得列表中的位置
else if(currentfont.getStyle()==(Font.BOLD+Font.ITALIC))
zixinglist.setSelectedIndex(3);
zitilist.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
// TODO 自动生成的方法存根
ziti.setText(fontName[zitilist.getSelectedIndex()]);
ziti.selectAll();
Font font1 = new Font(ziti.getText(), style[zixinglist.getSelectedIndex()], Integer.parseInt(daxiao.getText()));
//font (string,int,int);
//字型为int型,计算机不识别粗体这种人类语言
simple.setFont(font1);
}
});
okbu.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
Font font1 = new Font(ziti.getText(), style[zixinglist.getSelectedIndex()], Integer.parseInt(daxiao.getText()));
ta.setFont(font1);
jdr.dispose();
}
});
zixinglist.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
// TODO 自动生成的方法存根
zixing.setText(zixingstyle[zixinglist.getSelectedIndex()]);
zixing.selectAll();
Font font1 = new Font(ziti.getText(), style[zixinglist.getSelectedIndex()], Integer.parseInt(daxiao.getText()));
simple.setFont(font1);
}
});
daxiaolist.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
// TODO 自动生成的方法存根
daxiao.setText(fontSize[daxiaolist.getSelectedIndex()]);
daxiao.selectAll();
Font font1 = new Font(ziti.getText(), style[zixinglist.getSelectedIndex()], Integer.parseInt(daxiao.getText()));
simple.setFont(font1);
}
});
// jdr.add(jp3);
jdr.setBounds(200, 200, 400, 360);
// jdr.setResizable(false);
jdr.setVisible(true);
}
//添加替换的方法
public void replace(){
JDialog jfr = new JDialog(f,"查找页面");
jfr.setBounds(400,200,508, 180);
JButton bu1 = new JButton("替换");
JButton bu4 = new JButton("全部替换");
JButton bu5 = new JButton("确定");
JButton bu3 =new JButton("取消");
bu3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
jfr.setVisible(false);
}
});
JButton bu2 = new JButton("查找下一个");
JTextField jt = new JTextField(20);
JTextField jt2 = new JTextField(20);
JLabel jl = new JLabel("请输入要替换数据:");
JLabel j2 = new JLabel("请输入替换后数据:");
jfr.setLayout(new FlowLayout());
jfr.add(jl);
jfr.add(jt);
jfr.add(bu2);
jfr.add(j2);
jfr.add(jt2);
jfr.add(bu1);
jfr.add(bu4);
jfr.add(bu5);
bu5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
jfr.dispose();
}
});
jfr.add(bu3);
//利用while语句给循环查找与替换话的过程
//通过找到和需要查找一样的字符串的index,通过操作index达到替换的效果
bu4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
int flag = 0;
String str1 = ta.getText();
String str2 = jt.getText();
String str3 = jt2.getText();
if(ta.getSelectedText() == null)
{
s = str1.indexOf(str2,ta.getCaretPosition()+1);
}
else
s = str1.indexOf(str2, ta.getCaretPosition()-str2.length()+1);
while(s>-1){
if(s>-1)
{
ta.select(s, s+str2.length());
ta.replaceRange(str3, ta.getSelectionStart(), ta.getSelectionEnd());
flag++;
}
if(ta.getSelectedText() == null)
{
s = str1.indexOf(str2,ta.getCaretPosition()+1);
}
else
s = str1.indexOf(str2, ta.getCaretPosition()-str2.length()+1);
}
if(s<=-1 && flag==0)
{
JOptionPane.showMessageDialog(null,"找不到您替换的内容!","替换",JOptionPane.INFORMATION_MESSAGE);
}
else{
JOptionPane.showMessageDialog(null,"替换成功!","替换",JOptionPane.INFORMATION_MESSAGE);
}
}
});
bu2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
String str1 = ta.getText();
String str2 = jt.getText();
if(ta.getSelectedText() == null)
{
s = str1.indexOf(str2,ta.getCaretPosition()+1);
}
else
s = str1.indexOf(str2, ta.getCaretPosition()-str2.length()+1);
if(s>-1)
{
ta.setCaretPosition(s);
ta.select(s, s+str2.length());
}
else
{
JOptionPane.showMessageDialog(null,"找不到您查找的内容!","查找",JOptionPane.INFORMATION_MESSAGE);
}
}
});
bu1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
String str3 = jt2.getText();
if(ta.getSelectedText()==null){
JOptionPane.showMessageDialog(null,"找不到您要替换的内容!","替换",JOptionPane.INFORMATION_MESSAGE);
}
else
{
ta.replaceRange(str3, ta.getSelectionStart(), ta.getSelectionEnd());
JOptionPane.showMessageDialog(null,"替换成功!","替换",JOptionPane.INFORMATION_MESSAGE);
}
}
});
jfr.setVisible(true);
}
public static void main(String[] args){
new Menu1();
}
}
java实战,一个记事本
最新推荐文章于 2022-12-13 13:38:09 发布