【问题标题】:java - input dialog validationjava - 输入对话框验证
【发布时间】:2013-10-08 09:19:57
【问题描述】:

我正在尝试在 java 中实现 A* 算法。要求用户键入要成为网格的宽度和高度。我的问题是验证该输入。这是我的代码:

public class Map extends java.awt.Panel implements Serializable
{
JFrame frame = new JFrame();
String rows =  JOptionPane.showInputDialog(frame,
                "Συμπληρώστε τον αριθμό γραμμών του πλέγματος: \n",
                "Δημιουργία πλέγματος",
                JOptionPane.PLAIN_MESSAGE);
String cols =  JOptionPane.showInputDialog(frame,
                "Συμπληρώστε τον αριθμό στηλών του πλέγματος: \n",
                "Δημιουργία πλέγματος",
                JOptionPane.PLAIN_MESSAGE);

int rowsnum = Integer.parseInt(rows); 
int colsnum = Integer.parseInt(cols);
GridCell gridCell[][] = new GridCell[rowsnum][colsnum];

public Map()
{
        super();

    setLayout(new GridLayout(rowsnum,colsnum)); 
    for(int i=0;i<rowsnum;i++){ 
        for(int j=0;j<colsnum;j++){
           gridCell[i][j] = new GridCell();
           gridCell[i][j].setPosition(new Point(i,j));
           add(gridCell[i][j]);
        }
    }
}

我尝试用一​​种新方法检查输入,但我的问题是我需要从我的程序的其他类访问 rowsnumcolsnumgridcell[]

我是 Java 新手,非常感谢任何帮助。 :-)

【问题讨论】:

  • My problem is in validating that input.,请验证输入 == 或 != GridCell 上/进入的鼠标或按键事件
  • 您能再解释一下吗?
  • 请问什么是 == 验证输入

标签: java swing validation input a-star


【解决方案1】:

现在,您的 rowsnum、colsnum 和 GridCells 是“Map”类中的变量。

为了从其他类中作为实例变量访问它,

public int rowsnum = // something
public int colsnum = // something
public GridCell girdCell[][] = //something

“public”表示可以从类外访问变量。

在您的 Map 类之外,

Map m = new Map();
int rowsnum = m.rowsnum
int colsnum = m.colsnum
// access instance variables

但是,最好为实例变量创建 getter 和 setter。

也就是说,为获取和设置这些变量创建单独的方法。

public int getRowsNum() {
    return rowsnum;
}

等等。

【讨论】:

  • 我已经将验证放在一个方法中,但是当我将行、列、网格单元声明为公共时,我有一个错误“表达式的非法开始”。
  • public void validate() { if (rows==null ||rows.isEmpty()||!rows.matches("[0-9]*")) { JOptionPane.showMessageDialog(frame, "Δεν πληκτρολογήσατε έγκυρο αριθμό!", "Λάθος είσοδος", JOptionPane.ERROR_MESSAGE); } } 但是我不知道怎么称呼它!我添加了一个else {public int rowsnum = Integer.parseInt(rows); public int colsnum = Integer.parseInt(cols); public GridCell gridCell[][] = new GridCell[rowsnum][colsnum];},这就是错误显示的地方。
  • 那些字符串变量 rows & cols 应该在 Map 类之外声明。放到main()函数中,获取rows和cols的值后,传入Map构造函数,设置rowsnum和colsnum。
【解决方案2】:

您可以通过 Map 类对象访问它们(将访问修饰符更改为这些变量的公共变量)

示例:在 B 类中

public class Test {
    public static void  main(String args[]){
        Map someMap = new Map();

        //Access gridCell 
        someMap.gridCell;

        //Access rowsnum
        someMap.rowsnum;

        //Access colsnum
        someMap.colsnum;
    }
}

或者您可以将它们设为静态并使用类名访问它们

示例: Map.rowsnum;

【讨论】:

  • 是的,我现在按照代码的方式访问它们没有问题,但是如果我在 Map 的方法中初始化它们,那么我如何从 Map 类的其他方法或其他方法访问它们上课?
【解决方案3】:

我能想到的最简单的解决方案是

使用 If 验证宽度/高度数字

public class Map extends java.awt.Panel implements Serializable
    {      
    String width =  JOptionPane.showInputDialog("Give Width (>5 , <30)");
    int w = Integer.parseInt(width);

    String height =  JOptionPane.showInputDialog("Give height (>5 , <30)");
    int h = Integer.parseInt(height);

    GridCell gridCell[][] = new GridCell[w][h];

    public Map()
    {
    super();
    setLayout(new GridLayout(w,h)); 

    if ( w < 5)
    {JOptionPane.showMessageDialog ( null, "Number out of Limits ,application closing" );
     System.exit(0);}
    if ( w > 30)
    {JOptionPane.showMessageDialog ( null, "Number out of Limits ,application closing" );
     System.exit(0);}
    if ( h < 5)
    {JOptionPane.showMessageDialog ( null, "Number out of Limits ,application closing" );
     System.exit(0);}
    if ( h > 30)
    {JOptionPane.showMessageDialog ( null, "Number out of Limits ,application closing" );
     System.exit(0);}


        for(int i=0;i<w;i++){ 

            for(int j=0;j<h;j++){
               gridCell[i][j] = new GridCell();
               gridCell[i][j].setPosition(new Point(i,j));
               add(gridCell[i][j]);
            }
        }
    }

在 public main 上使用 try-catch 验证字符串输入错误

public static void main(String[] argv) {

try{    
AStar2 test = new AStar2();                   
test.setLocationRelativeTo(null);
test.setDefaultCloseOperation(test.EXIT_ON_CLOSE);}
catch(NumberFormatException e){JOptionPane.showMessageDialog ( null, "BLOODY HELL DONT PUT STRINGS FOR INPUT, APPLICATION IS CLOSING , TRY BETTER NEXT TIME" );}

    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    相关资源
    最近更新 更多