私は JFrame を初めて使用するので、JOptionPane のポップアップが止まらない理由を知りたいと思っていました。JOptionPane に連続して入力すると、値が前の JFrame オーバートップでオーバーラップし続け、プログラムがループ状態になり、印刷プロセスが開かれなくなります。プログラムがpublic static void mainクラスではなくpublic 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);
}
}