gpt4 book ai didi

java - 无法在 GUI 中显示小计、总计和销售税

转载 作者:太空宇宙 更新时间:2023-11-04 13:53:00 26 4
gpt4 key购买 nike

首先,由于某种原因,数学在我的 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();
}
}

最佳答案

再添加 2 个 JTextFields 用于小计和销售税:

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));

}

关于java - 无法在 GUI 中显示小计、总计和销售税,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30115521/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com