码迷,mamicode.com
首页 > 编程语言 > 详细

Java + eclipse + awt 编写锻炼打字小软件(未完成)

时间:2015-07-03 23:23:10      阅读:374      评论:0      收藏:0      [点我收藏+]

标签:

进入前界面:

import java.awt.*;

public class Welcome extends JFrame implements Runnable{
    Thread t;
    private LoginGraphics loginGraphics_IL;
    
    public static void main(String[] args) {
        Welcome inst = new Welcome();
        inst.setVisible(true);
    }

    public Welcome() {
        super();
        t=new Thread(this);
        initGUI();
    }
    
    private void initGUI() {
        try {
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            setUndecorated(true);
//计算屏幕大小
            Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
//添加图形
            setSize(443, 155);
//将窗口居中
            setLocation((screen.width-getWidth())/2,(screen.height-getHeight())/2);
            {
                loginGraphics_IL = new LoginGraphics();
                add(loginGraphics_IL);
                loginGraphics_IL.setBorder(BorderFactory.createCompoundBorder(
                    null,
                    null));
                loginGraphics_IL.setBackground(new java.awt.Color(192,192,192));
            }
            t.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void run(){
        try{
            Thread.sleep(3000);
            new MainFrame();
            this.setVisible(false);
            t.interrupt();
        }catch(InterruptedException e){}
    }
}

主界面:

import java.awt.*;

public class MainFrame extends JDesktopPane implements ActionListener{ 
    Dimension screen;
    Toolkit toolkit=Toolkit.getDefaultToolkit();
    private Icon ico;
    
    JFrame frame;
    Tool tool;
    public MainFrame(){ 
        JFrame.setDefaultLookAndFeelDecorated(true);
        frame = new JFrame(); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        Container container = frame.getContentPane(); 
        container.add(this, BorderLayout.CENTER); 
        this.setPreferredSize(new java.awt.Dimension(800, 550)); 
        frame.setTitle("TypingMaster");

        frame.setResizable(false);
        screen=toolkit.getScreenSize();
        frame.setLocation((screen.width-800)/2,screen.height/2-600/2);
        
        //添加工具条.
        tool=new Tool(this);
        this.add(tool,1);
        
        frame.pack(); 
        frame.setVisible(true);
    } 
    
    //  实现ActionListener接口的方法
    public void actionPerformed(ActionEvent e){
        
    }
    
    private void showFrame(){
        Hint hint =new Hint(tool);
        this.add(hint,2);
    }
}

 

tool(左侧栏)

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

/**
 * 左边的工具栏
 */
public class Tool extends JInternalFrame implements ActionListener{
    
    MainFrame parent;
    
    private JButton btnJP;
    private JButton btnExit;
    private JPanel p;
    private ImageIcon image;
    
    public Tool(MainFrame parent){
        this.parent=parent;
        
        //按下键盘练习
        p=new JPanel();
        btnJP=new JButton("\u952E\u76D8\u7EC3\u4E60",image);
        btnJP.setFont(new Font("x",Font.PLAIN,12));
        btnJP.setMnemonic(‘1‘);
        btnJP.addActionListener(this);
    
        //按下退出系统
        btnExit=new JButton("\u9000\u51FA\u7CFB\u7EDF",image);
        btnExit.setFont(new Font("x",Font.PLAIN,12));
        btnExit.setMnemonic(‘Q‘);
        btnExit.addActionListener(this);
        
        p.setLayout(new FlowLayout());
        p.add(btnJP);
        p.add(btnExit);
        getContentPane().add(this.p);
        
        
        setLayer(1); 
        setBounds(1,0,140,121); 
        setPreferredSize(new Dimension(140, 550)); 
        ((javax.swing.plaf.basic.BasicInternalFrameUI) this.getUI()).setNorthPane(null);
        this.putClientProperty("JInternalFrame.isPalette", Boolean.TRUE); 
        pack();
        setVisible(true); 
    }
    //实现ActionListener接口的方法
    public void actionPerformed(ActionEvent e){
        if(PublicData.onChoice==true){
            if(e.getSource()==btnJP){
                PublicData.onChoice=false;
                PublicData.Lang="English";
                PublicData.Text="Text/English_"+(int)(Math.random()*5+1)+".txt";
                PublicData.choice=1;
                showFrame();
            }
            
            if(e.getSource()==btnExit){
                int i=JOptionPane.showConfirmDialog(this.parent,"你真的要退出吗?","疯狂打字",
                        JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE);
                if(i==0){
                    System.exit(0);
                }
            }
        }
        
    }
    
    private void showFrame(){
        Hint hint =new Hint(this);
        parent.add(hint,2);
    }
}

LoginGraphics:

import java.awt.*;

public class LoginGraphics extends JPanel{
    public LoginGraphics() {
    }
    public void  paint(Graphics g){
        paintComponent(g);

        g.setFont(new Font("Dialog",Font.BOLD+Font.ITALIC,45));
        for(int i=1;i<=5;i++){
            g.setColor(new Color(255-(i*50),255-(i*50),255-(i*50)));
            g.drawLine(0,36+i,293,36+i);
        }      
        g.setColor(new Color(255,255,255));
        g.drawString("Typing Master",62,85);
        g.setColor(new Color(34,80,23));
        g.drawString("Typing Master",60,85);

        for(int i=1;i<=5;i++){
            g.setColor(new Color(255-(i*50),255-(i*50),255-(i*50)));
            g.drawLine(60,100-i,443,100-i);
        }

    }
}

编辑键区的字符与键盘对照:

import javax.swing.*;
/**
 * 编辑键区的字符与键盘对照
 */
public class Keyset extends JPanel{
    public Keyset() {
    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        String str=new String(PublicData.key);
        int keyTop=0;//字符在键盘上的纵坐标
        int keyLeft=0;//字符在键盘上的横坐标
        boolean leftShift=false;//是否是左边的Shift
        boolean rightShift=false;//是否是右边的Shift
        boolean yShift=false;//是否要用Shift
        boolean ySpace=false;//是否是空格
        String shift=new String("~!@#$%^&*()_+QWERTYUIOP{}|ASDFGHJKL:\"ZXCVBNM<>?");
        String noShift=new String("`1234567890-=qwertyuiop[]\\asdfghjkl;‘zxcvbnm,./");
        int LEFT=52;
        int TOP=20;
        int lTemp=LEFT;
        int tTemp=TOP;
        if(shift.lastIndexOf(str)<=46){//计算是不是要用上Shift
            if((shift.lastIndexOf(str)>=0 && shift.lastIndexOf(str)<=5) || 
                (shift.lastIndexOf(str)>=13 && shift.lastIndexOf(str)<=17) || 
                (shift.lastIndexOf(str)>=26 && shift.lastIndexOf(str)<=30) || 
                (shift.lastIndexOf(str)>=37 && shift.lastIndexOf(str)<=41)){//计算字符在不在左手区
                yShift=true;
                rightShift=true;
                if(shift.lastIndexOf(str)>=0 && shift.lastIndexOf(str)<=5){//计算字符在该区的位置
                    keyTop=1;
                    for(int i=0;i<=5;i++){
                        if(str.equals(shift.substring(i, i+1))){
                            keyLeft=i;
                            System.out.println(shift.substring(i, i+1));
                        }
                    }
                }
                if(shift.lastIndexOf(str)>=13 && shift.lastIndexOf(str)<=17){//计算字符在该区的位置
                    keyTop=2;
                    for(int i=13;i<=17;i++){
                        if(str.equals(shift.substring(i, i+1))){
                            keyLeft=i;
                        }
                    }
                }
                if(shift.lastIndexOf(str)>=26 && shift.lastIndexOf(str)<=30){//计算字符在该区的位置
                    keyTop=3;
                    for(int i=26;i<=30;i++){
                        if(str.equals(shift.substring(i, i+1))){
                            keyLeft=i;
                        }
                    }
                }
                if(shift.lastIndexOf(str)>=37 && shift.lastIndexOf(str)<=41){//计算字符在该区的位置
                    keyTop=4;
                    for(int i=37;i<=41;i++){
                        if(str.equals(shift.substring(i, i+1))){
                            keyLeft=i;
                        }
                    }
                }
            }
            if((shift.lastIndexOf(str)>=6 && shift.lastIndexOf(str)<=12) || 
                (shift.lastIndexOf(str)>=18 && shift.lastIndexOf(str)<=25) || 
                (shift.lastIndexOf(str)>=31 && shift.lastIndexOf(str)<=36) || 
                (shift.lastIndexOf(str)>=42 && shift.lastIndexOf(str)<=46)){//计算字符在不在右手区
                yShift=true;
                leftShift=true;
                if(shift.lastIndexOf(str)>=6 && shift.lastIndexOf(str)<=12){
                    keyTop=1;
                    for(int i=6;i<=12;i++){
                        if(str.equals(shift.substring(i, i+1))){
                            keyLeft=i;
                        }
                    }
                }
                if(shift.lastIndexOf(str)>=18 && shift.lastIndexOf(str)<=25){
                    keyTop=2;
                    for(int i=18;i<=25;i++){
                        if(str.equals(shift.substring(i, i+1))){
                            keyLeft=i;
                        }
                    }
                }
                if(shift.lastIndexOf(str)>=31 && shift.lastIndexOf(str)<=36){
                    keyTop=3;
                    for(int i=31;i<=36;i++){
                        if(str.equals(shift.substring(i, i+1))){
                            keyLeft=i;
                        }
                    }
                }
                if(shift.lastIndexOf(str)>=42 && shift.lastIndexOf(str)<=46){
                    keyTop=4;
                    for(int i=42;i<=46;i++){
                        if(str.equals(shift.substring(i, i+1))){
                            keyLeft=i;
                        }
                    }
                }
            }
        }
        if(noShift.lastIndexOf(str)<=46){
            if(noShift.lastIndexOf(str)>=0 && noShift.lastIndexOf(str)<=12){
                keyTop=1;
                for(int i=0;i<=12;i++){
                    if(str.equals(noShift.substring(i,i+1))){
                        keyLeft=i;
                    }
                }
            }
            if(noShift.lastIndexOf(str)>=13 && noShift.lastIndexOf(str)<=25){
                keyTop=2;
                for(int i=13;i<=25;i++){
                    if(str.equals(noShift.substring(i,i+1))){
                        keyLeft=i;
                    }
                }
            }
            if(noShift.lastIndexOf(str)>=26 && noShift.lastIndexOf(str)<=36){
                keyTop=3;
                for(int i=26;i<=36;i++){
                    if(str.equals(noShift.substring(i,i+1))){
                        keyLeft=i;
                    }
                }
            }
            if(noShift.lastIndexOf(str)>=37 && noShift.lastIndexOf(str)<=46){
                keyTop=4;
                for(int i=37;i<=46;i++){
                    if(str.equals(noShift.substring(i,i+1))){
                        keyLeft=i;
                    }
                }
            }
        }
        if(str.equals(" ")){
            ySpace=true;
        }

        //绘制编辑键区图形//
        g.setColor(Color.black);
        for(int i=0;i<=12;i++){
            if(keyTop==1 && keyLeft==i){
                g.setColor(Color.gray);
                g.fillRect(LEFT,TOP,35,35);
            }
            g.setColor(Color.black);
            g.draw3DRect(LEFT,TOP,35,35,true);
            LEFT=LEFT+37;
        }
        //←键
        g.draw3DRect(LEFT,TOP,70,35,true);
        
        int sum=12;
        LEFT=lTemp;
        TOP=TOP+37;
        //Tab键
        g.draw3DRect(LEFT,TOP,54,35,true);
        LEFT=LEFT+56;

        for(int i=1;i<=12;i++)
        {
            if(keyTop==2 && keyLeft==i+sum){
                g.setColor(Color.gray);
                g.fillRect(LEFT,TOP,35,35);
            }
            g.setColor(Color.black);
            g.draw3DRect(LEFT,TOP,35,35,true);
            LEFT=LEFT+37;
        }
        //|\键
        sum=sum+12;
        if(keyTop==2 && keyLeft==sum+1){
            g.setColor(Color.gray);
            g.fillRect(LEFT,TOP,51,35);
        }
        g.setColor(Color.black);
        g.draw3DRect(LEFT,TOP,51,35,true);
        
        sum=sum+1;
        LEFT=lTemp;
        TOP=TOP+37;
        //Caps Lock键
        g.draw3DRect(LEFT,TOP,65,35,true);
        LEFT=LEFT+67;

        for(int i=1;i<=11;i++){
            if(keyTop==3 && keyLeft==sum+i){
                g.setColor(Color.gray);
                g.fillRect(LEFT,TOP,35,35);
            }
            g.setColor(Color.black);
            g.draw3DRect(LEFT,TOP,35,35,true);
            LEFT=LEFT+37;
        }
        //Enter键
        g.draw3DRect(LEFT,TOP,77,35,true);
        sum=sum+11;
        LEFT=lTemp;
        TOP=TOP+37;
        //左Shift键
        if(leftShift==true){
            g.setColor(Color.gray);
            g.fillRect(LEFT,TOP,82,35);
        }
        g.setColor(Color.black);
        g.draw3DRect(LEFT,TOP,82,35,true);

        LEFT=LEFT+84;
        for(int i=1;i<=10;i++){
            if(keyTop==4 && keyLeft==sum+i){
                g.setColor(Color.gray);
                g.fillRect(LEFT,TOP,35,35);
            }
            g.setColor(Color.black);
            g.draw3DRect(LEFT,TOP,35,35,true);
            LEFT=LEFT+37;
        }
        //右Shift键
        if(rightShift==true){
            g.setColor(Color.gray);
            g.fillRect(LEFT,TOP,97,35);
        }
        g.setColor(Color.black);
        g.draw3DRect(LEFT,TOP,97,35,true);
        
        //左Ctrl键
        LEFT=lTemp;
        TOP=TOP+37;
        g.draw3DRect(LEFT,TOP,53,35,true);
        LEFT=LEFT+55;
        //win键
        g.draw3DRect(LEFT,TOP,44,35,true);
        LEFT=LEFT+46;
        //左Alt键
        g.draw3DRect(LEFT,TOP,44,35,true);
        LEFT=LEFT+46;
        //Space键
        if(ySpace==true){
            g.setColor(Color.gray);
            g.fillRect(LEFT,TOP,211,35);
        }
        g.setColor(Color.black);
        g.draw3DRect(LEFT,TOP,211,35,true);
        //右Alt键
        LEFT=LEFT+213;
        g.draw3DRect(LEFT,TOP,44,35,true);
        LEFT=LEFT+46;
        //win键
        g.draw3DRect(LEFT,TOP,46,35,true);
        LEFT=LEFT+48;
        //快捷键
        g.draw3DRect(LEFT,TOP,42,35,true);
        LEFT=LEFT+44;
        //右Ctrl键
        g.draw3DRect(LEFT,TOP,53,35,true);
        //编辑键区图形绘制完成
        //绘制字母、符号-------
        LEFT=lTemp;
        TOP=tTemp;
        g.setColor(Color.lightGray);
        Font f=new Font("x",Font.BOLD,13);
        g.setFont(f);
        LEFT=LEFT+7;
        TOP=TOP+14;
        for(int i=1;i<=13;i++){
            g.drawString(shift.substring(i-1,i),LEFT,TOP);
            g.drawString(noShift.substring(i-1,i),LEFT,TOP+18);
            LEFT=LEFT+37;
        }
        f=new Font("x",Font.BOLD,12);
        g.setFont(f);
        g.drawString("BackSpace",LEFT-4,TOP+8);
        LEFT=lTemp+7;
        TOP=TOP+37;
        g.drawString("Tab",LEFT,TOP+8);
        f=new Font("x",Font.BOLD,14);
        g.setFont(f);
        LEFT=lTemp+63;
        for(int i=13;i<=25;i++){
            g.drawString(shift.substring(i,i+1),LEFT,TOP);
            if(i>=23)
                g.drawString(noShift.substring(i,i+1),LEFT,TOP+16);
            LEFT=LEFT+37;
        }
        f=new Font("x",Font.BOLD,12);
        g.setFont(f);
        LEFT=lTemp;
        TOP=TOP+37;
        g.drawString("CapsLock",LEFT+7,TOP+8);
        f=new Font("x",Font.BOLD,14);
        g.setFont(f);
        LEFT=lTemp+74;
        for(int i=26;i<=36;i++){
            g.drawString(shift.substring(i,i+1),LEFT,TOP);
            if(i>=35)
                g.drawString(noShift.substring(i,i+1),LEFT,TOP+16);
            if("F".equals(shift.substring(i,i+1)) || "J".equals(shift.substring(i,i+1)))
                g.drawString("_",LEFT+5,TOP+15);
            LEFT=LEFT+37;
        }
        f=new Font("x",Font.BOLD,12);
        g.setFont(f);
        g.drawString("Enter←┘",LEFT+15,TOP+8);
        LEFT=lTemp+7;
        TOP=TOP+37;
        g.drawString("↑Shift",LEFT,TOP+8);
        f=new Font("x",Font.BOLD,14);
        g.setFont(f);
        LEFT=lTemp+91;
        for(int i=37;i<=46;i++){
            g.drawString(shift.substring(i,i+1),LEFT,TOP);
            if(i>=44)
                g.drawString(noShift.substring(i,i+1),LEFT,TOP+16);
            LEFT=LEFT+37;
        }
        f=new Font("x",Font.BOLD,12);
        g.setFont(f);
        g.drawString("↑Shift",LEFT,TOP+8);
        LEFT=lTemp+7;
        TOP=TOP+37;
        g.drawString("Ctrl",LEFT,TOP+8);
        LEFT=LEFT+55;
        g.drawString("Win",LEFT,TOP+8);
        LEFT=LEFT+46;
        g.drawString("Alt",LEFT,TOP+8);
        LEFT=LEFT+46;
        g.drawString("Space",LEFT+90,TOP+8);
        LEFT=LEFT+213;
        g.drawString("Alt",LEFT,TOP+8);
        LEFT=LEFT+46;
        g.drawString("Win",LEFT,TOP+8);
        LEFT=LEFT+50;
        g.drawString("SM",LEFT,TOP+8);
        LEFT=LEFT+44;
        g.drawString("Ctrl",LEFT,TOP+8);
    }
}

Hint:

import javax.swing.*;

public class Hint extends JInternalFrame{
    Tool parent;
    public Hint(Tool parent){
        this.parent=parent;
        setLayer(4);
        setBounds(142,326,0,0); 
        this.setPreferredSize(new java.awt.Dimension(657, 223));
        ((javax.swing.plaf.basic.BasicInternalFrameUI) this.getUI()).setNorthPane(null);
        this.putClientProperty("JInternalFrame.isPalette", Boolean.TRUE);
        this.setVisible(true);
        this.pack();
        this.add(new Keyset());
        if(PublicData.choice!=2){
            Editor edit=new Editor(this);
            this.parent.parent.add(edit,2);
            edit.requestFocus(); 
        }

    }
}

Editor:

import java.awt.FlowLayout;

public class Editor extends JInternalFrame implements DocumentListener,Runnable{

    Runtime r=Runtime.getRuntime();
    Hint parent;
    
    Document doc;
    StyledDocument styledDoc = new DefaultStyledDocument();
    Thread th;
    
    private int hour=0;//
    private int minute=0;//
    private int second=0;//
    
    private JLabel lblTime;
    private JTextField txtCount;
    private JLabel lblError;
    private JTextPane txtOriginal;
    private JTextPane txtNewText;
    private JPanel pShowData;
    private JButton btnClose;
    private JLabel lblWord;
    private JTextField txtMW;
    private JLabel lblM;
    private JTextField txtError;
    private JTextField txtRight;
    private JLabel lblRight;
    private JLabel lblCount;
    private JTextField txtTime;
    private JPanel pAttrib;
    
    //记录输入的正确和错误的个数据
    private int rightWord=0;
    private int errorWord=0;
    private int rightCount=0;
    private int errorCount=0;
    private int wordCount=0;
    //临时
    private boolean temp=true;
    

    public Editor(Hint parent){
        
        this.parent=parent;
        
        th=new Thread(this);
        
        
        //组建Editor用户界面....
        setLayer(4);
        setBounds(142,0,0,0); 
        this.setPreferredSize(new java.awt.Dimension(657, 325));//内部窗体大小
        ((javax.swing.plaf.basic.BasicInternalFrameUI) this.getUI()).setNorthPane(null);
        this.putClientProperty("JInternalFrame.isPalette", Boolean.TRUE);
        getContentPane().setLayout(null);
        this.setVisible(true);
        {
            pAttrib = new JPanel();
            getContentPane().add(pAttrib, new GridBagConstraints(1, 0, 12, 3, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
            FlowLayout pAttribLayout = new FlowLayout();
            pAttrib.setBorder(BorderFactory.createTitledBorder(""));
            pAttrib.setLayout(pAttribLayout);
            pAttrib.setBounds(5, 21, 658, 42);
            {
                lblTime = new JLabel();
                pAttrib.add(lblTime);
                lblTime.setText("\u7528\u65f6:");
                lblTime.setFont(new java.awt.Font("新宋体", 0, 12));
            }
            {
                txtTime = new JTextField(8);
                pAttrib.add(txtTime);
                txtTime.setText("00:00:00");
                txtTime.setEditable(false);
                txtTime.setHorizontalAlignment(SwingConstants.CENTER);
            }
            {
                lblCount = new JLabel();
                pAttrib.add(lblCount);
                lblCount.setText("\u603b\u5b57\u6570:");
                lblCount.setFont(new java.awt.Font("新宋体", 0, 12));
            }
            {
                txtCount = new JTextField();
                pAttrib.add(txtCount);
                txtCount.setColumns(6);
                txtCount.setText("0");
                txtCount.setEditable(false);
                txtCount.setHorizontalAlignment(SwingConstants.CENTER);
            }
            {
                lblRight = new JLabel();
                pAttrib.add(lblRight);
                lblRight.setText("\u6b63\u786e:");
                lblRight.setFont(new java.awt.Font("新宋体", 0, 12));
            }
            {
                txtRight = new JTextField();
                pAttrib.add(txtRight);
                txtRight.setColumns(4);
                txtRight.setText("0");
                txtRight.setEditable(false);
                txtRight.setHorizontalAlignment(SwingConstants.CENTER);
            }
            {
                lblError = new JLabel();
                pAttrib.add(lblError);
                lblError.setText("\u9519\u8bef:");
                lblError.setFont(new java.awt.Font("新宋体", 0, 12));
            }
            {
                txtError = new JTextField();
                pAttrib.add(txtError);
                txtError.setColumns(4);
                txtError.setText("0");
                txtError.setEditable(false);
                txtError.setHorizontalAlignment(SwingConstants.CENTER);
            }
            {
                lblM = new JLabel();
                pAttrib.add(lblM);
                lblM.setText("\u6bcf\u5206\u949f");
                lblM.setFont(new java.awt.Font("新宋体", 0, 12));
            }
            {
                txtMW = new JTextField();
                pAttrib.add(txtMW);
                txtMW.setColumns(4);
                txtMW.setText("0");
                txtMW.setEditable(false);
                txtMW.setHorizontalAlignment(SwingConstants.CENTER);
            }
            {
                lblWord = new JLabel();
                pAttrib.add(lblWord);
                lblWord.setText("\u4e2a\u5b57");
                lblWord.setFont(new java.awt.Font("新宋体", 0, 12));
            }
            {
                btnClose = new JButton();
                pAttrib.add(btnClose);
                btnClose.setText("\u79bb\u5f00");
                btnClose.setFont(new java.awt.Font("新宋体", 0, 12));
                btnClose.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                        exitFram();
                    }
                });
            }
        }
        {
            pShowData = new JPanel();
            getContentPane().add(
                pShowData,
                new GridBagConstraints(
                    1,
                    1,
                    12,
                    10,
                    0.0,
                    0.0,
                    GridBagConstraints.CENTER,
                    GridBagConstraints.BOTH,
                    new Insets(0, 0, 0, 0),
                    0,
                    0));
            GridLayout pShowDataLayout = new GridLayout(2, 1);
            pShowDataLayout.setHgap(5);
            pShowDataLayout.setVgap(5);
            pShowDataLayout.setColumns(1);
            pShowDataLayout.setRows(2);
            pShowData.setLayout(pShowDataLayout);
            pShowData.setBounds(5, 82, 644, 231);
            {
                txtOriginal = new JTextPane(styledDoc);
                pShowData.add(txtOriginal);
                txtOriginal.setPreferredSize(new java.awt.Dimension(600, 0));
                txtOriginal.setEditable(false);
            }
            {
                txtNewText = new JTextPane();
                pShowData.add(txtNewText);
                txtNewText.setPreferredSize(new java.awt.Dimension(600, 0));
                Document doc=txtNewText.getDocument();
                doc.addDocumentListener(this);
                
            }
        }
        {
            
            txtOriginal.setText(DistillText.getData(PublicData.Text,PublicData.textTag));
            //PublicData.key=txtOriginal.getText().substring(0,1);
            Empty empty=new Empty();
            empty.start();
            if(txtOriginal.getText().length()!=0)
            {
                if(PublicData.Lang.equals("English")){
                    PublicData.key=txtOriginal.getText().substring(0,1);
                    this.parent.repaint();
                }
                if(PublicData.Lang.equals("Chinese")){
                    PublicData.key="";
                }
            }
        }
        this.pack();
        txtNewText.requestFocus();   
        th.start();
    }
    
     //判断正确和错误的字.并改变它们的颜色
     public void chDocs(int xLen,int yLen,String xStr,String yStr)
    {
         SimpleAttributeSet attrSet=new SimpleAttributeSet(); 
         rightWord=0;
         errorWord=0;
         for(int i=1;i<=xLen;i++)
         {
             if(i<=yLen){//数据验证
                 if(xStr.substring(i-1,i).equals(yStr.substring(i-1,i))){
                     rightWord++;
                     StyleConstants.setForeground(attrSet,Color.BLUE);
                 }
                 else{
                     errorWord++;
                     StyleConstants.setForeground(attrSet,Color.RED);
                 }
                 StyleConstants.setUnderline(attrSet,true);
             }
             else{
                 StyleConstants.setForeground(attrSet,Color.BLACK);
                 StyleConstants.setUnderline(attrSet,false);
             }
             styledDoc.setCharacterAttributes(i-1,1,attrSet,true);
           }
         
         txtRight.setText((rightWord+rightCount)+"");
         txtError.setText((errorWord+errorCount)+"");
         int c=0;
         for(int i=0;i<minute;i++){
             c=c+60;
         }
         c=c+second;
         double s=(double)c/60;
         txtMW.setText(""+Math.round(((double)Integer.parseInt(txtRight.getText()))/s));
    }

    //实现DocumentListener所有的方法..用于监听输入文本...
    public void changedUpdate(DocumentEvent e){
        if(PublicData.Lang.equals("English")){
            PublicData.key=txtOriginal.getText().substring(txtNewText.getText().length(),
                    txtNewText.getText().length()+1);
            this.parent.repaint();
        }
        if(PublicData.Lang.equals("Chinese")){
            PublicData.key="";
        }
    }
    public void insertUpdate(DocumentEvent e){
        if(PublicData.Lang.equals("English")){
            try{
                PublicData.key=txtOriginal.getText().substring(txtNewText.getText().length(),
                        txtNewText.getText().length()+1);
            }catch(StringIndexOutOfBoundsException str){}
            this.parent.repaint();
        }
        if(PublicData.Lang.equals("Chinese")){
            PublicData.key="";
        }
    }

    public void removeUpdate(DocumentEvent e){
        if(PublicData.Lang.equals("English")){
            PublicData.key=txtOriginal.getText().substring(txtNewText.getText().length(),
                    txtNewText.getText().length()+1);
            this.parent.repaint();
        }
        if(PublicData.Lang.equals("Chinese")){
            PublicData.key="";
        }
    }
    
    //数据验证
    private void dataValidate(){
        chDocs(txtOriginal.getText().length(),txtNewText.getText()
                .length(),txtOriginal.getText(),txtNewText.getText());
        if(txtOriginal.getText().length()>txtNewText.getText().length()){
            if(PublicData.Lang.equals("English")){
                PublicData.key=txtOriginal.getText().substring(txtNewText.getText().length(),
                        txtNewText.getText().length()+1);
            }
            if(PublicData.Lang.equals("Chinese")){
                PublicData.key="";
            }
            txtCount.setText((txtNewText.getText().length()+wordCount)+"");
        }
        else{
            wordCount=wordCount+txtOriginal.getText().length();
            rightCount=rightCount+rightWord;
            errorCount=errorCount+errorWord;
            txtOriginal.setText(DistillText.getData(PublicData.Text,PublicData.textTag));
                
            if(PublicData.Lang.equals("English")){
                PublicData.key=txtOriginal.getText().substring(0,1);
                this.parent.repaint();
            }
            if(PublicData.Lang.equals("Chinese")){
                PublicData.key="";
                this.parent.repaint();
            }
        }
    }
    
    //退出时所要关闭的窗体
    private void exitFram(){
        PublicData.key="";
        PublicData.Text="";
        this.parent.repaint();
        this.parent.doDefaultCloseAction();
        this.doDefaultCloseAction();
        PublicData.onChoice=true;
        PublicData.textTag=1;
        r.gc();
        System.gc();
    }
    
    //计时器
    public  void run(){
        try{
            while(true){
                Thread.sleep(1000);
                second++;
                if(second==60){
                    second=0;
                    minute++;
                    if(minute==60){
                        minute=0;
                        hour++;
                        if(hour==1000){
                            second=0;
                            minute=0;
                            hour=0;
                        }
                    }
                }
                txtTime.setText(((hour<=9)?"0":"")+hour+":"+((minute<=9)?"0":"")
                        +minute+":"+((second<=9)?"0":"")+second);
            }
        }
        catch (InterruptedException e){}
    }
    
    class Empty extends Thread{
        public void run(){
            while(true){
                try{
                    Thread.sleep(100);
                    dataValidate();
                    if(txtNewText.getText().length()>=txtOriginal.getText().length()){
                        txtNewText.setText("");
                    }
                }
                catch(InterruptedException e){}
            }
        }
    }
}

Text:

import java.io.*;
public class DistillText {
    public static String getData(String fileName,int segment){
        int i=0;
        if((fileName==null) || (fileName.length()==0))
            return "";
        File f;
        FileReader in=null;
        String s="";
        try{
            f=new File(fileName);
            in=new FileReader(f);
            char[] buffer=new char[312];
            int len;
            if(PublicData.Lang.equals("English")){
                if(segment>=(int)(f.length()/312))
                    PublicData.textTag=0;
            }
            if(PublicData.Lang.equals("Chinese")){
                if(segment>=(int)(f.length()/312-1)+1)
                    PublicData.textTag=0;
            }
            while((len=in.read(buffer))!=-1){
                s=new String(buffer,0,len);
                i++;
                if(i==segment){
                    PublicData.textTag++;
                    break;
                }
            }
        }
        catch(IOException e){
            System.out.println(e);
        }
        finally{
            try{
                if(in!=null)
                    in.close();
            }
            catch(IOException e){}
        }
        return s;
    }
}

我要把它转化成使用swt来写,加油!!。

Java + eclipse + awt 编写锻炼打字小软件(未完成)

标签:

原文地址:http://www.cnblogs.com/tianjintou/p/4619872.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!