-1
텍스트 파일에서 명령을 읽는 드로잉 프로그램을 만들고 있는데, 파일을 선택하면 올바른 입력 매개 변수에 대한 명령의 유효성을 검사해야합니다. 프로그램에서 연 텍스트 파일에는 다음이 포함됩니다. 스윙/AWT 파일 선택기 유효성 검사 그래픽 드로잉
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
}
}
더 나은 도움을 받으려면 [mcve]를 게시하십시오. 그것을 MCVE로 만들기 위해서는 Complete (하나의 복사/붙여 넣기)가 필요하므로 소스 코드에서 데이터를 하드 코딩하여'String'으로 저장하고 화면에 표시하는 주요 방법을 포함시켜야합니다. BTW - 귀하가 요청한 첫 번째 질문을 제외하고는 답변을받지 못했습니다. 또한 SSCCE (MCVE와 동등)를 게시하기 위해 적어도 한 번은 사전에 조언을 받았으므로 그렇게하는 이점을 파악하지 못했습니다. SO에 대한 성공적인 솔루션을 얻으려면 이러한 요점을 생각해 볼 수 있습니다. –
그래, 그렇지만 나는 인정했다. 자바에 관해서 가장 친숙한 사용자는 아니지만, 처음부터 시작하는 것은 내 과제가 하루 만에 할 수있는 옵션이 아니다. – Adam
* "내 임무는 하루 만에해야합니다."* 그러면 당신은 더 빨리 일을 시작하고/또는 더 나은 시간 관리를 사용해야합니다. 그리고 앞으로도 우리의 문제를 오해하지 마십시오. 무료로 도움을주는 사람들은 일반적으로 조언을 따르기에 충분한 시간 관리 기술을 가진 사람들을 돕는 것을 선호합니다. –