价格框输出不正确-输出=giberish

  • 本文关键字:输出 giberish 不正确 java
  • 更新时间 :
  • 英文 :


当我测试添加按钮以查看它是否会将值相加时,输出将显示为价格(胡言乱语(Label@7a6d7fd9[styleClass=label]"

我知道这与有关

Label price = new Label("Price" + " " + answer);

可能是下面底部的答案

answer.setText(answer.getText() + myFormatter.format(tax + totalCost));

全代码

package shoppingcart;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.VBox;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
import javax.swing.JList;
import java.awt.List;
import java.io.File;
import java.io.FileNotFoundException;
import java.text.DecimalFormat;
import java.util.Scanner;
public class ShoppingCart extends Application
{
private Label answer;
private Label price;
private JList listItems;
private String[] listArray = new String[7];
private List cartItems = new List();
//int cartItems = 0;
private final double salesTax = 0.06;
public static void main(String[] args) throws FileNotFoundException
{
launch(args);
}
@Override
public void start(Stage primaryStage) throws FileNotFoundException
{
answer = new Label("");
price = new Label("");
String line;
int index = 0;
File file = new File("BookPrices.txt");
Scanner fileReader = new Scanner(file);
while (fileReader.hasNext())
{
line = fileReader.nextLine();
String[] titles = line.split(",");
listArray[index] = titles[0];
index++;
}
//list view items book
ListView < String > listView = new ListView < > ();
listView.setPrefSize(200, 170);
listView.getItems().addAll(listArray);

// create label to display the selection
Label selectedNameLabel = new Label("Select a Book");
Label price = new Label("Price" + " " + answer);
//Button for selection
Button addButton = new Button("Add to Cart");
addButton.setOnAction(new AddButtonListener());
//Delete button
Button removeButton = new Button("Remove Item");
removeButton.setOnAction(new RemoveButtonListener());

//Controls to VBox
VBox vbox = new VBox(10, listView, selectedNameLabel,
price, addButton, removeButton);
vbox.setPadding(new Insets(10));
vbox.setAlignment(Pos.CENTER);
//Show
Scene scene = new Scene(vbox);
primaryStage.setScene(scene);
primaryStage.show();
}

public class AddButtonListener implements EventHandler < ActionEvent >
{
@Override
public void handle(ActionEvent e)
{
//String value;
//value = (String) listItems.getSelectedValue();
String value = listItems.getSelectedValue().toString();
cartItems.add(value);
}
}
public class RemoveButtonListener implements EventHandler < ActionEvent >
{
@Override
public void handle(ActionEvent e)
{
// String value;
//value = (String) listItems.getSelectedValue();
String value = listItems.getSelectedValue().toString();
cartItems.remove(value);
}
}

public class ButtonClickHandler1 implements EventHandler < ActionEvent >
{
@Override
public void handle(ActionEvent e)
{
String line;
double totalCost = 0.0, costOfItem = 0.0;
File file = new File("BookPrices.txt");
Scanner fileReader = null;
try
{
fileReader = new Scanner(file);
}
catch (FileNotFoundException el)
{
el.printStackTrace();
}
while (fileReader.hasNextLine())
{
line = fileReader.nextLine();
String[] cost = line.split(",");
String title = cost[0];
costOfItem = Double.parseDouble(cost[1]);
for (int i = 0; i < cartItems.getItemCount(); i++)
{
if (title.equals(cartItems.getItem(i)))
totalCost += costOfItem;
}
}
double tax = salesTax * totalCost;
DecimalFormat myFormatter = new DecimalFormat("###.##");
answer.setText(answer.getText() + myFormatter.format(tax + totalCost));
}
}
}

GibberishLabel@7a6d7fd9[styleClass=label]实际上只是Object的常见toString()调用。

您需要在此处添加.getText()

Label price = new Label("Price" + " " + **answer.getText()**);

最新更新