1

我是 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);
}
}
4

2 回答 2

1

正如在 AWT 和 Swing 中的绘画:绘画方法中所讨论的,“Swing 程序应该覆盖paintComponent()而不是覆盖paint()”。在不受您控制的回调中唤起对话框也是非常不寻常的。由于您主要是在打电话drawString(),您可能会发现简单地更新组件(例如JLabelJTextFieldvia )会更容易setText()。如果要自动更新绘图区域,请使用无模式对话框,如下所示

于 2013-11-12T07:54:17.563 回答
1

我认为你的问题在下一个:

1)您在方法内部调用您的 JOptionPane paint(),这是正确的方法。

2) 为什么你用 来绘制你的项目Graphics,我认为你可以使用@trashgod 推荐的JLabel's 或's。JTextField我建议您使用JTable,我认为它适合您的情况。阅读教程

3)您可以在按钮操作(按钮教程)的帮助下添加新项目,而不是while循环。

4)如果你真的想在组件上绘制你的数据。扩展JPanel类,将它的实例添加到您的并在方法中JFrame绘制。paintComponents(Graphics g)

于 2013-11-12T08:04:57.963 回答