myedit.java
来自「a text editor application.」· Java 代码 · 共 731 行 · 第 1/2 页
JAVA
731 行
text.selectAll();
}
//ACTION FOR TIME/DATE MENU OPTION OF EDIT MENU
if (e.getSource()==TIMEDIT)
{
Date currDate;
String dd;
currDate = new java.util.Date();
dd=currDate.toString();
text.insert(dd,text.getCaretPosition());
}
//ACTION FOR WORD WRAP MENU OPTION OF EDIT MENU
if (e.getSource()==WORDEDIT)
{
if(WORDEDIT.isSelected())
text.setLineWrap(true);
else
text.setLineWrap(false);
}
//ACTION FOR SET FONT MENU OPTION OF EDIT MENU
if (e.getSource()==FONTEDIT)
{
fontDialogBox fontS = new fontDialogBox();
}
//ACTION FOR FIND MENU OPTION OF SEARCH MENU
if (e.getSource()==FINDSEARCH)
{
wholeText=text.getText();
findString =JOptionPane.showInputDialog(null, "Find What", "Find",
JOptionPane.INFORMATION_MESSAGE);
ind = wholeText.indexOf(findString,0);
text.setCaretPosition(ind);
text.setSelectionStart(ind);
text.setSelectionEnd(ind+findString.length());
}
//ACTION FOR FIND NEXT MENU OPTION OF SEARCH MENU
if (e.getSource()==FINDNEXTSEARCH)
{
wholeText= text.getText();
findString = JOptionPane.showInputDialog(null, "Find What","Find Next",
JOptionPane.INFORMATION_MESSAGE);
ind = wholeText.indexOf(findString, text.getCaretPosition());
text.setCaretPosition(ind);
text.setSelectionStart(ind);
text.setSelectionEnd(ind+findString.length());
}
//ACTION FOR ABOUT MENU OPTION OF HELP MENU
if (e.getSource()==ABOUTHELP)
{
JOptionPane.showMessageDialog(null, "This is a simple Text Editor application built using Java.",
"About Editor",
JOptionPane.INFORMATION_MESSAGE);
}
}
//ACTION FOR NEW MENU OPTION OF FILE MENU
public void newfile()
{
if(!text.getText().equals(""))
{
opened=false;
int confirm = JOptionPane.showConfirmDialog(null,
"Text in the Untitled file has changed. \n Do you want to save the changes?",
"New File",
JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE);
if( confirm == JOptionPane.YES_OPTION )
{
save();
text.setText(null);
}
else if( confirm == JOptionPane.NO_OPTION )
{
text.setText(null);
}
}
}
//ACTION FOR OPEN MENU OPTION OF FILE MENU
public void open()
{
text.setText(null);
JFileChooser ch = new JFileChooser();
ch.setCurrentDirectory(new File("."));
ch.setFileFilter(new javax.swing.filechooser.FileFilter()
{
public boolean accept(File f)
{
return f.isDirectory()
|| f.getName().toLowerCase().endsWith(".java");
}
public String getDescription()
{
return "Java files";
}
});
int result = ch.showOpenDialog(new JPanel());
if(result == JFileChooser.APPROVE_OPTION)
{
filename = String.valueOf(ch.getSelectedFile());
setTitle(filename);
opened=true;
FileReader fr;
BufferedReader br;
try
{
fr=new FileReader (filename);
br=new BufferedReader(fr);
String s;
while((s=br.readLine())!=null)
{
text.append(s);
text.append("\n");
}
fr.close();
}
catch(FileNotFoundException ex)
{
JOptionPane.showMessageDialog(this, "Requested file not found", "Error Dialog box", JOptionPane.ERROR_MESSAGE);}
catch(Exception ex)
{System.out.println(ex);}
}
}
//ACTION FOR SAVE MENU OPTION OF FILE MENU
public void save()
{
if(opened==true)
{
try
{
FileWriter f1 = new FileWriter(filename);
f1.write(text.getText());
f1.close();
opened = true;
}
catch(FileNotFoundException ex)
{
JOptionPane.showMessageDialog(this, "Requested file not found", "Error Dialog box", JOptionPane.ERROR_MESSAGE);}
catch(IOException ioe){ioe.printStackTrace();}
}
else
{
JFileChooser fc = new JFileChooser();
fc.setCurrentDirectory(new File("."));
int result = fc.showSaveDialog(new JPanel());
if(result == JFileChooser.APPROVE_OPTION)
{
filename = String.valueOf(fc.getSelectedFile());
setTitle(filename);
try
{
FileWriter f1 = new FileWriter(filename);
f1.write(text.getText());
f1.close();
opened = true;
}
catch(FileNotFoundException ex)
{
JOptionPane.showMessageDialog(this, "Requested file not found", "Error Dialog box", JOptionPane.ERROR_MESSAGE);}
catch(IOException ioe){ioe.printStackTrace();}
}
}
}
//ACTION FOR EXIT MENU OPTION OF FILE MENU AND CLOSE WINDOW BUTTON
public void exitApln()
{
if(!text.getText().equals(""))
{
int confirm = JOptionPane.showConfirmDialog(null,
"Text in the file has changed. \n Do you want to save the changes?",
"Exit",
JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE);
if( confirm == JOptionPane.YES_OPTION )
{
save();
dispose();
System.exit(0);
}
else if( confirm == JOptionPane.CANCEL_OPTION )
{
e=new myedit();
String s= text.getText();
e.setVisible(true);
e.text.setText(s);
}
else if( confirm == JOptionPane.NO_OPTION )
{
dispose();
System.exit(0);
}
}
else
{
System.exit(0);
}
}
//CLASS FOR UNDOLISTENER
class UndoListener implements UndoableEditListener
{
public void undoableEditHappened(UndoableEditEvent e)
{
undo.addEdit(e.getEdit());
undoAction.update();
}
}
//CLASS FOR BUILDING AND DISPLAYING FONT DIALOG BOX
class fontDialogBox extends JFrame implements ActionListener
{
//DECLARATION OF ALL VARIABLES USED IN fontDialogBox CLASS
String availableFontString[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
JList fontList = new JList(availableFontString);
JLabel fontLabel = new JLabel("Font");
JTextField valueFont=new JTextField("Arial");
JScrollPane fontPane = new JScrollPane(fontList);
String fontStyleString[] = {"Normal","Bold","Italic","Bold Italic"};
JList styleList = new JList(fontStyleString);
JLabel styleLabel = new JLabel("Style");
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane stylePane = new JScrollPane(styleList,v,h);
JTextField valueStyle=new JTextField("Normal");
String fontSizeString[] = {"8","10","12","14","16","18","20","22","24","28"};
JList sizeList = new JList(fontSizeString);
JLabel sizeLabel = new JLabel("Font size");
JScrollPane sizePane = new JScrollPane(sizeList);
JTextField valueSize=new JTextField("12");
JButton okButton = new JButton("OK");
JButton cancelButton = new JButton("Cancel");
JLabel sampleLabel = new JLabel("Sample:");
JTextField sample = new JTextField(" AaBbCc");
Font selectedFont;
//DEFAULT CONSTRUCTOR OF fontDialogBox CLASS
public fontDialogBox()
{
setSize(500,300);
setTitle("Font");
setVisible(true);
sample.setEditable(false);
getContentPane().setLayout(null);
fontLabel.setBounds(10,10,170,20);
valueFont.setBounds(10,35,170,20);
fontPane.setBounds(10,60,170,150);
styleLabel.setBounds(200,10,100,20);
valueStyle.setBounds(200,35,100,20);
stylePane.setBounds(200,60,100,150);
sizeLabel.setBounds(320,10,50,20);
valueSize.setBounds(320,35,50,20);
sizePane.setBounds(320,60,50,150);
okButton.setBounds(400,35,80,20);
cancelButton.setBounds(400,60,80,20);
sampleLabel.setBounds(150,235,50,30);
sample.setBounds(200,235,100,30);
getContentPane().add(fontLabel);
getContentPane().add(fontPane);
getContentPane().add(valueFont);
getContentPane().add(styleLabel);
getContentPane().add(stylePane);
getContentPane().add(valueStyle);
getContentPane().add(sizeLabel);
getContentPane().add(sizePane);
getContentPane().add(valueSize);
getContentPane().add(okButton);
getContentPane().add(cancelButton);
getContentPane().add(sampleLabel);
getContentPane().add(sample);
okButton.addActionListener(this);
cancelButton.addActionListener(this);
fontList.addListSelectionListener(new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent event)
{
if (!event.getValueIsAdjusting())
{
valueFont.setText(fontList.getSelectedValue().toString());
selectedFont = new Font(valueFont.getText(),styleList.getSelectedIndex(),Integer.parseInt(valueSize.getText()));
sample.setFont(selectedFont);
}
}
});
styleList.addListSelectionListener(new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent event)
{
if (!event.getValueIsAdjusting())
{
valueStyle.setText(styleList.getSelectedValue().toString());
selectedFont = new Font(valueFont.getText(),styleList.getSelectedIndex(),Integer.parseInt(valueSize.getText()));
sample.setFont(selectedFont);
}
}
});
sizeList.addListSelectionListener(new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent event)
{
if (!event.getValueIsAdjusting())
{
valueSize.setText(sizeList.getSelectedValue().toString());
selectedFont = new Font(valueFont.getText(),styleList.getSelectedIndex(),Integer.parseInt(valueSize.getText()));
sample.setFont(selectedFont);
}
}
});
}//END OF DEFAULT CONSTRUCTOR OF fontdialogBox CLASS
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==okButton)
{
selectedFont = new Font(valueFont.getText(),styleList.getSelectedIndex(),Integer.parseInt(valueSize.getText()));
text.setFont(selectedFont);
setVisible(false);
}
if(ae.getSource()==cancelButton)
{
setVisible(false);
}
}
}// END OF fontDialogBox CLASS
//MAIN FUNCTION OF MYEDIT CLASS
public static void main(String args[])
{
e= new myedit();
}
}//END OF MYEDIT CLASS
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?