0

我正在制作一个打字程序,现在我已经为我的应用程序制作了一个主页,其中包含一个 Jlist,其中包含来自数据库的练习负载。这些练习包含文本,因此通过示例

在我的 Jlist 中有练习 1,其中包含以下文字:鲁道夫出去散步。

现在,如果您选择练习 1 并选择一个确定按钮,他将移动到下一页,其中包含一个 JtextField,其中包含文本 Rudolf 出去散步,以及一个供用户键入相同内容的 JTextfield。

现在我有一个问题,我不知道怎么做,所以当用户需要输入鲁道夫时,这个词被突出显示,当用户输入例如 Pieter 时,它标记了 Pieter 这个词,因为它是错误的,必须是鲁道夫。

文本显示在字段中的类的代码:

package View;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.Highlighter;



import Model.ExcersizeFactory;

public class StartOefeningTekst extends JFrame implements KeyListener {

        private JTextArea questionArea; //holds the file text
        private JTextArea enterArea; //holds the text user type
        private JPanel jPanel1;
        private JScrollPane enterScroll, questionScroll; //scrollpane to add textpanes


      //start and index are the starting and ending positions of current word to be typed
        private int index=0;
        private int start;

        private String invoer="";               // this will store the word that is to be typed 
        private String typedword="";            // holds the current word we are typing 

        private int totalhit=0;             // total word we have typed
        private int wronghit=0;             //number of mistakes

        private Highlighter enterHt;            //highlighter for file text
        private Highlighter questionHt;         //highlighter for type text

        private Highlighter.HighlightPainter filepainter;  
        private Highlighter.HighlightPainter textpainter;

        private boolean endfilestatus;          
        private boolean typingstartstatus;


    public StartOefeningTekst() {
       initGUI();

        setText_QuestionArea();

    }

    public void setText_QuestionArea() {

        questionArea.setText(ExcersizeFactory.getInstance().getClickedExcersize().getText());
    }

    public void underLine() {

        String str=questionArea.getText();
        if(str.length()>index){

            String space=""; // to check the occurance of  space or next line i.e. end of word
            invoer="";      // this will store the word that is to be typed 

            start=index;    //last position of previous word becomes the starting position of next word 
            //this loop runs until space or /n character comes 
            while(!space.equals("\n") && !space.equals(" ")){
                    space=""+str.charAt(index); //get character at index position
                    index++;    // increment of index i.e. next time next character will fetch
            }

            invoer=str.substring(start,index-1); // store the words to typed 

            questionArea.setCaretPosition(start); // sets the courser to current postion 

            try {
                enterHt.removeAllHighlights();          //removes all highlights added on filetext 
                enterHt.addHighlight(start, index-1, filepainter); //highlights the current word that is to be typed
            } catch (Exception e) {
                System.out.println("some problem with Highlighter "+e);
            }
            //runs the underLine function again if String sub has nothing  
            if(invoer.equals("")){
                underLine();
            }
        }//end of if (that checks the index and file text length)
        else{
            endfilestatus=true;
        //  enterArea.setEditable(false);
        }

    }//end of underline method

    // get endfilestatus
    public boolean getfilestatus(){
        return endfilestatus;
    } 

    //for checking the text is correctly typed or not
        public synchronized void check(String text){

            invoer=invoer.trim();

            //if word is not correctly typed
            if(!invoer.equals(text)){

                wronghit++; //increment in wronghit

                //add highlight to the not correctly typed word
                try {
                    questionHt.addHighlight(enterArea.getText().length()-text.length()-1, enterArea.getText().length()-1, textpainter);
                } catch (BadLocationException e) {
                    e.printStackTrace();
                }
            }

            totalhit++; //increment total hit

        }//end of check

        //backSpace function

        public synchronized void backSpace(){

            if(typedword.length()>0){
            typedword=typedword.substring(0,typedword.length()-1);
            }
            else{
                    enterArea.append(" ");
            }
        }//end of backspace function

        //key listener methods 
        public void keyPressed(KeyEvent ke){

            if(ke.getKeyChar()==' '){
                enterArea.append(" ");
            }

            typingstartstatus=true;
            //System.out.println(ke.getKeyCode());
        }

        //key listener methods
        public void keyTyped(KeyEvent ke){

            enterArea.setCaretPosition(enterArea.getText().length()); //set course position at the end 

            String ch=""+ke.getKeyChar(); //gets the currently typed character


            if((!ch.equals(" ") && !ch.equals("\n") && ke.getKeyChar()!=8 && ke.getKeyChar()!=9 && ke.getKeyChar()!=38 && ke.getKeyChar()!=39 && ke.getKeyChar()!=37 && ke.getKeyChar()!=40) || ch.equals("'") || ch.equals("(") || ch.equals("%") || ch.equals("&")){
                 typedword=typedword+ch;    
            }
            else if(ke.getKeyChar()==8){
                backSpace();    
            }

            else{

                if(typedword.length()>0){

                    check(typedword);
                    underLine();
                    typedword="";

                }
            }

            //System.out.println(ttext);

        }

        //key listener methods
        public void keyReleased(KeyEvent ke){
            //if the arrow keys are pressed 
            if(ke.getKeyCode()==37 || ke.getKeyCode()==38 || ke.getKeyCode()==39 || ke.getKeyCode()==40)
            {
                enterArea.setCaretPosition(enterArea.getText().length());
                System.out.println("released");
            }

        }

        //result of typing test
        public void result(float time, Container win){

            enterArea.setEditable(false);
            if(totalhit>0){
            JOptionPane.showInternalMessageDialog(win, "Gross Speed : "+(int)(totalhit/(time/60))+" WPM \n"
                    +"Accuracy : "+(((totalhit-wronghit)*100)/totalhit)+"%"+"\n"
                    +"Net Speed : "+(int)((totalhit-wronghit)/(time/60))+" wpm", "Your Typing Speed Result ",JOptionPane.INFORMATION_MESSAGE);


            }
            else{
                JOptionPane.showInternalMessageDialog(win, "You haven't Typed anything.");
            }

        }

        //get typing start status
        public boolean getTypingStartStatus(){
            return typingstartstatus;
        }

        public void setTypingStartStatus(boolean set ){
            typingstartstatus=set;
        }


    private void initGUI() {


        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

          jPanel1 = new JPanel();


        endfilestatus=false;
        typingstartstatus=false;

        //questionArea
        questionArea = new JTextArea(10,1);
        questionArea.setLineWrap(true);
        questionArea.setEditable(false);
        questionArea.setWrapStyleWord(true);
        questionArea.setFont(new Font("Arial",Font.PLAIN,17));
        questionArea.setFocusable(false);

        //file high lighter
        filepainter = new DefaultHighlighter.DefaultHighlightPainter(Color.CYAN);
        enterHt = new DefaultHighlighter();
        questionArea.setHighlighter(enterHt);
        questionScroll = new JScrollPane(questionArea);
        questionScroll.setPreferredSize(new Dimension(400,200));
        questionScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        questionScroll.setBorder(questionScroll.getBorder());

        enterArea = new JTextArea(10,1);
        enterArea.setLineWrap(true);
        enterArea.setWrapStyleWord(true);
        enterArea.setFont(new Font("Times New Roman",Font.PLAIN,17));

        //text high lighter
        questionHt = new DefaultHighlighter();
        textpainter = new DefaultHighlighter.DefaultHighlightPainter(Color.RED);
        enterArea.setHighlighter(questionHt);

        //scroll pane for enterArea
        enterScroll = new JScrollPane(enterArea);
        enterScroll.setPreferredSize(new Dimension(400,200));
        enterScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        enterScroll.setBorder(enterScroll.getBorder());

        //adding to the frame
        add(questionScroll);
        add(enterScroll);

        //addning keyListerner
        enterArea.addKeyListener(this);

        validate();

        //under line the current word to be typed
        underLine();

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(enterScroll, javax.swing.GroupLayout.DEFAULT_SIZE, 594, Short.MAX_VALUE)
            .addComponent( questionScroll, javax.swing.GroupLayout.DEFAULT_SIZE, 594, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent( questionScroll, javax.swing.GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(enterScroll, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();





    }







}
4

0 回答 0