📄 diary.java
字号:
});
popup.add(menuItem);
MouseListener popupListener=new PopupListener(popup);
textDemo.addMouseListener(popupListener);
}
private void newMenuItem(){
int n;
Object[] option={"YES","NO","CANCEL"};
if(changed==true)
{
n=JOptionPane.showOptionDialog(null,"你的文件还没保存,你想现在保存吗?",
"保存",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE,
null,option,option[0]);
switch(n)
{
case JOptionPane.YES_OPTION:{
doSave(fileName);
fileName="";
textDemo.setText("");
changed=false;
break;
}
case JOptionPane.NO_OPTION:{
fileName="";
textDemo.setText("");
changed=false;
break;
}
case JOptionPane.CANCEL_OPTION:
break;
}
}
else
{
fileName="";
textDemo.setText("");
}
}
private void doSave(String fileName)
{
FileOutputStream fos=null;
if(fileName=="")
{
doSaveAs();
}
else
{
String str=textDemo.getText();
try
{
fos=new FileOutputStream(fileName);
fos.write(str.getBytes());
}
catch(IOException e1)
{
}
finally
{
try
{
fos.close();
}
catch (IOException e2)
{
}
}
changed=false;
}
}
private void doSaveAs()
{
FileOutputStream fos=null;
String str=textDemo.getText();
FileDialog fileDialog=new FileDialog(this,"Save As...",FileDialog.SAVE);
fileDialog.setVisible(true);
if(fileDialog.getFile()==null)
{
return;
}
fileName=fileDialog.getDirectory()+File.separator+fileDialog.getFile();
try
{
fos=new FileOutputStream(fileName);
fos.write(str.getBytes());
}
catch(IOException e1)
{
}
finally
{try
{
fos.close();
}
catch(IOException e2)
{
}
}
changed=false;
}
private void openMenuItem()
{
FileDialog fileDialog=new FileDialog(this,"Open...",
FileDialog.LOAD);
fileDialog.setVisible(true);
if(fileDialog.getFile()==null)
{
return;
}
fileName=fileDialog.getDirectory()+File.separator+fileDialog.getFile();
FileInputStream fis=null;
String str=null;
try
{
fis=new FileInputStream(fileName);
int size=fis.available();
byte[] bytes=new byte[size];
fis.read(bytes);
str=new String(bytes);
}
catch (IOException e1)
{
}
finally{
try{
fis.close();
}
catch (IOException e2)
{
}
}
if(str!=null)
{
textDemo.setText(str);
}
changed=false;
}
private void findMenuItem(ActionEvent e)
{
new Finder(this,textDemo).setVisible(true);
}
private void FcolorMenuItem(ActionEvent e)
{
Color newColor=JColorChooser.showDialog(
this,"选择字体颜色",textDemo.getForeground());
if(newColor!=null)
{
textDemo.setForeground(newColor);
}
}
private void BcolorMenuItem(ActionEvent e)
{
Color newColor=JColorChooser.showDialog(this,"选择背景颜色",textDemo.getBackground());
if(newColor!=null)
{
textDemo.setBackground(newColor);
}
}
}
class PopupListener extends MouseAdapter
{
PopupListener(JPopupMenu popupMenu)
{
popup=popupMenu;
}
public void mousePressed(MouseEvent e)
{
maybeShowPopup(e);
}
public void mouseReleased(MouseEvent e)
{
maybeShowPopup(e);
}
private void maybeShowPopup(MouseEvent e){
if(e.isPopupTrigger())
{
popup.show(e.getComponent(),e.getX(),e.getY());
}
}
JPopupMenu popup;
}
class Finder extends javax.swing.JDialog
{
public Finder(Frame parent, JTextArea textEditor)
{
super(parent,true);
this.textEditor=textEditor;
initComponents();
pack();
setLocationRelativeTo(parent);
findField.requestFocus();
}
private void initComponents()
{
GridBagConstraints gridBagConstriants;
findPanel=new JPanel();
findLabel=new JLabel();
findField=new JTextField();
buttonPanel=new JPanel();
findButton=new JButton();
closeButton=new JButton();
getContentPane().setLayout(new GridBagLayout());
setTitle("查找");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
closeDialog(e);}
});
getAccessibleContext().setAccessibleName("Find Dialog") ;
getAccessibleContext().setAccessibleDescription("Find dialog.");
findPanel.setLayout(new GridBagLayout());
findLabel.setLabelFor(findField);
findLabel.setText("查找内容:");
findPanel.add(findLabel,new GridBagConstraints());
findLabel.getAccessibleContext().setAccessibleDescription("Find text.");
findField.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
findFieldActionPerformed(e);
}
});
GridBagConstraints gridBagConstraints=new GridBagConstraints();
gridBagConstraints.fill=GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx=1.0;
gridBagConstraints.insets=new Insets(0,5,0,0);
findPanel.add(findField,gridBagConstraints);
findField.getAccessibleContext().setAccessibleName("Find Field") ;
getAccessibleContext().setAccessibleDescription("Find field.");
gridBagConstraints=new GridBagConstraints();
gridBagConstraints.fill=GridBagConstraints.BOTH;
gridBagConstraints.insets=new Insets(11,12,0,11);
getContentPane().add(findPanel,gridBagConstraints);
buttonPanel.setLayout(new GridBagLayout());
findButton.setMnemonic('f');
findButton.setText("查找");
findButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
findButtonActionPerformed(e);
}
});
buttonPanel.add(findButton,new GridBagConstraints());
closeButton.setMnemonic('c');
closeButton.setText("关闭");
gridBagConstraints=new GridBagConstraints();
gridBagConstraints.insets=new Insets(0,5,0,0);
buttonPanel.add(closeButton,gridBagConstraints);
gridBagConstraints=new GridBagConstraints();
gridBagConstraints.gridx=0;
gridBagConstraints.gridy=1;
gridBagConstraints.anchor=GridBagConstraints.SOUTHEAST;
gridBagConstraints.insets=new Insets(17,12,11,11);
getContentPane().add(buttonPanel,gridBagConstraints);
}
private void findFieldActionPerformed(ActionEvent e){
if(findField.getText().length()>0)
{
findButton.doClick();
}
}
private void findButtonActionPerformed(ActionEvent e)
{
String text=textEditor.getText();
String textToFind=findField.getText();
if(!"".equals(textToFind))
{
int index=text.indexOf(textToFind);
if(index!=-1)
{
textEditor.setCaretPosition(index);
closeDialog(null);
}
else
{
Toolkit.getDefaultToolkit().beep();
}
}
}
private void closeButtonActionPerformed(ActionEvent e)
{
closeDialog(null);
}
private void closeDialog(WindowEvent e)
{
setVisible(false);
dispose();
}
private javax.swing.JPanel buttonPanel;
private javax.swing.JButton closeButton;
private javax.swing.JButton findButton;
private javax.swing.JTextField findField;
private javax.swing.JLabel findLabel;
private javax.swing.JPanel findPanel;
private javax.swing.JTextArea textEditor;
}
class About extends javax.swing.JDialog
{
public About(Frame parent)
{
super(parent,true);
initComponents();
pack();
setLocationRelativeTo(parent);
}
private void initComponents()
{
JTextField jTextField1=new JTextField();
setTitle("关于记事本");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
closeDialog(e);
}
});
getAccessibleContext().setAccessibleName("About Dialog");
getAccessibleContext().setAccessibleDescription("About dialog.");
jTextField1.setEditable(false);
jTextField1.setText("这是一个简单的文本编辑器");
getContentPane().add(jTextField1,BorderLayout.CENTER);
jTextField1.getAccessibleContext().setAccessibleName("About Text");
jTextField1.getAccessibleContext().setAccessibleDescription("About text.");
}
private void closeDialog(WindowEvent e)
{
setVisible(false);
dispose();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -