用于绘画方法的键盘输入(joptionpane)

时间:2013-12-09 08:49:36

标签: java swing input jpanel paintcomponent

帮助我很沮丧,无法找到joption窗格的输入扫描程序方法。我需要从对话框中读取用户输入,但是我的in = a.nextInt无法工作。如果在(输入= 1)之后,将绘制某个形状,扫描对话框输入的代码是什么,任何人都可以帮助

import java.awt.*;
import javax.swing.*;
import java.util.Scanner;
public class Paint extends JPanel {


public Paint() {

    Scanner a = new Scanner(System.in);
     in = a.nextInt(JOptionPane.showInputDialog("Please enter number: "));

    setBackground(Color.WHITE);

}
public void paintComponent(Graphics g){
    super.paintComponent(g);



    if(in == 1){
    g.setColor(Color.BLACK);
    g.drawLine(400, 400, 400, 350);
    g.drawLine(400, 350, 350, 350);
    }
}
}

1 个答案:

答案 0 :(得分:0)

使用下一个结构:

int in = -1;
String showInputDialog = JOptionPane.showInputDialog("Please enter number: ");
try{
    in = Integer.valueOf(showInputDialog);
} catch (Exception e){
    e.printStackTrace();
}

而不是

Scanner a = new Scanner(System.in);
in = a.nextInt(JOptionPane.showInputDialog("Please enter number: "));