📄 mytexteditor.java
字号:
else if(e.getSource() == jcopy) { //复制
try {
String str = this.text.getSelectedText();
if(str.length() != 0) {
this.strtext=str;
}
}
catch(Exception ex)
{
this.statusbar.setText("复制出错!" + ex.getMessage());
}
}
else if(e.getSource() == jpaste) { //粘贴
if(this.strtext.length() > 0){
this.text.insert(this.strtext,this.text.getCaretPosition());
this.isChanged = true;
}
}
else if(e.getSource() == jfind) { //查找
JOptionPane.showMessageDialog(frame1, "抱歉,还未实现查找~");
}
else if(e.getSource() == jreplace) { //替换
JOptionPane.showMessageDialog(frame1, "抱歉,还未实现替换~");
}
else if(e.getSource() == jselectall) { //全选
this.text.setSelectionStart(0);
this.text.setSelectionEnd(this.text.getText().length());
}
else if(e.getSource() == jdate) { //时间日期
java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//设置时间的格式
java.util.Date currentTime = new java.util.Date(); //获取当前时间
String datetime = formatter.format(currentTime).toString(); //将当前时间按格式转为字符串
text.setText(text.getText() + datetime); //将时间添加到文件末尾
this.isChanged = true;
}
else if(e.getSource() == jchangeLine) { //自动换行
if(!this.text.getLineWrap()){
this.text.setLineWrap(true);
}
else{
this.text.setLineWrap(false);
}
}
else if(e.getSource() == jfont) { //设置字体
Font cur = this.text.getFont(); //字体,初始取原有值
int type ; //字型
int size ; //字号
FontSet fsd = new FontSet(frame1, "字体设置", true);
fsd.setVisible(true);
if(fsd.flag){ //点击确定按钮,设置新的字体
switch(fsd.getFontType()){
case 0: type = Font.PLAIN; break;
case 1: type = Font.ITALIC; break;
case 2: type = Font.BOLD; break;
case 3: type = Font.ROMAN_BASELINE; break;
default: type = Font.PLAIN; break;
}
Font newfont = new Font(fsd.getFont1(), type, fsd.getFontSize());
this.text.setFont(newfont);
}
else { //取消键,则保留原字体
this.text.setFont(cur);
}
}
else if(e.getSource() == jcolor){ //设置文字颜色
textColor = JColorChooser.showDialog(frame1, "", textColor);
text.setForeground(textColor);
}
else if(e.getSource() == jabout) { //关于作者
JOptionPane.showMessageDialog(frame1, "Developed By 王敬智");
}
}
private void open() {
if(jFileChooser1.showOpenDialog(frame1) == JFileChooser.APPROVE_OPTION){
open(jFileChooser1.getSelectedFile());
}
}
private void open(File file_open){
try{
BufferedInputStream in = new BufferedInputStream(
new FileInputStream(file_open));
byte[] b = new byte[in.available()];
in.read(b, 0, b.length);
//text.append(new String(b, 0, b.length)); //在现有文件的尾部加上新打开的文件
text.setText(new String(b, 0, b.length)); //覆盖原有文件式的打开
in.close();
isSaved = true;
statusbar.setText(file_open.getName() + " Opened");
}
catch(IOException ex){
statusbar.setText("Error opening " + file_open.getName());
}
}
private void save() {
if(isSaved == false){
if(jFileChooser1.showSaveDialog(frame1) == JFileChooser.APPROVE_OPTION){
save(jFileChooser1.getSelectedFile());
}
}
else{
save(jFileChooser1.getSelectedFile());
}
}
private void save(File file_save){
try {
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(file_save));
byte[] b = (text.getText().getBytes());
out.write(b, 0, b.length);
out.close();
isSaved = true;
statusbar.setText(file_save.getName() + " saved");
}
catch(IOException ex){
statusbar.setText("Error saving " + file_save.getName());
}
}
/*-------------*
* main
*----------- */
public static void main(String[] args){
MyTextEditor MTE = new MyTextEditor();
}
}
/*----------------------*
*--- 字体设置对话框 ------*
*---------------------*/
class FontSet extends JDialog {
private String font1 = " ";
JLabel txtStr = new JLabel("字体");
JLabel txt2 = new JLabel("字型");
JLabel txt3 = new JLabel("字号");
java.awt.List list1 = new java.awt.List(6,false);
java.awt.List list2 = new java.awt.List(5,false);
java.awt.List list3 = new java.awt.List(6,false);
JButton btnOk = new JButton("确定");
JButton btnNo = new JButton("取消");
FlowLayout flowLayout1 = new FlowLayout();
boolean flag = true;
int type;
int size;
public FontSet(Frame frame, String title, boolean modal){ //对话框构造方法
this.setTitle("字体设置");
this.setSize(800,150);
this.setModal(true);
txtStr.setSize(50,100);
list1.setSize(300,150);
list1.setMultipleMode(false); //单选
txt2.setSize(50,100);
list2.setSize(200,150);
txt3.setSize(50,100);
list3.setSize(100,150);
btnOk.setMnemonic('Y');
btnOk.setSize(100,20);
btnOk.addActionListener(new btnOk_actionAdapter(this));
btnNo.setMnemonic('N');
btnNo.setSize(100,20);
btnNo.addActionListener(new btnNo_actionAdapter(this));
btnNo.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent eok){
setVisible(false);
}
});
///////////// Font //////////////////////////////////
GraphicsEnvironment gl = GraphicsEnvironment.getLocalGraphicsEnvironment();
String fontName[] = gl.getAvailableFontFamilyNames(); //获取当前可使用的字体
//String fontName[] = {"中文宋体", "中文楷体", "黑体"};
for(int i=0; i<fontName.length; i++){ //将可用的字体加到list1列表框中
list1.add(fontName[i]);
}
String fontx[] = {"常规","斜体","粗体","粗斜体"};
for(int i=0; i<fontx.length; i++){ //将可用的字型加到list2列表框中
list2.add(fontx[i]);
}
int fontSize[] = {10,12,14,16,18,20,22,24,26,30,32,34,36,38,40};
for(int i=0;i<fontSize.length;i++){ //将可用的字号加到list3列表框中
list3.add(Integer.toString(fontSize[i])); //添加时将整型的字号转为String类
}
list1.select(0); //设置初始默认选择
list2.select(0);
list3.select(8);
//// 将各组件添加进来 //////
this.getContentPane().setLayout(flowLayout1);
this.getContentPane().add(txtStr,flowLayout1);
this.getContentPane().add(list1,flowLayout1);
this.getContentPane().add(txt2,flowLayout1);
this.getContentPane().add(list2,flowLayout1);
this.getContentPane().add(txt3,flowLayout1);
this.getContentPane().add(list3,flowLayout1);
this.getContentPane().add(btnOk,flowLayout1);
this.getContentPane().add(btnNo,flowLayout1);
}
public FontSet(){ //无参数构造方法
this(null,"",true);
}
void btnOk_actionPerformed(ActionEvent e){ //确认键事件响应
this.flag = true;
this.font1 = this.list1.getSelectedItem();
this.type = this.list2.getSelectedIndex();
this.size = Integer.parseInt(this.list3.getSelectedItem());
this.dispose();
}
void btnNo_actionPerformed(ActionEvent e){ //取消键事件响应
this.flag = false;
this.dispose();
}
public String getFont1(){ //获得字体
return this.font1;
}
public int getFontType(){ //获得字型
return this.type;
}
public int getFontSize(){ //获得字号
return this.size;
}
class btnOk_actionAdapter implements java.awt.event.ActionListener{
FontSet adaptee;
btnOk_actionAdapter(FontSet adaptee){
this.adaptee = adaptee; //设置监听器
}
public void actionPerformed(ActionEvent e){
adaptee.btnOk_actionPerformed(e); //调用事件响应方法
}
}
class btnNo_actionAdapter implements java.awt.event.ActionListener{
FontSet adaptee;
btnNo_actionAdapter(FontSet adaptee){
this.adaptee = adaptee; //设置监听器
}
public void actionPerformed(ActionEvent e){
adaptee.btnNo_actionPerformed(e); //调用事件响应方法
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -