📄 notepad.java
字号:
text.paste();
}
else if(e.getSource()==itemRemove)
{
int start = text.getSelectionStart();
int end = text.getSelectionEnd();
text.replaceRange("",start,end);
}
else if(e.getSource()==itemSelectAll)
{
text.selectAll();
}
else if(e.getSource()==itemTime)
{
nowTime = new Date();
time = new SimpleDateFormat("北京时间:yyyy年MM月dd日 E HH时mm分ss秒");
text.insert(time.format(nowTime),text.getCaretPosition());
}
else if(e.getSource()==itemLineWrap)
{
if((clickTimes%2)!=0)
{
text.setLineWrap(true);
text.setWrapStyleWord(true);
clickTimes++;
}
else
{
text.setLineWrap(false);
text.setWrapStyleWord(false);
clickTimes++;
}
}
else if(e.getSource()==itemAbout)
{
AboutDialog about=new AboutDialog(this,"关于",true);
about.setVisible(true);
}
}
public void changedUpdate(DocumentEvent e)
{
text_event = 1;
}
public void insertUpdate(DocumentEvent e){ text_event = 1; }
public void removeUpdate(DocumentEvent e){ text_event = 1; }
public void run(){}
class FontDialog extends Dialog implements ItemListener,ActionListener
{
Choice choiceFont,choiceSize,choiceStyle;
Panel panelButton,panelStyle,panelColor;
JScrollPane scrollPaneFont,scrollPaneSize,scrollPaneStyle,scrollPaneModel;
JList listFont,listSize,listStyle;
Button yes,no,foreColor,backgroundColor;
String stringName,stringSize,fontstyle;
int size,style;
Font f,textFont;
JTextArea model;
CheckboxGroup g;
Checkbox 常规,粗体,斜体,粗斜体;
FontDialog(Frame f,String s,boolean b)
{
super(f,s,b);
setBounds(60,60,400,300);
setLayout(null);
choiceFont =new Choice();
choiceFont.setBounds(30,40,120,130);
choiceSize =new Choice();
choiceSize.setBounds(180,40,120,130);
choiceStyle =new Choice();
panelButton = new Panel();
panelButton.setBounds(130,240,120,50);
model = new JTextArea("中国龙AaBbCc123?!@");
scrollPaneModel = new JScrollPane(model);
scrollPaneModel.setBounds(160,100,160,100);
GraphicsEnvironment ge =GraphicsEnvironment.getLocalGraphicsEnvironment();
String fontName[] = ge.getAvailableFontFamilyNames();
listFont = new JList(fontName);
scrollPaneFont = new JScrollPane(listFont);
scrollPaneFont.setBounds(30,300,140,160);
String stringSize[] ={"8","9","10","11","12","13","14","16","18","20","22","24","26","28","36","72"};
listSize = new JList(stringSize);
scrollPaneSize = new JScrollPane(listSize);
scrollPaneSize.setBounds(300,300,100,160);
String fontStyle[] = { "常规","粗体","斜体","粗斜体"};
listStyle = new JList(fontStyle);
scrollPaneStyle = new JScrollPane(listFont);
scrollPaneStyle.setBounds(300,300,100,160);
panelStyle = new Panel();
panelStyle.setBounds(20,80,80,120);
g = new CheckboxGroup();
常规 = new Checkbox("常规",true,g);
粗体 = new Checkbox("粗体",false,g);
斜体 = new Checkbox("斜体",false,g);
粗斜体 = new Checkbox("粗斜体",false,g);
yes=new Button("确定");
no=new Button("取消");
panelColor = new Panel();
panelColor.setBounds(30,200,80,60);
foreColor = new Button("设置字体颜色");
foreColor.setBounds(30,200,80,20);
backgroundColor = new Button("设置背景颜色");
backgroundColor.setBounds(30,220,75,20);
textFont=text.getFont();
stringName=textFont.getFontName();
size=textFont.getSize();
for(int i=0;i<stringSize.length;i++)
{
choiceSize.add(stringSize[i]);
}
for(int i=0;i<fontName.length;i++)
{
choiceFont.add(fontName[i]);
}
for(int i=0;i<fontStyle .length;i++)
{
choiceStyle.add(fontStyle [i]);
}
choiceFont.select(stringName);
choiceSize.select(""+size);
// listSize.add();
add(choiceFont);
add(choiceSize);
add(scrollPaneModel);
add(panelButton);
add(panelStyle);
panelStyle.add(choiceStyle);
panelStyle.add(常规);
panelStyle.add(粗体);
panelStyle.add(斜体);
panelStyle.add(粗斜体);
add(scrollPaneFont);
add(scrollPaneSize);
add(scrollPaneStyle);
panelButton.add(yes);
panelButton.add(no);
add(panelColor);
panelColor.add(foreColor);
panelColor.add(backgroundColor);
choiceFont.addItemListener(this);
choiceSize.addItemListener(this);
choiceStyle.addItemListener(this);
常规.addItemListener(this);
粗体.addItemListener(this);
斜体.addItemListener(this);
粗斜体.addItemListener(this);
yes.addActionListener(this);
no.addActionListener(this);
foreColor.addActionListener(this);
backgroundColor.addActionListener(this);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ setVisible(false); }
});
validate();
}
public void itemStateChanged(ItemEvent e)
{
stringName =choiceFont.getSelectedItem();
stringSize=choiceSize.getSelectedItem();
size=Integer.parseInt(stringSize);
/*
fontstyle = choiceFont.getSelectedItem();
if(fontstyle=="常规"){ style = Font.TRUETYPE_FONT; }
else if(fontstyle=="粗体"){ style = Font.BOLD; }
else if(fontstyle=="斜体"){ style = Font.ITALIC; }
else if(fontstyle=="粗斜体"){ style = Font.BOLD +Font.ITALIC ; }
*/
g.setSelectedCheckbox(常规);
Checkbox box = (Checkbox)e.getSource();
if(box==常规){ style = Font.TRUETYPE_FONT; }
else if(box==粗体){ style = Font.BOLD; }
else if(box==斜体){ style = Font.ITALIC; }
else if(box==粗斜体){ style = Font.BOLD +Font.ITALIC ; }
f =new Font(stringName,style,size);
model.setFont(f);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==yes)
{
text.setFont(f);
setVisible(false);
}
else if(e.getSource()==no)
setVisible(false);
else if(e.getSource()==foreColor)
{
Color foreColor = JColorChooser.showDialog(this,"设置字体颜色",text.getForeground());
if(foreColor!= null)
text.setForeground(foreColor);
}
else if(e.getSource()==backgroundColor)
{
Color backgroundColor = JColorChooser.showDialog(this,"设置背景颜色",text.getBackground());
if(backgroundColor != null)
text.setBackground(backgroundColor);
}
}
}
}
public class NotePad
{
public static void main(String args[])
{
FirstWindow win =new FirstWindow("新建文本文档");
}
}
class AboutDialog extends Dialog implements ActionListener
{
AboutDialog(Frame f,String s,boolean b)
{
super(f,s,b);
setBounds(200,200,300,200);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ setVisible(false); }
});
}
public void actionPerformed(ActionEvent e)
{
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -