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

进入前界面:

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("[email protected]#$%^&*()_+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来写,加油!!。

时间: 2024-11-08 03:13:27

Java + eclipse + awt 编写锻炼打字小软件(未完成)的相关文章

Java Eclipse解决中文字体太小

解决方式有两种: 一.把字体设置为Courier New 操作步骤:打开Elcipse,点击菜单栏上的“Windows”——点击“Preferences”——点击“Genneral”——点击“Appearance”——点击“Colors and Font”——在右侧框展开“Basic”文件夹--双击“Text Font”——在弹出窗选择“Courier New”(注:这里可能找不到“Courier New”,点击字体选择框左下角的“显示更多字体”链接来打开设置字体的控制面板,找到“Courier

做一个具有图片读入,写出,灰度化,黑白化的有图形界面的小软件

我们这一次的树莓基础的作业是做一个做一个具有图片读入,写出,灰度化,黑白化的小软件,我采用的是java,因为java有大量性能非常好的接口可用,譬如在图片这块,ImageIO,BufferedImage都是非常好用的,而我这一次作业也是使用这两个包. 首先我们要解决的是界面,界面可以直接用JFrame来打, 非常简单,我的界面有一个菜单栏和一个图片显示区,菜单栏有三个选项:文件,转换,保存,文件菜单有一个子菜单:打开文件,转换菜单有两个子菜单:变成灰度和变成黑白,保存菜单有三个子菜单:jpg,

1.3—一个典型的JAVA程序的编写和运行过程

JAVA语言应用范围 桌面应用编程 WEB客户端编程 WEB服务器编程 手机编程 机器人编程 第一个JAVA程序 JAVA开发环境搭建 下载:Download J2SDK (Java  2 Software  Development Kit) from http://java.sun.com 安装:run the executable(跟普通软件安装一样,点击下一步就OK!)  环境变量配置: 环境变量(就是为相关命令提供一个路径信息,告诉他到哪里去找相关文件信息): 设置: 新增系统环境变量J

java eclipse 作业代写、代做eclipse实验

java eclipse 作业代写.代做eclipse实验<java程序设计>课程实验报告 实验四 继承与多态一.实验目的及要求1.了解如何使用类及其成员的修饰符,掌握方法的继承.重载和覆盖.2.掌握类的继承关系和派生方法.3.掌握多态的概念与使用. 4.掌握接口的定义与使用.二.实验内容(一)类的继承性练习1.进一步理解继承的含义 新类可从现有的类中产生,并保留现有类的成员变量和方法并可根据需要对它们加以修改.新类还可添加新的变量和方法.这种现象就称为类的继承. 当建立一个新类时,不必写出全

java+eclipse+selenium环境搭建

这几天在学selenium,大头虾的我.安装环境还是遇到了挺多问题,赶紧来记录下.不然下次又...(参考虫师的<Selenium2 Java自动化测试实战>),就随便写写加深下自己的印象. 1.安装java 访问java官网下载jdk http://www.oracle.com/technetwork/java/javase/downloads/index.html 下载自己需要的版本,我现在使用的是jdk-8u101-windows-x64.exe java环境分JDK和JRE,JDK就是J

【小软件】年度优秀员工

部门快要年会了,最近做了个小软件,“年度优秀员工”apk. 需求:部门的七个小组每组推荐两名候选人,部门所有员工投票选出七名优秀员工.每人必须选不是同一组的两人,不能多选或者少选,必须是本部门的员工才有资格选,选定两个后最终本软件以短信的形式向同一个手机号发送过去,最终通过短信数据库导出表格,统计即可. 实现界面如下: 代码如下: activity_main.xml 1 <TableLayout xmlns:android="http://schemas.android.com/apk/r

Java eclipse Myeclipse tomcat安装及配置

Java eclipse Myeclipse tomcat安装及配置作者:天涯 来源:中国自学编程网 发布日期:1223857747目前,开发Java网页程序,最流行的就是用Myeclipse来进行编程设计.这主要跟该程序的有点有非常大的原因.Myeclipse程序中,有数不尽的原因,但值得向大家推荐的有如下几个原因: ? 配置tomcat比较方便: ? 代码自动补全: ? 代码提示: ? 自动报错,还带错误提示. 当然Myecilpse还有许多优点,我在这里就不一一列举了. 网上,关于Java

java实现 swing模仿金山打字 案例源码

java实现 swing模仿金山打字 案例源码,更多Java技术就去Java教程网.http://java.662p.com 代码: <font size="3">import Java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Image; import java.awt.Toolkit; import java.io.File; import java.io.IO

JAVA学习AWT绘图

package com.graphics; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; /** * 1:Graphics类是所有图形上下文的抽象基类. * * 2:Graphics2D继承了Graphics类,实现了功能更加强大的绘图操作的集合. * 由于Graphics2D类是Graphics类的扩展,也是推荐使用的java绘图类 * 所以本章主要介绍使用Graphics2D类实