【发布时间】:2017-01-07 11:47:28
【问题描述】:
I'm making a drawing program which reads commands from a text file, when file is selected the program should validate the commands for correct entry parameters.程序中打开的文本文件包括;
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
}
}
【问题讨论】:
-
如需尽快获得更好的帮助,请发帖minimal reproducible example。要使其成为 MCVE,它需要是完整的(在一次复制/粘贴中),因此它需要将源代码中的数据硬编码为
String,并包含一个将其放在屏幕上的主要方法。顺便说一句 - 我注意到除了你问的第一个问题,你没有收到任何答案。此外,您至少在以前的一次场合被建议发布 SSCCE(相当于 MCVE),但迄今为止未能掌握这样做的好处。如果您希望在 SO 上获得成功的解决方案,您可能需要反思这些要点。 -
是的,所以我承认,我不是最熟悉 java 的用户,从头开始不是我的任务一天之内的选择。
-
"..我的作业一天后到期" 那么你应该早点开始和/或使用更好的时间管理。将来,请不要把您的问题误认为是我们的问题。免费提供帮助的人通常更愿意帮助具有良好时间管理技能的人,至少有足够的时间听从他们的建议。
标签: java swing validation awt jtextarea