📄 mytxt.java
字号:
fontSize.setBounds(280,5,70,20);
fontNamesText.setBounds(10,26,140,20);
fontStyleText.setBounds(155,26,120,20);
fontSizeText.setBounds(280,26,70,20);
right.setBounds(360,26,70,20);
right.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
mt.setFont(fontSimple);
fontDialog.dispose();
}
});
cancel.setBounds(360,50,70,20);
cancel.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
fontDialog.dispose();
}
});
fontAllNames=ge.getAvailableFontFamilyNames();
fontNamesScroll.setListData(fontAllNames);
fontNamesScroll.setVisibleRowCount(6);
fontNamesScroll.setFixedCellWidth(140);
fontNamesScroll.setFixedCellHeight(20);
namesScroll.setBounds(10,48,140,140);
while(true){
if(fontAllNames[index].equals(font)){
fontNamesScroll.setSelectedIndex(index);
index=0;
break;
}
index++;
}
fontNamesScroll.addListSelectionListener(this);
fontStyleScroll.setListData(fontStyleList);
fontStyleScroll.setVisibleRowCount(6);
fontStyleScroll.setFixedCellHeight(20);
fontStyleScroll.setFixedCellWidth(120);
styleScroll.setBounds(155,48,120,140);
while(true){
if(fontStyleList[index].equals(style)){
fontStyleScroll.setSelectedIndex(index);
index=0;
break;
}
index++;
}
fontStyleScroll.addListSelectionListener(this);
fontSizeScroll.setListData(fontSizeList);
fontSizeScroll.setVisibleRowCount(6);
fontSizeScroll.setFixedCellHeight(20);
fontSizeScroll.setFixedCellWidth(70);
sizeScroll.setBounds(280,48,70,140);
while(true){
if(fontSizeList[index].equals(size)){
fontSizeScroll.setSelectedIndex(index);
index=0;
break;
}
index++;
}
fontSizeScroll.addListSelectionListener(this);
simple=new JTextField("罗识识的记事本,My Notepad...");
simple.setSize(250,70);
simple.setEditable(false);
simple.setHorizontalAlignment(SwingConstants.CENTER);
fontsimple.setBounds(5,196,260,70);
/*fontsimple.setLocation(5, 196);
*fontsimple.setPreferredSize(new Dimension(250,100));不等于下面
*fontsimple.setLocation(5, 196);
*fontsimple.setSize(new Dimension(250,100));
*/
fontsimple.setBorder(BorderFactory.createTitledBorder("示例"));
fontsimple.add(simple);
con.add(fontNames);
con.add(fontStyle);
con.add(fontSize);
con.add(fontNamesText);
con.add(fontStyleText);
con.add(fontSizeText);
con.add(right);
con.add(cancel);
con.add(sizeScroll);
con.add(namesScroll);
con.add(styleScroll);
con.add(fontsimple);
fontDialog.setSize(new Dimension(445,330));
fontDialog.setLocation(200,200);
fontDialog.setResizable(false);
fontDialog.setVisible(true);
}
public void updateSimple(){
fontSimple=new Font(font,style1,Integer.parseInt(size));
simple.setFont(fontSimple);
}
public void valueChanged(ListSelectionEvent e){
if(e.getSource()==fontNamesScroll){
font=fontNamesScroll.getSelectedValue().toString();
fontNamesText.setText(font);
updateSimple();
}
else if(e.getSource()==fontStyleScroll){
style=fontStyleScroll.getSelectedValue().toString();
fontStyleText.setText(style);
if(fontStyleScroll.getSelectedIndex()==0){
style1=Font.PLAIN;
}
else if(fontStyleScroll.getSelectedIndex()==1){
style1=Font.ITALIC;
}
else if(fontStyleScroll.getSelectedIndex()==2){
style1=Font.BOLD;
}
else
style1=Font.ITALIC+Font.BOLD;
updateSimple();
}
else{
size=fontSizeScroll.getSelectedValue().toString();
fontSizeText.setText(size);
updateSimple();
}
}
}
public void checkMenuItemEnabled(){
Txt.requestFocus();
String s=Txt.getSelectedText();
if(s!=null){
ji9.setEnabled(true);
jp_Cut.setEnabled(true);
ji10.setEnabled(true);
jp_Copy.setEnabled(true);
ji12.setEnabled(true);
jp_Del.setEnabled(true);
}
else{
ji9.setEnabled(false);
jp_Cut.setEnabled(false);
ji10.setEnabled(false);
jp_Copy.setEnabled(false);
ji12.setEnabled(false);
jp_Del.setEnabled(false);
}
Transferable b=cb.getContents(this);
if(b!=null){
ji11.setEnabled(true);
jp_Parst.setEnabled(true);
}
else{
ji11.setEnabled(false);
jp_Parst.setEnabled(false);
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==ji1){
if(!OldText.equals(Txt.getText())){
int result=JOptionPane.showConfirmDialog(this, "文件未保存,是否要保存??","文件是否保存?",JOptionPane.YES_NO_CANCEL_OPTION);
if(result==JOptionPane.YES_OPTION){
JFileChooser jfc=new JFileChooser();
jfc.setDialogTitle("保存文件");
//jfc.changeToParentDirectory();
jfc.setApproveButtonText("保存");
jfc.addChoosableFileFilter(new MyFileFilter(".txt","txt文本文件"));
//jfc.addChoosableFileFilter(new MyFileFilter(".java","java文本文件"));
//jfc.setFileFilter(new MyFileFilter());
if(jfc.showSaveDialog(this)==JFileChooser.APPROVE_OPTION){
File ft=jfc.getSelectedFile();
Filename=jfc.getSelectedFile().getName();
if(Filename.endsWith(".txt")){
//if(jfc.getSelectedFile().getName().toUpperCase().endsWith(".txt".toUpperCase())){
try{
FileWriter f=new FileWriter(ft);//用于实现字符流
BufferedWriter fs=new BufferedWriter(f);
fs.write(Txt.getText(),0,Txt.getText().length());
fs.flush();
fs.close();
Txt.setText("");
Filename=null;
}catch(FileNotFoundException ex){
JOptionPane.showMessageDialog(null, "文件未找到!!");
return;
}catch(IOException ey){
JOptionPane.showMessageDialog(null, "保存出错");
return;
}
}
else{
JOptionPane.showMessageDialog(jfc, "请正确输入文件名,文件未保存!!!");
}
}
else if(jfc.showSaveDialog(this)==JOptionPane.NO_OPTION){
return;
}
}
else if(result==JOptionPane.NO_OPTION)
Txt.setText("");
else if(result==JOptionPane.CANCEL_OPTION)
return;
}
else
Txt.setText("");
}
else if(e.getSource()==ji2){
if(!OldText.equals(Txt.getText())){
int result=JOptionPane.showConfirmDialog(this, "文件未保存,是否要保存??","文件是否保存?",JOptionPane.YES_NO_CANCEL_OPTION);
if(result==JOptionPane.YES_OPTION){
JFileChooser jfc=new JFileChooser();
jfc.setDialogTitle("保存文件");
//jfc.changeToParentDirectory();
jfc.setApproveButtonText("保存");
jfc.addChoosableFileFilter(new MyFileFilter(".txt","txt文本文件"));
//jfc.addChoosableFileFilter(new MyFileFilter(".java","java文本文件"));
//jfc.setFileFilter(new MyFileFilter());
if(jfc.showSaveDialog(this)==JFileChooser.APPROVE_OPTION){
File ft=jfc.getSelectedFile();
Filename=jfc.getSelectedFile().getName();
if(Filename.endsWith(".txt")){
//if(jfc.getSelectedFile().getName().toUpperCase().endsWith(".txt".toUpperCase())){
try{
FileWriter f=new FileWriter(ft);//用于实现字符流
BufferedWriter fs=new BufferedWriter(f);
fs.write(Txt.getText(),0,Txt.getText().length());
fs.flush();
fs.close();
Txt.setText("");
Filename=null;
}catch(FileNotFoundException ex){
JOptionPane.showMessageDialog(null, "文件未找到!!");
return;
}catch(IOException ey){
JOptionPane.showMessageDialog(null, "保存出错");
return;
}
}
else{
JOptionPane.showMessageDialog(jfc, "请正确输入文件名,文件未保存!!!");
}
}
else if(result==JOptionPane.NO_OPTION){
return;
}
JFileChooser jf=new JFileChooser();
String BText;
jfc.setDialogTitle("打开文件");
jfc.setApproveButtonText("打开");
jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
jfc.addChoosableFileFilter(new MyFileFilter(".txt",".txt文本文件"));
if(jfc.showOpenDialog(this)==JFileChooser.APPROVE_OPTION){
File f=jf.getSelectedFile();
Filename=jf.getSelectedFile().getName();
if(f.getName()==null||f.getName().equals("")){
JOptionPane.showMessageDialog(null, "请正确输入文件名");
}
else if(f.getName().toUpperCase().endsWith(".txt".toUpperCase())){
try{
Txt.setText("");
FileReader fr=new FileReader(f);
BufferedReader br=new BufferedReader(fr);
while((BText=br.readLine())!=null){
Txt.append(BText+"\n\r");//文件这里要添加/15/12,因为在保存的时候,每一行都不保存的
}
br.close();
}catch(FileNotFoundException ey){
}catch(IOException et){
}
}
}
}
else if(result==JOptionPane.NO_OPTION){
JFileChooser jfc=new JFileChooser();
String BText;
jfc.setDialogTitle("打开文件");
jfc.setApproveButtonText("打开");
jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
jfc.addChoosableFileFilter(new MyFileFilter(".txt",".txt文本文件"));
if(jfc.showOpenDialog(this)==JFileChooser.APPROVE_OPTION){
File f=jfc.getSelectedFile();
Filename=jfc.getSelectedFile().getName();
if(f.getName()==null||f.getName().equals("")){
JOptionPane.showMessageDialog(null, "请正确输入文件名");
}
else if(f.getName().toUpperCase().endsWith(".txt".toUpperCase())){
try{
Txt.setText("");
FileReader fr=new FileReader(f);
BufferedReader br=new BufferedReader(fr);
while((BText=br.readLine())!=null){
Txt.append(BText+"\n\r");//文件这里要添加/15/12,因为在保存的时候,每一行都不保存的
}
br.close();
}catch(FileNotFoundException ey){
}catch(IOException et){
}
}
}
}
}
else{
JFileChooser jfc=new JFileChooser();
String BText;
jfc.setDialogTitle("打开文件");
jfc.setApproveButtonText("打开");
jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
jfc.addChoosableFileFilter(new MyFileFilter(".txt",".txt文本文件"));
if(jfc.showOpenDialog(this)==JFileChooser.APPROVE_OPTION){
File f=jfc.getSelectedFile();
Filename=jfc.getSelectedFile().getName();
if(f.getName()==null||f.getName().equals("")){
JOptionPane.showMessageDialog(null, "请正确输入文件名");
}
else if(f.getName().toUpperCase().endsWith(".txt".toUpperCase())){
try{
Txt.setText("");
FileReader fr=new FileReader(f);
BufferedReader br=new BufferedReader(fr);
while((BText=br.readLine())!=null){
Txt.append(BText+"\n\r");//文件这里要添加/15/12,因为在保存的时候,每一行都不保存的
}
br.close();
}catch(FileNotFoundException ey){
}catch(IOException et){
}
}
}
}
}
else if(e.getSource()==ji3){
if(Filename==null){
JFileChooser jf=new JFileChooser();
jf.setDialogTitle("保存文件");
jf.setApproveButtonText("保存");
int result=jf.showSaveDialog(this);
if(result==JFileChooser.APPROVE_OPTION){
File file=jf.getSelectedFile();
Filename=jf.getSelectedFile().getName();
try{
FileWriter fw=new FileWriter(file);
BufferedWriter bs=new BufferedWriter(fw);
bs.write(OldText=Txt.getText(), 0, Txt.getText().length());
bs.flush();
bs.close();
NotSave=false;
}catch(IOException et){
}
}
}
else{
if(!OldText.equals(Txt.getText())){
File file=new File(Filename);
try{
FileWriter fw=new FileWriter(file);
BufferedWriter bs=new BufferedWriter(fw);
bs.write(OldText=Txt.getText(), 0, Txt.getText().length());
bs.flush();
bs.close();
}catch(IOException et){
}
}
}
}
else if(e.getSource()==ji4){
JFileChooser jf=new JFileChooser();
jf.setDialogTitle("保存文件");
jf.setApproveButtonText("另存为");
int result=jf.showSaveDialog(this);
if(result==JFileChooser.APPROVE_OPTION){
File file=jf.getSelectedFile();
Filename=jf.getSelectedFile().getName();
try{
FileWriter fw=new FileWriter(file);
BufferedWriter bs=new BufferedWriter(fw);
bs.write(OldText=Txt.getText(), 0, Txt.getText().length());
bs.flush();
bs.close();
}catch(IOException et){
}
}
}
else if(e.getSource()==ji5){
}
else if(e.getSource()==ji6){
}
else if(e.getSource()==ji7){
mexit();
}
else if(e.getSource()==ji8||e.getSource()==jp_Undo){
Txt.requestFocus();
if(undo.canUndo()){
try {
undo.undo();
}
catch(CannotUndoException ex) {
System.out.println("Unable to undo: " + ex);
ex.printStackTrace();
}
if(!undo.canUndo()){
ji8.setEnabled(false);
jp_Undo.setEnabled(false);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -