📄 main.java
字号:
}
}
}
if(e.getSource()==jump)
{
Goto.setVisible(true);
}
if(e.getSource()==Goto.取消)
{
Goto.setVisible(false);
Goto.GotoText.setText(null);
}
if(e.getSource()==find || e.getSource()==btn3)
{
Srd.setVisible(true);
}
if(e.getSource()==replace || e.getSource()==btn4)
{
Rld.setVisible(true);
}
if(e.getSource()==help || e.getSource()==btn7)
{
Hdg.setVisible(true);
}
if(e.getSource()==mit6 || e.getSource()==popmenu.getItem(3)|| e.getSource()==btn5)
{
String temp=text1.getSelectedText(); //获取鼠标移动选取文本
StringSelection text=new StringSelection(temp); //买票
clipboard.setContents(text,null); //把内容放到剪贴板中
int start=text1.getSelectionStart(); //开始位置
int end =text1.getSelectionEnd(); //结束位置
text1.replaceRange("",start,end); //从text1中删除被选取的文本
}
if(e.getSource()==mit7 || e.getSource()==popmenu.getItem(2))
{
String temp=text1.getSelectedText();
StringSelection text=new StringSelection(temp);
clipboard.setContents(text,null);
mit8.setEnabled(true);
}
if(e.getSource()==mit8 ||e.getSource()==popmenu.getItem(4))
{
Transferable contents=clipboard.getContents(this); //从当前剪贴板中获取数据
DataFlavor flavor=DataFlavor.stringFlavor;
if( contents.isDataFlavorSupported(flavor)) //如果剪切板中的内容是支持数据类型的话
try
{
String str;
str=(String)contents.getTransferData(flavor);
int start=text1.getSelectionStart(); //开始位置
int end =text1.getSelectionEnd(); //结束位置
text1.replaceRange(str,start,end);
}
catch(Exception ee)
{
}
}
if(e.getSource()==mit9 || e.getSource()==popmenu.getItem(0))
{
String m="清空文本区?";
int ok=JOptionPane.showConfirmDialog(this,m,"确认",JOptionPane.YES_NO_OPTION,
JOptionPane.INFORMATION_MESSAGE);
if(ok==JOptionPane.YES_OPTION)
{
text1.setText(null);
setTitle(" 记事本");
}
}
if(e.getSource()==mit10)
{
SimpleDateFormat matter=new SimpleDateFormat("HH时mm分ss秒 yyyy年MM月Edd日 北京时间");
text1.append("\n现在时间是:"+matter.format(nowTime));
}
if(e.getSource()==mit11)
{
Mf2.setVisible(true);
validate();
// Mf2.setBounds(100,100,200,250);
}
if(e.getSource()==mit12 || e.getSource()==btn6)
{
Color newColor=JColorChooser.showDialog(this,"调色板",text1.getBackground());
text1.setBackground(newColor);
validate();
}
if(e.getSource()==mit13)
{
p=getToolkit().getPrintJob(this,"ok",null);
g=p.getGraphics();
g.dispose();
p.end();
}
if(e.getSource()==mit1)
{
String s=text1.getText();
char a[]=s.toCharArray();
if(a.length!=0)
{ constdialog.setVisible(true);}
else
{
text1.setText(null);
}
}
if(e.getSource()==mit16)
{
try{
Runtime notepade=Runtime.getRuntime();
notepade.exec("notepad.chm");
}
catch(Exception expe){}
}
if(e.getSource()==Goto.确定)
{
// String 临记=Goto.GotoText.getText();
// int 行数=Integer.valueOf(临记).intValue();
// text1.setRows(行数);
}
if(e.getSource()==constdialog.确定)
{
filesave.setVisible(true);
}
if(e.getSource()==constdialog.否)
{
constdialog.setVisible(false);
text1.setText(null);
}
if(e.getSource()==Srd.btn1);
{
// int position;
String temp=Srd.text1.getText();
String tom=text1.getText();
String lenght=Srd.text1.getText();
//StringTokenizer fenxi=new StringTokenizer(tom,"temp ");
int len=lenght.length();
/* int position=tom.indexOf(temp);
if(position==-1)
{
JOptionPane.showMessageDialog(this,"文件没有找到");
}
else{
text1.setCaretPosition(position);
text1.setSelectionStart(position);
text1.setSelectionEnd(position+len);
}*/
int MouseCare=text1.getCaretPosition();
int nextone=tom.indexOf(temp,MouseCare);
try{
text1.setCaretPosition(nextone);
text1.setSelectionStart(nextone);
text1.setSelectionEnd(nextone+len);
}
catch(Exception ew2){}
if(nextone==-1)
{
JOptionPane.showMessageDialog(this,"文件没有找到");
}
}
if(e.getSource()==Srd.btn2)
{
Srd.setVisible(false);
Srd.text1.setText("");
}
if(e.getSource()==constdialog.退出)
{
JOptionPane.showMessageDialog(this,"退出程序?");
System.exit(0);
}
if(e.getSource()==Rld.btn1)
{
// text1.setCaretPosition(0);
//int 光标;
String 临时文件=Rld.txt1.getText();
String 当前文本=text1.getText();
int 长度=(int)Rld.txt1.getText().length();
//int 行=text1.getLineCount();
/*int 光标=临时文件.indexOf(当前文本);
if(光标==-1)
{
JOptionPane.showMessageDialog(this,"文件没有找到");
}
try{
text1.setCaretPosition(光标);
text1.setSelectionStart(光标);
text1.setSelectionEnd(光标+长度);
}
catch(Exception ew2){}*/
int 当前光标=text1.getCaretPosition();
int 下一位置=当前文本.indexOf(临时文件,当前光标);
try{
text1.setCaretPosition(下一位置);
text1.setSelectionStart(下一位置);
text1.setSelectionEnd(下一位置+长度);
}
catch(Exception ew2){}
if(下一位置==-1)
{
JOptionPane.showMessageDialog(this,"文件没有找到");
}
//光标=当前文本.indexOf(临时文件,光标+1);
}
if(e.getSource()==Rld.btn2)
{
String 临时文件=Rld.txt1.getText();
String 当前文本=text1.getText();
int 长度=(int)Rld.txt1.getText().length();
int 光标=当前文本.indexOf(临时文件);
if(光标==-1)
{
JOptionPane.showMessageDialog(this,"文件没有找到");
}
try{
text1.setCaretPosition(光标);
text1.setSelectionStart(光标);
text1.setSelectionEnd(光标+长度);
}
catch(Exception ew2){}
String abc=Rld.RepalceText.getText();
text1.replaceRange(abc,光标,光标+长度);
}
if(e.getSource()==Rld.btn3)
{
}
if(e.getSource()==Rld.btn4)
{
Rld.RepalceText.setText(null);
Rld.txt1.setText(null);
Rld.setVisible(false);
}
if(e.getSource()==btn1)
{
String s;
int state=chooser.showOpenDialog(null);
File file=chooser.getSelectedFile();
String title=chooser.getName(file);
setTitle(title+" 记事本");
if(file!=null)
{
try{
in=new FileReader(file);
cin=new BufferedReader(in);
while((s=cin.readLine())!=null)
text1.append(s+'\n');
cin.close();
in.close();
}
catch(IOException ea){}
}
}
if(e.getSource()==btn2)
{
int state=chooser.showSaveDialog(null);
File file=chooser.getSelectedFile();
if(file!=null)
{
try{
tofile=new FileWriter(file);
out=new BufferedWriter(tofile);
out.write(text1.getText(),0,(text1.getText()).length());
out.flush();
out.close();
tofile.close();
}
catch(IOException exs){}
}
}
if(e.getSource()==Mf2.btn1)
{
String name=Mf2.list.getSelectedItem();
String str=Mf2.list1.getSelectedItem();
String str1=Mf2.list2.getSelectedItem();
int size=Integer.parseInt(str1);
//int style=Integer.parseInt(str);
if(str=="粗体")
{
Font f=new Font(name,Font.BOLD,size);
text1.setFont(f);
validate();
}
if(str=="斜体")
{
Font f=new Font(name,Font.ITALIC,size);
text1.setFont(f);
validate();
}
if(str=="粗斜体")
{
Font f=new Font(name,Font.CENTER_BASELINE,size);
text1.setFont(f);
validate();
}
if(str=="真字体类")
{
Font f=new Font(name,Font.TRUETYPE_FONT,size);
text1.setFont(f);
validate();
}
if(str=="罗马字体")
{
Font f=new Font(name,Font.ROMAN_BASELINE,size);
text1.setFont(f);
validate();
} if(str=="PLAIN")
{
Font f=new Font(name,Font.PLAIN ,size);
text1.setFont(f);
validate();
} if(str=="HANGING_BASELINE")
{
Font f=new Font(name,Font.HANGING_BASELINE,size);
text1.setFont(f);
validate();
} if(str=="从左到右")
{
Font f=new Font(name,Font.LAYOUT_LEFT_TO_RIGHT,size);
text1.setFont(f);
validate();
} if(str=="LEFT to RIGHT")
{
Font f=new Font(name,Font.LAYOUT_NO_LIMIT_CONTEXT,size);
text1.setFont(f);
validate();
} if(str=="从右到左")
{
Font f=new Font(name,Font.LAYOUT_RIGHT_TO_LEFT,size);
text1.setFont(f);
validate();
}
}
if(e.getSource()==Window风格)
{
String plafName="com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
try
{
UIManager.setLookAndFeel(plafName);
SwingUtilities.updateComponentTreeUI(this);
}
catch(Exception err)
{
err.printStackTrace();
}
}
if(e.getSource()==Metal风格)
{
String plafName=UIManager.getSystemLookAndFeelClassName();
try
{
UIManager.setLookAndFeel(plafName);
SwingUtilities.updateComponentTreeUI(this);
}
catch(Exception err)
{
err.printStackTrace();
}
}
if(e.getSource()==Motif风格)
{
String plafName="com.sun.java.swing.plaf.motif.MotifLookAndFeel";
try
{
UIManager.setLookAndFeel(plafName);
SwingUtilities.updateComponentTreeUI(this);
}
catch(Exception err)
{
err.printStackTrace();
}
}
}
public void itemStateChanged(ItemEvent e)
{
if(e.getItemSelectable()==自动换行)
{
if(自动换行.getState())
{
text1.setLineWrap(true);
}
else
{
text1.setLineWrap(false);
}
}
if(e.getItemSelectable()==jDriver)
{
try
{
String 路径=jDriver.getSelectedItem();
JTree tree=null;
System.out.println("ok");
root=new DefaultMutableTreeNode(路径);
File path=new File(路径);
File[] listfile=path.listFiles();
System.out.println("ok1");
Vector listData=new Vector();
listData.clear();
//list1.removeAll();
for(int k=1; k<listfile.length; k++)
{
if(listfile[k].isDirectory())
{
t1=new DefaultMutableTreeNode(listfile[k]);
root.add(t1);
System.out.println("ok2");
}
else if(listfile[k].isFile())
{
listData.add(0,listfile[k]);
System.out.println("ok3");
}
}
// listData.set(0,"文件");
// list1=new JList();
// list1.setListData(listdata);
//int n=tree.getCounts();
tree=new JTree(root);
//jsp=new JScrollPane
}
catch(Exception ioexp){
System.out.println("你选择了不正确的盘符");
}
}
}
}
public class Main
{
public static void main(String args[])
{
MyFrame win=new MyFrame("记事本");
Toolkit tool=win.getToolkit();
Image myimage=tool.getImage("ac.gif");
win.setIconImage(myimage);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -