我正在尝试使用扫描仪在 JFrame 中绘制一个矩形,但出现以下错误:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at DrawerAutoRect.main(DrawerAutoRect.java:39)
我的这个程序的目标是选择要绘制的对象的类型,即:线条、矩形、椭圆,然后输入参数,即如果我正在绘制的矩形,输入将是r, 200,200,400,400
并且它绘制一个矩形与 JFrame 上的这些尺寸。然后我只需输入“end”,它就会等待输入和绘制对象。
import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Scanner;
import javax.swing.JFrame;
public class DrawerAutoRect extends JFrame {
public DrawerAutoRect(){
setSize(1020,1020);
}
public static void paint(Graphics g, int a, int b,int c, int d)
{
Graphics2D g2 = (Graphics2D)g;
g2.drawRect(a, b, c, d);
}
public static void main(String[] args) {
int x1 = 100;
int x2 = 100;
int y1 = 50;
int y2 = 50;
Scanner s = new Scanner(System.in);
String dim = s.next();
while(dim!="end"){
if(dim.equals("r")){
x1 = s.nextInt();
y1 = s.nextInt();
x2 = s.nextInt();
y2 = s.nextInt();
}
}
paint(null, x1, y1, x2, y2);
DrawerAutoRect r = new DrawerAutoRect();
r.setDefaultCloseOperation(EXIT_ON_CLOSE);
r.setVisible(true);
//r.pack();
r.setTitle("Tutorial");
}