Swing / AWT filechooser验证图形绘图

时间:2017-01-07 11:47:29

标签: java swing validation awt jtextarea

我正在制作一个从文本文件中读取命令的绘图程序,当选择文件时,程序应该验证命令以获得正确的输入参数。程序中打开的文本文件包括;

Move 100 100 // (move pen to X Y)
MIVE 100 50 // (Invalid comamnd spelt incorrectly)
move x y // (invalid command not an integer)
Line 20 100 // (draw a line at X Y)

我遇到的问题是,在将文本文件打开到程序中时,它正在导入文本文件,但它没有将其验证到JTextArea并在选定的X / Y处绘制线条 - 坐标。有人能指出我正确的方向吗?

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Instructionpanel extends JPanel {

JTextArea instructions;

// Move line text clear
public Instructionpanel(GraphicsPanel graphicspanel) {

    instructions = new JTextArea(
            "This is where the instructions will displayed", 10, 50); // Rows
                                                                        // *
                                                                        // columns
    instructions.setLineWrap(true);
    instructions.setWrapStyleWord(true);
    instructions.setEditable(true);
    JScrollPane areaScrollPane = new JScrollPane(instructions);
    areaScrollPane
            .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    add(areaScrollPane);
    // add(instructions);
}

public void processFile(File file) {
    Scanner scan = null;
    try {
        scan = new Scanner(file);

    } catch (FileNotFoundException el) {
        el.printStackTrace();
    }

    String allInstructions = "";

    String allInstructions1 = "";
    String instruction = "";
    while (scan.hasNext()) {
        instruction = scan.nextLine() + "\n";
        // check or validate the instruction

        allInstructions1 += validateInstruction(instruction);

    }

    instructions.setText(allInstructions1);

}

public String validateInstruction(String orig) {
    String returnString = orig;

    // Do all the checking
    // Convert string to an array
    String command = "";
    String[] instructionArray = orig.split("  ");

    int x, y = 0;

    switch(instructionArray[0])
        {
    case "MOVE":
        // Check there three parameters
        doMove( instructionArray );            {
            // And they are integers
            instructions = new JTextArea(" Incorrect parameter type i.e 100");
            instructions = new JTextArea(":incorrect number of parameters i.e Line 100 200");
            try {

                GraphicsPanel.setPos (Integer.parseInt(instructionArray[1]),Integer.parseInt(instructionArray[2]));

            } 

            catch (NumberFormatException e) 
            {
                instructions = new JTextArea(" only numbers are allowed ");
            }
        }
        break;

            case "LINE":
                doLine ( instructionArray );
        // Check there three parameters
        if (instructionArray.length != 3) {
            // And they are integers
            instructions = new JTextArea(" Incorrect parameter type i.e 100");
            instructions = new JTextArea(":incorrect number of parameters i.e Line 100 200");
            try {
                GraphicsPanel.drawLine (Integer.parseInt(instructionArray[1]),Integer.parseInt(instructionArray[2]));
            } catch (NumberFormatException e) {
                instructions = new JTextArea(" only numbers are allowed ");
            }break;

            }}
    return orig;}

private void doLine(String[] instructionArray) {
    // TODO Auto-generated method stub

}
private void doMove(String[] instructionArray) {
    // TODO Auto-generated method stub  
}
}

1 个答案:

答案 0 :(得分:0)

从快速查看我可以说,String[] instructionArray可能只创建了一个元素,因为orig.split(" ")具有双倍空格作为分隔符,因此instructionArray[0]包含整个行,因为您的令牌仅隔开一个空间。因此,validateInstruction(String orig)中的所有案例都不匹配。

现在你可以在orig.split(" ")电话中将双倍空格替换为单个空格。

据说我无法保证这将解决变更后可能发生的所有潜在问题。