Java I';我正试图在一个单独的屏幕上打印收据
我正在尝试制作一个出纳程序,在单独的屏幕上打印所选产品的价格。我快到了,但不知道如何做最后一部分 当我选择我的产品时,我会得到以下回报:Java I';我正试图在一个单独的屏幕上打印收据,java,Java,我正在尝试制作一个出纳程序,在单独的屏幕上打印所选产品的价格。我快到了,但不知道如何做最后一部分 当我选择我的产品时,我会得到以下回报: [Ljava.lang.String;@3774e19 而不是像: Pizza Margherita - (eur)6.- 这是我的代码: public class Main extends Application { int screenWidth = 500; int screenHeight = 300; int pizzaCounter = 0
[Ljava.lang.String;@3774e19
而不是像:
Pizza Margherita - (eur)6.-
这是我的代码:
public class Main extends Application {
int screenWidth = 500;
int screenHeight = 300;
int pizzaCounter = 0;
int totalPrice = 0;
double korting = 1.0;
int buttonCounter = 0;
int count = 0;
Text pizzaCounterText = new Text();
final StackPane root = new StackPane();
final StackPane root2 = new StackPane();
public static final ObservableList data = FXCollections.observableArrayList();
ListView<String> receipt = new ListView<>(data);
Button[] buttons = new Button[8];
String[] producten = new String[8];
String[] totaal = new String[200];
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Kassa");
TextArea textArea = new TextArea();
VBox vbox = new VBox(textArea);
receipt.setEditable(false);
receipt.setStyle("-fx-font-size:20.0;");
root2.getChildren().add(receipt);
HBox hbox = new HBox();
Button discount = new Button("10% korting");
Button afrekenen = new Button("Afrekenen!");
discount.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if (korting == 1) {
korting = 0.9;
} else {
korting = 1;
}
}
});
afrekenen.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
primaryStage.setScene(new Scene(root2, screenWidth, screenHeight));
primaryStage.show();
}
});
hbox.getChildren().addAll(discount, afrekenen);
Scene scene = new Scene(vbox, -310, -40);
primaryStage.setScene(scene);
primaryStage.show();
pizzaCounterText.setText("Items bought: " + Integer.toString(pizzaCounter) + "\nTotal: " + Integer.toString(totalPrice) + ",--");
producten[0] = "Pizza Margherita-6";
producten[1] = "Pizza Funghi-10";
producten[2] = "Pizza Prociutto-8";
producten[3] = "Pizza Hawaii-11";
producten[4] = "Pizza Shoarma-12";
producten[5] = "Pizza Salami-10";
producten[6] = "Cola Regular-2";
producten[7] = "Cola Zero-2";
// maakt de buttons
for (int i = 0; i < 8; i++) {
String[] parts = producten[i].split("-");
buttons[i] = createButton(i, parts[0], Integer.parseInt(parts[1]));
}
root.setAlignment(TOP_LEFT);
pizzaCounterText.setTranslateX(screenWidth - 300);
pizzaCounterText.setTranslateY(screenHeight - 40);
vbox.setTranslateX(screenWidth - 500);
vbox.setTranslateY(screenHeight - 40);
hbox.setTranslateX(screenWidth - 350);
hbox.setTranslateY(screenHeight - 90);
root.getChildren().add(hbox);
root.getChildren().add(vbox);
root.getChildren().add(pizzaCounterText);
primaryStage.setScene(new Scene(root, screenWidth, screenHeight));
primaryStage.show();
}
public Button createButton(int index, String name, int price) {
Button btn = new Button();
btn.setText(name);
btn.setMinWidth(100);
int column = buttonCounter % 3;
int row = buttonCounter / 3;
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
totalPrice += price;
pizzaCounter++;
do {
totaal[count] = name;
count++;
data.add(producten);
} while (bonActive());
pizzaCounterText.setText("Items bought: " + Integer.toString(pizzaCounter) + "\nTotal: " + Double.toString(totalPrice * korting));
System.out.println(Arrays.deepToString(totaal));
}
});
btn.setTranslateX(50 + 150 * column);
btn.setTranslateY(35 + 50 * row);
root.getChildren().add(btn);
buttonCounter++;
return btn;
}
public boolean bonActive() {
return false;
}
public void bon() {
for (int i = 0; i < totaal.length; i++) {
System.out.println(totaal[i]);
}
}
public类主扩展应用程序{
int屏幕宽度=500;
int屏幕高度=300;
int pizzaccounter=0;
int totalPrice=0;
双korting=1.0;
int buttonCounter=0;
整数计数=0;
Text pizzaccountertext=新文本();
最终StackPane根=新StackPane();
最终StackPane root2=新StackPane();
公共静态最终ObservableList data=FXCollections.observableArrayList();
ListView接收=新ListView(数据);
按钮[]按钮=新按钮[8];
字符串[]producten=新字符串[8];
字符串[]totaal=新字符串[200];
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
公共无效开始(阶段primaryStage){
初级阶段。设置标题(“Kassa”);
TextArea TextArea=新建TextArea();
VBox VBox=新的VBox(文本区域);
可编辑的收据(假);
收据.setStyle(“-fx字体大小:20.0;”);
root2.getChildren().add(收据);
HBox HBox=新的HBox();
按钮折扣=新按钮(“10%折扣”);
按钮afrekenen=新按钮(“afrekenen!”);
setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
如果(korting==1){
korting=0.9;
}否则{
korting=1;
}
}
});
setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
设置场景(新场景(root2,屏幕宽度,屏幕高度));
primaryStage.show();
}
});
hbox.getChildren().addAll(折扣,afrekenen);
场景=新场景(vbox,-310,-40);
初级阶段。场景(场景);
primaryStage.show();
PizzaccounterText.setText(“购买的物品:“+Integer.toString(Pizzaccounter)+”\n总计:“+Integer.toString(totalPrice)+”,-”;
producten[0]=“披萨玛格丽塔-6”;
producten[1]=“披萨丰喜-10”;
producten[2]=“Pizza Prociutto-8”;
producten[3]=“夏威夷比萨饼-11”;
producten[4]=“披萨Shoarma-12”;
producten[5]=“比萨萨拉米-10”;
producten[6]=“可乐常规-2”;
producten[7]=“可乐零-2”;
//纽扣
对于(int i=0;i<8;i++){
字符串[]parts=producten[i]。拆分(“-”;
buttons[i]=createButton(i,部分[0],整数.parseInt(部分[1]);
}
根.设置对齐(左上角);
setTranslateX(屏幕宽度-300);
PizzaccounterText.setTranslateY(屏幕高度-40);
vbox.setTranslateX(屏幕宽度-500);
vbox.setTranslateY(屏幕高度-40);
setTranslateX(屏幕宽度-350);
hbox.setTranslateY(屏幕高度-90);
root.getChildren().add(hbox);
root.getChildren().add(vbox);
root.getChildren().add(pizzaccounterText);
设置场景(新场景(根、屏幕宽度、屏幕高度));
primaryStage.show();
}
公共按钮createButton(整数索引、字符串名称、整数价格){
按钮btn=新按钮();
btn.setText(名称);
btn.设置最小宽度(100);
int column=按钮计数器%3;
int row=按钮计数器/3;
btn.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
总价+=价格;
pizzaccounter++;
做{
总数[计数]=名称;
计数++;
data.add(producten);
}while(bonActive());
PizzaccounterText.setText(“购买的物品:“+Integer.toString(Pizzaccounter)+”\n总计:“+Double.toString(totalPrice*korting));
System.out.println(Arrays.deepToString(totaal));
}
});
btn.setTranslateX(50+150*列);
btn.setTranslateY(35+50*行);
root.getChildren().add(btn);
按钮计数器++;
返回btn;
}
公共布尔活动(){
返回false;
}
公共图书馆{
for(int i=0;i
}每次数组
字符串[]producten
显示此对象时,您都在添加可观察列表数据。我想你想做的是:
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
totalPrice += price;
pizzaCounter++;
do {
totaal[count] = name;
count++;
data.add(name + " " + price);
} while (bonActive());
pizzaCounterText.setText("Items bought: " + Integer.toString(pizzaCounter) + "\nTotal: " + Double.toString(totalPrice * korting));
System.out.println(Arrays.deepToString(totaal));
}
});
并且在泛型对象上有一个强类型:observeListData=FXCollections.observearraylist()代码>顺便说一句,korting意味着折扣,afrekenen意味着检查下一次可能的重复。在你发布问题之前,你应该删除尽可能多的代码,直到你得到答案。您发布的90%代码与您的问题无关。
static class Pizza {
String name;
int price;
public Pizza(String name, int price) {
this.name = name;
this.price = price;
}
@Override
public String toString() {
return "Pizza{" +
"name='" + name + '\'' +
", price=" + price +
'}';
}
}