我是JFrame的新手,我想知道为什么我的JOptionPane不会停止弹出。当我连续输入JOptionPane时,这些值在JFrame上面与前一个值重叠,并且它使我的程序保持循环,防止打印过程打开。我认为这是因为程序在公共void类而不是public static void main类中请求输入值,但我不知道如何将这些值放在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);
}
}
答案 0 :(得分:1)
我认为你的问题在下一个:
1)你在paint()
方法中调用了你的JOptionPane,这是正确的方法。
2)为什么用Graphics
绘制项目,我认为你可以按照@trashgod的推荐使用JLabel
或JTextField
。我建议您使用JTable
,我认为它适合您的情况。请阅读tutorial。
3)您可以借助按钮操作(tutorial for button)而不是while
循环来添加新项目。
4)如果你想真正在组件上绘制数据。扩展JPanel
课程,将其实例添加到JFrame
并使用paintComponents(Graphics g)
方法绘制。