我是 JFrame 的新手,我想知道为什么我的 JOptionPane 不会停止弹出。当我连续键入 JOptionPane 时,这些值在 JFrame 上不断重叠在前面的值之上,并且它使我的程序处于循环状态,从而阻止打印过程打开。我认为这是因为程序在公共 void 类而不是公共静态 void 主类中请求输入值,但由于图形组件,我不知道如何将这些值放在 JFrame 上。
import java.awt.*;
import java.awt.print.PageFormat;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.swing.*;
public class Assignment3 extends JFrame
{
Assignment3()
{
setTitle("Amber's Skateboarders");
setSize(1200,720);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void paint(Graphics g)
{
Font bigName = new Font("Ariel Bold", Font.BOLD, 20);
Font company = new Font("Comic Sans", Font.BOLD, 12);
Font myList = new Font("Comic Sans", Font.PLAIN, 12);
g.setFont(bigName);
g.drawString("Skateboarders", 15, 100);
String companyName;
String companyAddress;
int itemIndex = 0;
int pricesIndex = 0;
int quantityIndex = 0;
String[] merchandise = new String[1000];
double[] prices = new double[1000];
int[] quantity = new int[1000];
int addMore;
String[] options = {"Yes", "No"};
double subtotal = 0;
double[] result = new double[1000];
String[] list = new String[1000];
companyName = JOptionPane.showInputDialog(null, "What is your company name?");
g.setFont(company);
g.drawString(companyName, 15, 115);
companyAddress = JOptionPane.showInputDialog(null, "What is your company's address?");
g.setFont(company);
g.drawString(companyAddress, 15, 130);
do
{
merchandise[itemIndex] = JOptionPane.showInputDialog(null, "What is the name of the item you want?");
prices[pricesIndex] = Double.parseDouble(JOptionPane.showInputDialog(null, "How much does this cost?"));
quantity[quantityIndex] = Integer.parseInt(JOptionPane.showInputDialog(null, "How much of this item do you need?"));
itemIndex++;
pricesIndex++;
quantityIndex++;
addMore = JOptionPane.showOptionDialog(null, "Do you want to add more to your shopping cart?", "Before you go!", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,null, options, options[0]);
}
while(addMore == 0);
int k;
g.setFont(myList);
if (addMore == 1)
{
for (int i = 0; i <= pricesIndex-1; i++)
{
result[i] = prices[i]*quantity[i];
}
for (double j : result)
{
subtotal += j;
}
for (k = 0; k<pricesIndex; k++)
{
list[k] = String.valueOf(quantity[k])+"\u00D7"+merchandise[k];
g.drawString(list[k], 15, k*15+145);
g.drawString("$" +String.valueOf(result[k]), 120, k*15+145);
//all += String.valueOf(quantity[k])+"\u00D7"+merchandise[k]+"\t\t\t"+String.valueOf(result[k])+"\n";
}
k=pricesIndex;
double taxes = subtotal*0.2;
g.setFont(company);
g.drawString("Subtotal: ", 15, k*15+145);
g.drawString("$"+String.valueOf(subtotal), 120, k*15+145);
k++;
g.drawString("Tax: ", 15, k*15+145);
g.drawString("$"+String.valueOf(taxes), 120, k*15+145);
k++;
g.drawString("Total: $", 15, k*15+145);
g.drawString("$"+String.valueOf(taxes+subtotal), 120, k*15+145);
}
public static void main(String[] args)
{
Assignment3 main = new Assignment3();
main.paint(null);
}
}