我是新來的Java和我試圖創建一個程序,將首先要求用戶輸入一些數字,並將輸出一個音量,面積等。此外,我想顯示一個矩形,我不知道如何做,因爲我的程序運行良好,它只是不會顯示矩形。我能做什麼?如何結合GUI類和掃描儀類?
package testchap3;
import java.util.*;
import javax.swing.JApplet;
import java.awt.*;
public class Chapter_3 extends JApplet
{
public void paint(Graphics page)
{
page.drawRect(50,50,60,60);
}
public static void main(String[] args)
{
int lenght,width,height,volume,Area,Perimeter;
Scanner scan = new Scanner(System.in);
System.out.println("What is the lenght:");
lenght = scan.nextInt();
System.out.println("What is the height:");
height = scan.nextInt();
System.out.println("What is the width:");
width= scan.nextInt();
volume = (lenght*height*width);
Area= volume/height;
Perimeter= lenght+width+lenght+width;
System.out.println("Your volume is:"+volume);
System.out.println("Your Area is:"+ Area);
System.out.println("Your perimeter is:"+Perimeter);
}
}
不要將命令行和GUI組合到一個這樣的程序中。您可以使用GUI元素來提示用戶輸入。 – csmckelvey 2014-10-07 22:28:36
對於這個特定的場景,如果你想有一個GUI,你不需要使用掃描儀,只需看看JOptionPane。 http://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html – Ferox 2014-10-07 22:38:56