【发布时间】:2014-06-01 22:09:50
【问题描述】:
我在如何将这个程序放在 JFrame 上时遇到了麻烦。我不确定该怎么做。如果有人给我一些帮助,那就太好了。非常感谢,我很感激。帮助任何人:/?
package VendMach;
import java.util.Scanner;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
public class VendMachMain extends JComponent {
public static class groceryshopping{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int choice = 1;
double subtotal = 0;
double price = 0;
double discount = 0;
do {
System.out.println( "Quick Deli" + "\n" +"1. Lunchables $ 1.99 per box" +
"\n" + "2. Chips $ 0.99 per bag" + "\n" + "3. Chocolate Bars $ 0.99 each" + "\n" +
"4. Soda $ 0.99 a can" + "\n" + "5. Cold Cut Sandwiches $ 2.99 each" + "\n" + "6. Apple Slices $ 0.75 per bag" +
"\n" + "7. Juice Boxes $ 0.99 each" + "\n" + "8. Cinnabons $ 0.99 each" + "\n" + "9. Cookies $ 1.99 per bag" +
"\n" + "10.Gum $ 1.15 per pack" + "\n" + " " + "\n" + "0. Quit" + "\n" + " " + "\n" + "Your subtotal is $ " +(int)(subtotal * 100) / 100.0
+ "\n" );
System.out.println("What would you like to purchase? \nIf you have completed your checkout, enter 0.");
choice = input.nextInt();
if (choice == 0)
break;
System.out.println("How many do you want?");
int qty = input.nextInt();
switch (choice) {
case 1:
price = 1.99;
break;
case 2:
price = 0.99;
break;
case 3:
price = 0.99;
break;
case 4:
price = 0.99;
break;
case 5:
price = 2.99;
break;
case 6:
price = 0.75;
break;
case 7:
price = 0.99;
break;
case 8:
price = 0.99;
break;
case 9:
price = 1.99;
break;
case 10:
price = 1.15;
}
subtotal = subtotal + (qty * price);
}
while(choice > 0);
System.out.println("Do you have the special discount card? (Y/N)");
String discountInput = input.next();
char discountClub = discountInput.charAt(0);
if (discountClub == 'y' || discountClub == 'Y'){
discount = subtotal * .20;
}
double tax = ((subtotal - discount) * 0.075);
double finalCost = subtotal - discount + tax;
JOptionPane.showMessageDialog(null,"The subtotal is $ " + (int)(subtotal * 100) / 100.0 + "\n" + "Discount $ " + (int) (discount * 100) / 100.0
+ "\n" + "Tax $ " + (int)(tax* 100) / 100.0 + "\n" + "Total price $ " + (int)(finalCost * 100) / 100.0);
}
}
}
【问题讨论】:
-
你不能。您的“程序”只不过是带有控制台输入的静态主要方法。为了创建一个 GUI,您必须首先将逻辑提取到真正诚实的 OOP 类中。我建议你先这样做。我会废弃这段代码,思考程序的逻辑,然后从头开始创建我的 GUI。
-
事实上你问了同样类型的问题here。这是懒惰的高度,我建议你停止这样做,不假思索地问类似的问题。
标签: java swing jframe jpanel joptionpane