当老师叫我们帮他做事,比如文件内内容再分类,我们就可以建个面板,里面有各要导入文件按钮,先把分类内容copy下,再点按钮导入进txt文件就行啦。

以下为java代码

package classification;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import layout.tablelayout;
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import java.io.bufferedwriter;
import java.io.file;
import java.io.filenotfoundexception;
import java.io.filereader;
import java.io.filewriter;
import java.io.ioexception;
import java.io.reader;
import java.io.writer;
import java.io.bufferedreader;
import java.io.fileoutputstream;
import java.io.outputstreamwriter;
import java.awt.toolkit;
import java.awt.datatransfer.clipboard;
import java.awt.datatransfer.dataflavor;
import java.awt.datatransfer.stringselection;
import java.awt.datatransfer.transferable;


public class classification extends jframe implements actionlistener{
    public classification(){
        
        //  创建一个窗口。
                frame frame = new frame("classification");
                frame.setbounds (1300 , 100, 200, 450);
                
                //设置背景颜色。
                frame.setbackground(color.black);
                
                //在这个窗口创建一个网格。    
                double size[][] =
                    {
                            //0              ,1               ,2               ,3               
                            {tablelayout.fill,tablelayout.fill}, //
                            {tablelayout.fill,tablelayout.fill,tablelayout.fill,tablelayout.fill,tablelayout.fill} //
                    };
                tablelayout layout = new tablelayout(size);
                frame.setlayout(layout);
                
                //创建一些按钮.
                
                //编辑字体
                font  b = new font("宋体",font.plain,30);
                
                //写入文本数组。
                for (int i = 0;i < button.length;i++)
                {
                    button[i] = new button(button[i]);
                }
                
                //应用字体。
                //按钮字体。
                for(int i = 0; i <= 9; i++) {
                    button[i].setfont(b);
                    button[i].setforeground(color.black);
                }
                
                //添加按钮
                frame.add(button[0],"0,0,0,0"); //1
                frame.add(button[1],"1,0,1,0"); //2
                frame.add(button[2],"0,1,0,1"); //3
                frame.add(button[3],"1,1,1,1"); //4
                frame.add(button[4],"0,2,0,2"); //5
                frame.add(button[5],"1,2,1,2"); //6
                frame.add(button[6],"0,3,0,3"); //7
                frame.add(button[7],"1,3,1,3"); //8
                frame.add(button[8],"0,4,0,4"); //9
                frame.add(button[9],"1,4,1,4"); //撤回

                for(int i = 0; i <= 9; i++) {
                    button[i].addactionlistener(this) ;
                }
                
                //编辑按钮
                for(int i = 0; i <= 9; i++) {
                    button[i].setfont(b);
                    button[i].setbackground(color.green);
                }
                
                //添加窗口关闭。
                frame.addwindowlistener 
                (new windowadapter()
                    {
                        public void windowclosing (windowevent e)
                        {
                            system.exit (0);
                        }
                    }
                );
                
                //窗口置顶。
                 frame.setalwaysontop(true);
                 
                //显示窗口。
                frame.show();
                
        }
    string button[] = {"1","2","3","4","5","6","7","8","9",""};
    button button[] = new button[button.length];
    
    public static void main(string[] args) {
        new classification();
    }
    
    //路径
    string[] s = {
            "d:\homework\chapter1.txt",
            "d:\homework\chapter2.txt",
            "d:\homework\chapter3.txt",
            "d:\homework\chapter4.txt",
            "d:\homework\chapter5.txt",
            "d:\homework\chapter6.txt",
            "d:\homework\chapter7.txt",
            "d:\homework\chapter8.txt",
            "d:\homework\chapter9.txt"
    };
    
    //文件读入粘贴板内容
    public static void method2(string filename, string content) {
        filewriter writer = null;
        try {
            writer = new filewriter(filename,true);
            clipboard sysclip = toolkit.getdefaulttoolkit().getsystemclipboard();
            // 获取剪切板中的内容
            transferable cliptf = sysclip.getcontents(null);
            if (cliptf != null) { 
                // 检查内容是否是文本类型
                if (cliptf.isdataflavorsupported(dataflavor.stringflavor)) {
                    try {
                        content = (string) cliptf
                                .gettransferdata(dataflavor.stringflavor);
                        writer.write(content);
                    } catch (exception e) {
                        e.printstacktrace();
                    }
                }
            }

        }catch(ioexception e) {
            e.printstacktrace();
        }
        finally {
            try {
                if(writer != null) {
                    writer.close();
                }
            }catch(ioexception e) {
                e.printstacktrace();
            }
        }
    }
     //按钮响应
    public void actionperformed(actionevent e) {
          if(e.getsource() == button[0])
            try {
                method2(s[0],"");
            } catch (exception e1) {
                // todo auto-generated catch block
                e1.printstacktrace();
            }
          if(e.getsource() == button[1])
                try {
                    method2(s[1],"");
                } catch (exception e1) {
                    // todo auto-generated catch block
                    e1.printstacktrace();
                }
          if(e.getsource() == button[2])
                try {
                    method2(s[2], "");
                } catch (exception e1) {
                    // todo auto-generated catch block
                    e1.printstacktrace();
                }
          if(e.getsource() == button[3])
                try {
                    method2(s[3],"");
                } catch (exception e1) {
                    // todo auto-generated catch block
                    e1.printstacktrace();
                }
          if(e.getsource() == button[4])
                try {
                    method2(s[4],"");
                } catch (exception e1) {
                    // todo auto-generated catch block
                    e1.printstacktrace();
                }
          if(e.getsource() == button[5])
                try {
                    method2(s[5],"");
                } catch (exception e1) {
                    // todo auto-generated catch block
                    e1.printstacktrace();
                }
          if(e.getsource() == button[6])
                try {
                    method2(s[6],"");
                } catch (exception e1) {
                    // todo auto-generated catch block
                    e1.printstacktrace();
                }
          if(e.getsource() == button[7])
                try {
                    method2(s[7],"");
                } catch (exception e1) {
                    // todo auto-generated catch block
                    e1.printstacktrace();
                }
          if(e.getsource() == button[8])
                try {
                    method2(s[8],"");
                } catch (exception e1) {
                    // todo auto-generated catch block
                    e1.printstacktrace();
                }
    }
    
}

 

分类: 未分类

0 条评论

发表回复

Avatar placeholder

您的邮箱地址不会被公开。 必填项已用 * 标注