【问题标题】:unable to get subtotal, total and sales tax to show in gui无法在 gui 中显示小计、总计和销售税
【发布时间】:2015-05-08 12:54:13
【问题描述】:

首先,由于某种原因,数学在我的 gui 中被搞砸了,所以一本 20.61 的书一直显示它的总数为 16.43 或类似的东西。除此之外,我正在尝试为要在应用程序中显示的销售税、总计和小计创建字段……但唯一显示的是总计。 如何创建与总计显示相同的销售税和小计 sop?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.Scanner;
import java.util.Vector;
import java.text.DecimalFormat;

public class JavaProgram extends JFrame
{
    private final String BOOK_DATA_FILENAME = "bookData1.txt";
    private final int LIST_ROWS_VISIBLE = 5;
    private final String PROGRAM_NAME = "Books'r'Us";

    private JPanel bookPanel;
    private JPanel controlPanel;
    private JPanel cartPanel;
    private JPanel checkOutPanel;

    private JList bookList;
    private JList cartList;

    private JButton addToCartButton;
    private JButton removeFromCartButton;
    private JButton clearCartButton;

    private JTextField totalText;
    private double total = 0.00;

    private Vector<Book> books = new Vector<Book>();
    private Vector<Book> cart = new Vector<Book>();

    public JavaProgram() throws IOException
    {
        setTitle( PROGRAM_NAME );
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

        setLayout( new BorderLayout() );

        buildBookPanel();
        buildControlPanel();
        buildCartPanel();
        buildCheckOutPanel();

        add( bookPanel, BorderLayout.WEST );
        add( controlPanel, BorderLayout.CENTER );
        add( cartPanel, BorderLayout.EAST );
        add( checkOutPanel, BorderLayout.SOUTH );

        pack();

        setLocationRelativeTo( null );
        setVisible( true );
    }

    private void buildBookPanel() throws IOException
    {
        bookPanel = new JPanel();
        bookPanel.setBorder( BorderFactory.createTitledBorder( "Books" ) );

        loadBooks();

        bookList = new JList( books );
        bookList.setVisibleRowCount( LIST_ROWS_VISIBLE );
        JScrollPane scrollPane = new JScrollPane( bookList );

        bookPanel.add( scrollPane );
    }

    private void loadBooks() throws IOException
    {
        File bookFile = new File( BOOK_DATA_FILENAME );
        Scanner inputFile = new Scanner( bookFile );

        while( inputFile.hasNext() )
        {
            String bookDataLine = inputFile.nextLine();
            String[] token = bookDataLine.split( "," );

            Book newBook = new Book();

            newBook.setTitle( token[0] );
            newBook.setAuthor( token[1] );
            newBook.setPrice( Double.parseDouble( token[2] ) );

            books.add( newBook );
        }

        inputFile.close();
    }

    private void buildControlPanel()
    {
        controlPanel = new JPanel();
        controlPanel.setBorder( BorderFactory.createEmptyBorder( 30, 10, 10, 10 ) );

        controlPanel.setLayout( new GridLayout( 4, 1 ) );

        addToCartButton = new JButton( "Add to Cart" );
        addToCartButton.addActionListener( new AddButtonListener() );

        removeFromCartButton = new JButton( "Remove from Cart" );
        removeFromCartButton.addActionListener( new RemoveButtonListener() );

        clearCartButton = new JButton( "Clear Cart" );
        clearCartButton.addActionListener( new ClearButtonListener() );

        controlPanel.add( addToCartButton );
        controlPanel.add( removeFromCartButton ); 
        controlPanel.add( clearCartButton );
    }

    private void buildCartPanel()
    {
        cartPanel = new JPanel();
        cartPanel.setBorder( BorderFactory.createTitledBorder( "Shopping Cart" ) );

        cartList = new JList();
        cartList.setVisibleRowCount( LIST_ROWS_VISIBLE );
        JScrollPane scrollPane = new JScrollPane( cartList );

        cartPanel.add( scrollPane );
    }

    private void buildCheckOutPanel()
    {
        checkOutPanel = new JPanel();

      checkOutPanel.add( new JLabel( "Subtotal" ) );

      totalText = new JTextField( 10 );
        totalText.setEditable( false );

      checkOutPanel.add( new JLabel( "Sales Tax" ) );

      totalText = new JTextField( 10 );
        totalText.setEditable( false );

      checkOutPanel.add( new JLabel( "Total: " ) );

        totalText = new JTextField( 10 );
        totalText.setEditable( false );

        checkOutPanel.add( totalText );
    }

    private void addToCart( Book book )
    {
        cart.add( book );
        cartList.setListData( cart );
        total += book.getPrice();
        updateTotal();
    }

    private void removeFromCart( Book book, int index )
    {
        cart.remove( index );
        cartList.setListData( cart );
        total -= book.getPrice();
        updateTotal();
    }

    private void updateTotal()
    {
        DecimalFormat fmt = new DecimalFormat( "$##,##0.00" );
        totalText.setText( fmt.format( total ) );
    }

    private class AddButtonListener implements ActionListener
   {  
        public void actionPerformed( ActionEvent e )
      {
            if( bookList.getSelectedValue()!=null )
         {
            Book selected = (Book) bookList.getSelectedValue();
            addToCart( selected );
                bookList.clearSelection();
         }
            else
                JOptionPane.showMessageDialog( JavaProgram.this,
                        "Please select a book before adding to cart.", 
                        PROGRAM_NAME, 
                        JOptionPane.PLAIN_MESSAGE );
      }
   }

    private class RemoveButtonListener implements ActionListener
   {      
        public void actionPerformed( ActionEvent e )
      {
            int selectedIndex = cartList.getSelectedIndex();

            if( selectedIndex != -1 )
         {
            Book selected = (Book) cartList.getSelectedValue();
            removeFromCart( selected, selectedIndex );
         }
            else
                JOptionPane.showMessageDialog( JavaProgram.this,
                        "Please select a book from the cart.", 
                        PROGRAM_NAME, 
                        JOptionPane.PLAIN_MESSAGE );
      }
   }

    private class ClearButtonListener implements ActionListener
   {      
        public void actionPerformed( ActionEvent e )
      {
            final int YES = 0;
            final int NO = 1;

            int choice = JOptionPane.showConfirmDialog( JavaProgram.this,
                            "Clear shopping cart?",
                            PROGRAM_NAME,
                            JOptionPane.YES_NO_OPTION );

            if( choice==YES )
            {
                cart.clear();
                cartList.setListData( cart );
                total = 0.00;
                updateTotal();
            }
        }
   }

    public static void main( String[] args ) throws IOException
    {
        new JavaProgram();
    }
}

【问题讨论】:

  • Book 在哪里?还有“书籍文件”?
  • 发布MCVE最小代码,为我们编译并演示问题)。

标签: java swing


【解决方案1】:

为小计和销售税再添加 2 个 JTextField:

private JTextField subTotal;
private JTextField salesTax;
private JTextField totalText;

修改你的 buildCheckOutPanel() 方法:

private void buildCheckOutPanel()
{
    checkOutPanel = new JPanel();

  checkOutPanel.add( new JLabel( "Subtotal" ) );

  subTotal = new JTextField( 10 );
    subTotal.setEditable( false );
    checkOutPanel.add( subTotal );

  checkOutPanel.add( new JLabel( "Sales Tax" ) );

  salesTax = new JTextField( 10 );
    salesTax.setEditable( false );
    checkOutPanel.add( salesTax );

  checkOutPanel.add( new JLabel( "Total: " ) );

    totalText = new JTextField( 10 );
    totalText.setEditable( false );

    checkOutPanel.add( totalText );
}

修改 updateTotal() 方法:

private void updateTotal()
{
    DecimalFormat fmt = new DecimalFormat( "$##,##0.00" );
    subTotal.setText( fmt.format( total ) );
    double tax =total*0.1; //if sales-tax is 10% of total
    salesTax.setText( fmt.format(tax) );
    totalText.setText(fmt.format(total+tax));

}

【讨论】:

  • 我做了你展示的一切,但仍然一无所获......老实说,我不知道如何为这个问题创建一个 n mcve......坦率地说,它让我很恼火,我无法弄清楚这一点解决问题...
  • @kingvicious ...我已经尝试过此代码..它对我来说非常适合...我想说,而不是从文件中获取输入...尝试实现此代码一些静态输入...稍后将其更改为来自文件的输入
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多