📄 notepad.java
字号:
if(gt < 2 || gt >= totalLine) {
if(gt < 2)
editArea.setCaretPosition(0);
else
editArea.setCaretPosition(s.length());
}
else
editArea.setCaretPosition(lineNumber[gt-2] + 1);
gotoDialog.dispose();
}
});
JButton cancelButton=new JButton("取消");
cancelButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
gotoDialog.dispose();
}
});
Container con=gotoDialog.getContentPane();
con.setLayout(new FlowLayout());
con.add(gotoLabel);
con.add(linenum);
con.add(okButton);
con.add(cancelButton);
gotoDialog.setSize(200,100);
gotoDialog.setResizable(false);
gotoDialog.setLocation(300,280);
gotoDialog.setVisible(true);
}//"转到"处理结束
//插入时间日期
else if(e.getSource()==editMenu_TimeDate||e.getSource()==timeButton)
{
editArea.requestFocus();
SimpleDateFormat currentDateTime = new SimpleDateFormat("HH:mm yyyy-MM-dd");
editArea.insert(currentDateTime.format(new Date()),editArea.getCaretPosition());
}
//全选
else if(e.getSource()==popupMenu_SelectAll||e.getSource()==editMenu_SelectAll)
{
editArea.selectAll();
}
//自动换行
else if(e.getSource()==formatMenu_LineWrap)
{
if(formatMenu_LineWrap.getState())
{
editArea.setLineWrap(true);
}
else
editArea.setLineWrap(false);
}
//字体设置
else if(e.getSource()==formatMenu_Font||e.getSource()==fontButton)
{
editArea.requestFocus();
new MyFont();
}
//设置字体颜色(前景色)
else if(e.getSource()==formatMenu_Color_FgColor||e.getSource()==fgcolorButton)
{
editArea.requestFocus();
Color color=JColorChooser.showDialog(this,"更改字体颜色",Color.black);
if(color!=null)
{
editArea.setForeground(color);
}
else
return;
}
//设置编辑区背景颜色
else if(e.getSource()==formatMenu_Color_BgColor||e.getSource()==bgcolorButton)
{
editArea.requestFocus();
Color color=JColorChooser.showDialog(this,"更改背景颜色",Color.white);
if(color!=null)
{
editArea.setBackground(color);
}
else
return;
}
//设置状态栏可见性
else if(e.getSource()==viewMenu_Status)
{
if(viewMenu_Status.getState())
statusLabel.setVisible(true);
else
statusLabel.setVisible(false);
}
//帮助主题
else if(e.getSource()==helpMenu_HelpTopics||e.getSource()==helpButton)
{
editArea.requestFocus();
String helpTopicsStr=new String("<center><br>欢迎访问GXBINBIN的个人博客<br>"+
"<a href=http://gxbinbin.blog.edu.cn target=_blank>"+
"http://gxbinbin.blog.edu.cn</a><br><br><br>"+
"厦大软件学院04级 黄斌 2006.5.20.</center>");
JEditorPane editPane=new JEditorPane("text/html",helpTopicsStr);
editPane.setEditable(false);
editPane.addHyperlinkListener(new HyperlinkListener(){
public void hyperlinkUpdate(HyperlinkEvent hle){
if (hle.getEventType()==HyperlinkEvent.EventType.ACTIVATED)
{
try
{
Process process = Runtime.getRuntime().exec("cmd.exe /c start http://gxbinbin.blog.edu.cn");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
});
JFrame helpTopicsFrame=new JFrame("帮助主题");
helpTopicsFrame.setContentPane(new JScrollPane(editPane));
helpTopicsFrame.setSize(250,200);
helpTopicsFrame.setLocation(300,300);
helpTopicsFrame.setVisible(true);
}
//关于
else if(e.getSource()==helpMenu_About){
JOptionPane.showMessageDialog(this,
"*****************************************************\n"+
"| 程序作者:厦门大学软件学院软件工程专业04级 黄斌 |\n"+
"| 课 程:《JAVA面象对象程序设计》 |\n"+
"| 设计时间:2006年05月20日 |\n"+
"| 联系电话:0596-6286991 |\n"+
"| 作者 QQ:47116642 |\n"+
"| 邮箱地址:huangbin180@163.com |\n"+
"| 欢迎大家与我交流,让我们一起共同提高!^_^ |\n"+
"*****************************************************\n"
,"关于记事本",JOptionPane.INFORMATION_MESSAGE);
}
//工具栏"粗体"按钮事件处理
else if(e.getSource()==boldButton)
{
editArea.requestFocus();
Font tempFont=editArea.getFont();
if(editArea.getFont().getStyle()==Font.PLAIN){
tempFont=new Font(editArea.getFont().getFontName(),Font.BOLD,editArea.getFont().getSize());
}else if(editArea.getFont().getStyle()==Font.ITALIC){
tempFont=new Font(editArea.getFont().getFontName(),Font.BOLD+Font.ITALIC,editArea.getFont().getSize());
}else if(editArea.getFont().getStyle()==Font.BOLD){
tempFont=new Font(editArea.getFont().getFontName(),Font.PLAIN,editArea.getFont().getSize());
}else if(editArea.getFont().getStyle()==(Font.BOLD+Font.ITALIC)){
tempFont=new Font(editArea.getFont().getFontName(),Font.ITALIC,editArea.getFont().getSize());
}
editArea.setFont(tempFont);
}
//工具栏"斜体"按钮事件处理
else if(e.getSource()==italicButton)
{
editArea.requestFocus();
Font tempFont=editArea.getFont();
if(editArea.getFont().getStyle()==Font.PLAIN){
tempFont=new Font(editArea.getFont().getFontName(),Font.ITALIC,editArea.getFont().getSize());
}else if(editArea.getFont().getStyle()==Font.ITALIC){
tempFont=new Font(editArea.getFont().getFontName(),Font.PLAIN,editArea.getFont().getSize());
}else if(editArea.getFont().getStyle()==Font.BOLD){
tempFont=new Font(editArea.getFont().getFontName(),Font.BOLD+Font.ITALIC,editArea.getFont().getSize());
}else if(editArea.getFont().getStyle()==(Font.BOLD+Font.ITALIC)){
tempFont=new Font(editArea.getFont().getFontName(),Font.BOLD,editArea.getFont().getSize());
}
editArea.setFont(tempFont);
}
}/*方法actionPerformed()结束*/
//实现DocumentListener接口中的方法
public void removeUpdate(DocumentEvent e) {
editMenu_Undo.setEnabled(true);
popupMenu_Undo.setEnabled(true);
undoButton.setEnabled(true);
}
public void insertUpdate(DocumentEvent e) {
editMenu_Undo.setEnabled(true);
popupMenu_Undo.setEnabled(true);
undoButton.setEnabled(true);
}
public void changedUpdate(DocumentEvent e) {
editMenu_Undo.setEnabled(true);
popupMenu_Undo.setEnabled(true);
undoButton.setEnabled(true);
}
//End of DocumentListener
//main 方法
public static void main(String args[])
{
//设置界面为WindowsLookAndFeel
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch(Exception e) {
}
Notepad frame = new Notepad();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}/*main 方法结束*/
//实现了接口UndoableListener的类UndoHandler
class UndoHandler implements UndoableEditListener {
public void undoableEditHappened(UndoableEditEvent uee){
undo.addEdit(uee.getEdit());
}
}/*End of class UndoHandler */
//用于设置字体的类MyFont
class MyFont implements ActionListener{
final JDialog fontDialog;
final JTextField tfFont,tfSize,tfStyle;
final int fontStyleConst[]={Font.PLAIN,Font.BOLD,Font.ITALIC,Font.BOLD+Font.ITALIC};
final JList listStyle,listFont,listSize;
JLabel sample;
//构造函数MyFont
public MyFont(){
fontDialog = new JDialog(Notepad.this,"字体设置",true);
Container con=fontDialog.getContentPane();
con.setLayout(new FlowLayout(FlowLayout.LEFT));
Font currentFont=editArea.getFont();
JLabel lblFont=new JLabel("字体(F):");
lblFont.setPreferredSize(new Dimension(100,20));
JLabel lblStyle=new JLabel("字形(Y):");
lblStyle.setPreferredSize(new Dimension(100,20));
JLabel lblSize=new JLabel("大小(S):");
lblSize.setPreferredSize(new Dimension(100,20));
tfFont=new JTextField(15);
tfFont.setText(currentFont.getFontName());
tfFont.selectAll();
tfFont.setPreferredSize(new Dimension(200,20));
tfStyle=new JTextField(15);
if(currentFont.getStyle()==Font.PLAIN)
tfStyle.setText("常规");
else if(currentFont.getStyle()==Font.BOLD)
tfStyle.setText("粗体");
else if(currentFont.getStyle()==Font.ITALIC)
tfStyle.setText("斜体");
else if(currentFont.getStyle()==(Font.BOLD+Font.ITALIC))
tfStyle.setText("粗斜体");
tfFont.selectAll();
tfStyle.setPreferredSize(new Dimension(200,20));
tfSize=new JTextField(8);
tfSize.setText(currentFont.getSize()+"");
tfSize.selectAll();
tfSize.setPreferredSize(new Dimension(200,20));
final String fontStyle[]={"常规","粗体","斜体","粗斜体"};
listStyle=new JList(fontStyle);
GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
final String fontName[]=ge.getAvailableFontFamilyNames();
int defaultFontIndex=0;
for(int i=0;i<fontName.length;i++)
{
if(fontName[i].equals(currentFont.getFontName()))
{
defaultFontIndex=i;
break;
}
}
listFont=new JList(fontName);
listFont.setSelectedIndex(defaultFontIndex);
listFont.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
listFont.setVisibleRowCount(7);
listFont.setFixedCellWidth(82);
listFont.setFixedCellHeight(20);
listFont.addListSelectionListener(
new ListSelectionListener(){
public void valueChanged(ListSelectionEvent event){
tfFont.setText(fontName[listFont.getSelectedIndex()]);
tfFont.requestFocus();
tfFont.selectAll();
updateSample();
}
}
);
listStyle.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
if(currentFont.getStyle()==Font.PLAIN)
listStyle.setSelectedIndex(0);
else if(currentFont.getStyle()==Font.BOLD)
listStyle.setSelectedIndex(1);
else if(currentFont.getStyle()==Font.ITALIC)
listStyle.setSelectedIndex(2);
else if(currentFont.getStyle()==(Font.BOLD+Font.ITALIC))
listStyle.setSelectedIndex(3);
listStyle.setVisibleRowCount(7);
listStyle.setFixedCellWidth(99);
listStyle.setFixedCellHeight(20);
listStyle.addListSelectionListener(
new ListSelectionListener(){
public void valueChanged(ListSelectionEvent event){
tfStyle.setText(fontStyle[listStyle.getSelectedIndex()]);
tfStyle.requestFocus();
tfStyle.selectAll();
updateSample();
}
}
);
final String fontSize[]={"8","9","10","11","12","14","16","18","20","22","24","26","28","36","48","72"};
listSize=new JList(fontSize);
int defaultFontSizeIndex=0;
for(int i=0;i<fontSize.length;i++)
{
if(fontSize[i].equals(currentFont.getSize()+""))
{
defaultFontSizeIndex=i;
break;
}
}
listSize.setSelectedIndex(defaultFontSizeIndex);
listSize.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
listSize.setVisibleRowCount(7);
listSize.setFixedCellWidth(39);
listSize.setFixedCellHeight(20);
listSize.addListSelectionListener(
new ListSelectionListener(){
public void valueChanged(ListSelectionEvent event){
tfSize.setText(fontSize[listSize.getSelectedIndex()]);
tfSize.requestFocus();
tfSize.selectAll();
updateSample();
}
}
);
fontOkButton=new JButton("确定");
fontOkButton.addActionListener(this);
JButton cancelButton=new JButton("取消");
cancelButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
fontDialog.dispose();
}
});
sample=new JLabel("黄斌的记事本-HuangBin's Notepad");
sample.setHorizontalAlignment(SwingConstants.CENTER);
sample.setPreferredSize(new Dimension(300,50));
JPanel samplePanel=new JPanel();
samplePanel.setBorder(BorderFactory.createTitledBorder("示例"));
samplePanel.add(sample);
con.add(lblFont);
con.add(lblStyle);
con.add(lblSize);
con.add(tfFont);
con.add(tfStyle);
con.add(tfSize);
con.add(fontOkButton);
con.add(new JScrollPane(listFont));
con.add(new JScrollPane(listStyle));
con.add(new JScrollPane(listSize));
con.add(cancelButton);
con.add(samplePanel);
updateSample();
fontDialog.setSize(350,340);
fontDialog.setLocation(200,200);
fontDialog.setResizable(false);
fontDialog.setVisible(true);
}//构造函数结束
//更新示例显示的字体和风格大小等
public void updateSample(){
Font sampleFont=new Font(tfFont.getText(),fontStyleConst[listStyle.getSelectedIndex()],Integer.parseInt(tfSize.getText()));
sample.setFont(sampleFont);
}//End method updateSample
//设置文本编辑区的字体
public void actionPerformed(ActionEvent e){
if(e.getSource()==fontOkButton){
Font tempFont=new Font(tfFont.getText(),fontStyleConst[listStyle.getSelectedIndex()],Integer.parseInt(tfSize.getText()));
editArea.setFont(tempFont);
fontDialog.dispose();
}
}//End method actionPerformed
}/*End of class MyFont*/
}
/*
*小结:
*
* 经过编写这个记事本程序,使得自己对"JAVA面向对象程序设计"这门课有了更深刻的理解和认识,
*提高了自己对JAVA编程的兴趣。
* 虽然老师只要求实现记事本中的一部分功能,但是我在写这个程序过程中,对其它尚未要求实现
*的功能不断有了新的想法,所以就决定将这些功能全部实现了。
* 在编写这个程序之前,我到图书馆查找了很多资料,并做了很多笔记,最让我喜爱的两本书就是
*《深入浅出Java Swing程序设计》和《JFC核心编程》,从这两本书中我对Java Swing和JFC有了更深
*刻的了解,认识到了Java Swing功能的强大和JFC的庞大!并且被JAVA的魅力给吸引住了。同时,我深深
*地认识到自己知识的不足,短短的一个学期学到的Java知识是很有限的,只有在平时多练多思考才会有
*更大的进步!在以后的学习中,我一定会不断学习,不断提高自己的编程经验和能力!
*
* 同时感谢老师,感谢助教一个学期来对我们的教诲!
*
* 厦门大学软件学院软件工程专业2004级
*
* 黄斌 04369077
*
* 2006.5.22.
**/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -