1
我已经写了这段代码,询问用户要显示的形状,然后在JFrame框中显示它,我的问题是问题显示在控制台框(内部eclipse)不在JFrame框,所以我怎么能改变?如何在JFrame框中显示用户输入?
此外问题重复两次我不知道为什么。
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.WHITE);
Scanner user_input = new Scanner(System.in);
int shape_num;
System.out.println("What is the shape you want to draw? 1- Rectangle 2- Circle");
shape_num = user_input.nextInt();
if(shape_num ==1){
g.setColor(Color.BLUE);
g.fillRect(25, 25, 150, 50);
}
else if(shape_num ==2) {
g.setColor(Color.RED);
g.fillOval(25, 80, 100, 100);
}
else if (shape_num > 2) {
System.out.println("Error");
}
}
public static void main(String[] args){
JFrame f = new JFrame("Title");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Rectangle r = new Rectangle();
f.add(r);
f.setSize(400, 250);
f.setVisible(true);
}
}珍贵的一切事物
我可以继续使用您的代码并重新制作矩形/椭圆中给定高度和宽度的部分,并让用户输入他们选择的尺寸? – ItssMohammed