欢迎来到天天文库
浏览记录
ID:40556961
大小:20.31 KB
页数:10页
时间:2019-08-04
《Java 简易 文本编辑器》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、importjava.awt.*;importjava.io.*;importjava.util.Calendar;importjava.awt.event.*;importjavax.swing.*;importjavax.swing.event.*;publicclassTestNotePad{publicstaticvoidmain(String[]args){TextAreaFrameframe=newTextAreaFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisibl
2、e(true);}}classTextAreaFrameextendsJFrameimplementsActionListener{publicTextAreaFrame(){setTitle("NotePad");setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);chooser.setCurrentDirectory(newFile("."));//建立一个面板,放置按钮buttonPanel=newJPanel();//斜体、粗体选框bold=newJCheckBox("Bold");buttonPanel.add(bold);bold.addA
3、ctionListener(newActionListener(){publicvoidactionPerformed(ActionEventevent){FontcurrentFont=textArea.getFont();intstyle=0;FontnewFont=null;if(bold.isSelected()){style=Font.BOLD;if(italic.isSelected()){style+=Font.ITALIC;}}else{if(italic.isSelected()){style+=Font.ITALIC;}else{style=Font.PL
4、AIN;}}newFont=currentFont.deriveFont(style);textArea.setFont(newFont);}});italic=newJCheckBox("Italic");buttonPanel.add(italic);italic.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventevent){FontcurrentFont=textArea.getFont();intstyle=0;FontnewFont=null;if(bold.isS
5、elected()){style=Font.BOLD;if(italic.isSelected()){style+=Font.ITALIC;}}else{if(italic.isSelected()){style+=Font.ITALIC;}else{style=Font.PLAIN;}}newFont=currentFont.deriveFont(style);textArea.setFont(newFont);}});//改变字体颜色colorCombo=newJComboBox();colorCombo.setEditable(true);colorCombo.addI
6、tem("Red");colorCombo.addItem("Blue");colorCombo.addItem("Black");buttonPanel.add(colorCombo);colorCombo.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventevent){intindex=colorCombo.getSelectedIndex();Stringv=(String)(colorCombo.getItemAt(index));if(v.equalsIgnoreCa
7、se("red")){textArea.setForeground(Color.RED);}elseif(v.equalsIgnoreCase("blue")){textArea.setForeground(Color.BLUE);}else{textArea.setForeground(Color.BLACK);}}});colorButton=newJButton("ChangeColor");buttonPanel.add(colorButton);colorButton.addActionLis
此文档下载收益归作者所有