📄 mynotepad.java
字号:
find.showReplace();
}
else if(e.getSource()==itemCut)
{
temp=text.getSelectedText();
StringBuffer tmp=new StringBuffer(text.getText());
int start=text.getSelectionStart();
int end=text.getSelectionEnd();
tmp.delete(start, end);
text.setText(tmp.toString());
text.setSelectionStart(start);
}
else if(e.getSource()==itemDelete)
{
StringBuffer tmp=new StringBuffer(text.getText());
int start=text.getSelectionStart();
int end=text.getSelectionEnd();
tmp.delete(start, end);
text.setText(tmp.toString());
text.setSelectionStart(start);
}
else if(e.getSource()==itemCopy)
{
temp=text.getSelectedText();
}
else if(e.getSource()==itemPaste)
{
int start=text.getSelectionStart();
text.insert(temp, start);
}
else if(e.getSource()==itemSelectAll)
{
text.selectAll();
}
else if(e.getSource()==itemTime)
{
java.text.SimpleDateFormat timeObj=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
this.text.append("\n"+timeObj.format(new java.util.Date()));
}
/* else if(e.getSource()==itemWrap)
{
text.getFont();
if(text.getSelectionEnd()==text.WIDTH)
{
text.append("\n");
}
}
else if(e.getSource()==itemUndo)
{
}
*/
else if(e.getSource()==itemBgColor)
{
text.requestFocus();
Color color=JColorChooser.showDialog(this, "change background color", Color.white);
if(color!=null)
{
text.setBackground(color);
}
else
return;
}
else if(e.getSource()==itemFgColor)
{
text.requestFocus();
Color color=JColorChooser.showDialog(this, "change foreground color", Color.black);
if(color!=null)
{
text.setForeground(color);
}
else
return;
}
else if(e.getSource()==itemFont)
{
font=new Dialog(this,"font");
font.setLocation(250,250);
font.setLayout(new FlowLayout());
Label topic=new Label("font setting");
Label sample=new Label("sample:my simple notepad!");
List list=new List(6,false);
list.add("Plain");
list.add("Bold");
list.add("Italic");
list.add("Bold+Italic");
MyItemListener ItemObj=new MyItemListener(sample);
list.addItemListener(ItemObj);
Button ok=new Button("ok");
ok.addActionListener(new MyButtonListener(this.text,font,list));
ok.setSize(new Dimension(20,5));
font.add(topic);
font.add(list);
font.add(sample);
font.add(ok);
font.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent ee)
{
font.dispose();
}
}
);
font.setSize(180,240);
font.setResizable(false);
font.setVisible(true);
}
else if(e.getSource()==itemAbout)
{
about=new Dialog(this,"about notepad");
TextArea ta=new TextArea("welcome to use this notepad!\n\nwriter: Zhou Xinyu");
ta.setEditable(false);
about.add(ta);
about.setBounds(250, 300, 200, 200);
about.setResizable(false);
about.setVisible(true);
about.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
about.dispose();
}
}
);
}
}
/* class undoHandler implements UndoableEditListener
{
public void undoableEditHappened(undoableEditEvent ue)
{
undo.addEdit(ue);
}
}
*/
}
class MyButtonListener implements ActionListener
{
TextArea text;
Dialog font;
List list;
public MyButtonListener(TextArea text,Dialog font,List list)
{
this.list=list;
this.font=font;
this.text=text;
}
public void actionPerformed(ActionEvent e)
{
font.setVisible(false);
switch(list.getSelectedIndex())
{
case 0:
{
text.setFont(new Font("Times new Roman",Font.PLAIN,text.getFont().getSize()));
break;
}
case 1:
{
text.setFont(new Font("Times new Roman",Font.BOLD,text.getFont().getSize()));
break;
}
case 2:
{
text.setFont(new Font("Times new Roman",Font.ITALIC,text.getFont().getSize()));
break;
}
case 3:
{
text.setFont(new Font("Times new Roman",Font.ITALIC+Font.BOLD,text.getFont().getSize()));
break;
}
}
}
}
class MyItemListener implements ItemListener
{
Label text;
public MyItemListener(Label text)
{
this.text=text;
}
public void itemStateChanged(ItemEvent e)
{
int id_font=((java.awt.List)e.getSource()).getSelectedIndex();
switch(id_font)
{
case 0:
{
text.setFont(new Font("Times new Roman",Font.PLAIN,text.getFont().getSize()));
break;
}
case 1:
{
text.setFont(new Font("Times new Roman",Font.BOLD,text.getFont().getSize()));
break;
}
case 2:
{
text.setFont(new Font("Times new Roman",Font.ITALIC,text.getFont().getSize()));
break;
}
case 3:
{
text.setFont(new Font("Times new Roman",Font.ITALIC+Font.BOLD,text.getFont().getSize()));
break;
}
}
}
}
class Find extends Dialog implements ActionListener
{
Label lFind=new Label("search : ");
Label lReplace=new Label("replace : ");
TextField tFind=new TextField(10);
TextField tReplace=new TextField(10);
Button bFind=new Button("search");
Button bReplace=new Button("replace");
TextArea content;
public Find(Frame owner,TextArea content)
{
super(owner,"search",false);
this.content=content;
setLayout(null);
lFind.setBounds(10,30,80,20);
lReplace.setBounds(10,70,80,20);
tFind.setBounds(90,30,90,20);
tReplace.setBounds(90,70,90,20);
bFind.setBounds(190,30,80,20);
bReplace.setBounds(190,70,80,20);
add(lFind);
add(tFind);
add(bFind);
add(lReplace);
add(tReplace);
add(bReplace);
setResizable(false);
bFind.addActionListener(this);
bReplace.addActionListener(this);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
Find.this.dispose();//remember it
}
});
this.setLocation(250,320);
}
public void showFind()
{
setTitle("search");
setSize(280,55);
setVisible(true);
}
public void showReplace()
{
setTitle("replace");
setSize(280,110);
setVisible(true);
}
public void find()
{
String text=content.getText();
String str=tFind.getText();
int end=text.length();
int len=str.length();
// int start=content.getCaretPosition();
int start=content.getSelectionEnd();//search from the location of the cursor;
boolean find=true;
if(start==end)
{
start=0;
}
for(;start<=end-len;start++)
{
if(text.substring(start,start+len).equals(str)){
content.select(start,start+len);
// content.setSelectionStart(start);
// content.setSelectionEnd(start+len);
// this.setVisible(false);
content.requestFocus();
find=true;
return;
}
find=false;
}
if(find==false){
JOptionPane.showMessageDialog(this, "this is the last one");
}
}
public void replace()
{
String str=tReplace.getText();
if(content.getSelectedText().equals(tFind.getText()))
{
content.replaceRange(str, content.getSelectionStart(), content.getSelectionEnd());
// this.setVisible(false);
}
else
{
find();
content.replaceRange(str, content.getSelectionStart(), content.getSelectionEnd());
// this.setVisible(false);
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==bFind)
{
find();
}
else if(e.getSource()==bReplace)
{
replace();
}
}
}
public class MyNotepad
{
public static void main(String args[])
{
new FileWindows();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -