Java - JComponent(paintcomponent)和JOptionPane问题?

时间:2014-10-06 22:48:58

标签: java paintcomponent jcomponent

我在我的java编程类中使用Eclipse(Mars)作为项目。我遇到了一个小问题,并想知道我是否能得到一些帮助。 我的程序用于以下内容: 编写程序以直观显示(使用JComponent和Paint Component计算销售额。在线零售商销售五种产品,其价格如下。 1.牛仔靴74.99 2.牧马人25.99 3.牛仔帽24.95 4.章节34.89 5.马刺12.50

我遇到的问题是Transaction.class中的getItem()方法。当我运行代码时,它似乎陷入循环,即使我有一个值为6的断点。当我再次输入6时(使用jOptionPane)它似乎崩溃并将JOptionPane打印到paintComponent中。如果有人能提供帮助我会非常感激。

这是我的代码。

Main.class

package project1_JaredMcMillian;

import javax.swing.JFrame; import javax.swing.JOptionPane;

public class Main {

private double boots = 74.99;
private double jeans = 25.99;
private double hat = 24.95;
private double chaps = 38.89;
private double spurs = 12.50;

public static void main(String[] args) {
    //*******************************************************************//
    // Name: Jared McMillian
    //Program Name: Project 1
    //Purpose: To calculate and display sales transactions of products.
    //Created: 10/5/2014
    //Due Date: 10/6/2014
    //*******************************************************************//
    int transCount = 1;

    do {
    JFrame frame = new JFrame();
    frame.setSize(500, 700);
    frame.setTitle("Transaction #" + transCount);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Transaction reciept = new Transaction(transCount);


    RecieptComponent trans = new RecieptComponent(transCount, reciept);

    frame.add(trans);

    frame.setVisible(true);

    if (transCount != 3)
    {
        int newTrans = JOptionPane.showConfirmDialog(null, "Would you like to make another Transaction?");
        if (newTrans == 1)
            break;
    }

    transCount++;
    } while (transCount <= 3);
}

}

DrawingComponent.class
----------------------------------------------------------------------------------------

package project1_JaredMcMillian;

import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JComponent;

public class RecieptComponent extends JComponent {
//member instance fields,
private int transCount;
private Transaction reciept;


public RecieptComponent(int transNum, Transaction rec)
{
    transCount = transNum;
    reciept = rec;
}

    public void paintComponent(Graphics f)
    {
        Graphics2D g = (Graphics2D) f;


        reciept.getItems(g);
        reciept.printReciept(g);
        double x = reciept.getTotal();
        g.drawString("Total = " + x, 50, 250);


    }

}

Transaction.class
----------------------------------------------------------------------------------------

package project1_JaredMcMillian;

import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JComponent;
import javax.swing.JOptionPane;

public class Transaction extends JComponent {
//Member Instance Fields.
private int transCount;
private double total;
private int itemsBought[] = {0,0,0,0,0,0,0,0,0,0,0};



public Transaction (int count)
{
    transCount = count;
}

//Accessor Methods.

public void transTitle(Graphics2D g)
{
    g.drawString("Hello! Welcome to J-Mart! We can satisfy all of your rowdy cowboy      needs!", 20, 15);
    g.drawString("Our products consist of: ", 20, 30);
    g.drawString("1. D. Martin Cowboy Boots -- $74.99", 40, 60);
    g.drawString("2. Wrangler Jeans ---------- $25.99", 40, 75);
    g.drawString("3. J. Millan Cowboy Hat ---- $24.95", 40, 90);
    g.drawString("4. Rough-Rider Chaps ------- $38.89", 40, 105);
    g.drawString("5. Chrome Vanadium Spurs --- $12.50", 40, 120);
}

public void getItems(Graphics2D g)
{

    for (int x = 0; x < 20; x++)
    {

        itemsBought[x] = Integer.parseInt(JOptionPane.showInputDialog("Which item (#)                would you like to purchase? (Finish = 6)"));
        if (itemsBought[x] == 6)
            break;

    }


}

public double getTotal()
{
    for (int i = 0;i<itemsBought.length;i++)
        switch(itemsBought[i])
        {
        case 1:
            total+=74.99;
            break;
        case 2:
            total+=25.99;
            break;
        case 3:
            total+=24.95;
            break;
        case 4:
            total+=38.89;
            break;
        case 5:
            total+= 12.50;
            break;

        default: 
            break;
        }

    return total;       
}

public void printReciept(Graphics2D g)
{
    int x = 0;
    g.drawString("Your Items:", 20, 150);
    for (int i=0; i<itemsBought.length; i++)
    {

        if (itemsBought[i] == 1)
            g.drawString("1. D. Martin Cowboy Boots -- $74.99", 30, 165);
        if (itemsBought[i] == 2)
            g.drawString("2. Wrangler Jeans ---------- $25.99", 30, 180);
        if (itemsBought[i] == 3)
            g.drawString("3. J. Millan Cowboy Hat ---- $24.95", 30, 195);
        if (itemsBought[i] == 4)
            g.drawString("4. Rough-Rider Chaps ------- $38.89", 30, 210);
        if (itemsBought[i] == 5)
            g.drawString("5. Chrome Vanadium Spurs --- $12.50", 30, 225);
        if (itemsBought[i] == 6)
        {
            break;
        }

      }
   }
}

0 个答案:

没有答案