我是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]+"ttt"+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);
}
}
我认为你的问题在下一个:
1)你在方法paint()
调用你的JOptionPane,这是正确的方式。
2)为什么你用Graphics
画你的项目,我认为你可以按照@trashgod的建议使用JLabel
或JTextField
。我建议您使用JTable
,我认为它适合您的情况。为此阅读教程。
3)您可以借助按钮操作(按钮教程)而不是while
循环来添加新项目。
4)如果你想在组件上实际绘制数据。扩展JPanel
类,将其实例添加到JFrame
并在paintComponents(Graphics g)
方法中绘制。
正如 AWT 中的绘画和 Swing:绘画方法中所讨论的,"Swing 程序应该覆盖paintComponent()
而不是覆盖paint()
"。在不受您控制的回调中调用对话框也是非常不寻常的。由于您主要调用drawString()
因此您可能会发现通过setText()
简单地更新JLabel
和JTextField
等组件更容易。如果要自动更新绘图区域,请使用此处所示的无模式对话框。