以下示例: http://code.makery.ch/blog/javafx-8-event-handling-examples/ 我已经制作了一个涉及组合框的基本GUI。在运行我的程序时,没有任何项目被添加到组合框中。如果你能指出我的错误,为什么它可能不会将这些项目添加到组合框中,我将非常感激。 这是我的项目的ZIP文件: https://www.dropbox.com/home/Project-JavaFX 提前谢谢!
Main.java:
package Main;
import Items.Item;
import Items.ItemManager;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class Main extends Application {
private static myController uj = new myController();
private static ItemManager im = new ItemManager();
public static void main(String[] args) {
uj.initialize();
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Pane mainPane = (Pane) FXMLLoader.load(Main.class.getResource("Main.fxml"));
primaryStage.setScene(new Scene(mainPane));
primaryStage.show();
}
}
Main.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.net.*?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.layout.AnchorPane?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="720.0" prefWidth="1280.0" stylesheets="@myStyle.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Main.myController">
<children>
<Rectangle arcHeight="5.0" arcWidth="5.0" fill="WHITE" height="196.0" layoutX="478.0" layoutY="-2.0" stroke="BLACK" strokeType="INSIDE" width="343.0" />
<ImageView fitHeight="568.0" fitWidth="684.0" layoutX="356.0" layoutY="167.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../../../Pictures/builder.jpg" />
</image>
</ImageView>
<ImageView fitHeight="885.0" fitWidth="857.0" layoutX="629.0" layoutY="187.0" pickOnBounds="true" preserveRatio="true" rotate="90.0">
<image>
<Image url="@../../../../Pictures/Uroburos-rootkit-Espionage-Russia.jpg" />
</image>
</ImageView>
<ImageView fitHeight="885.0" fitWidth="857.0" layoutX="-187.0" layoutY="187.0" pickOnBounds="true" preserveRatio="true" rotate="90.0">
<image>
<Image url="@../../../../Pictures/Uroburos-rootkit-Espionage-Russia.jpg" />
</image>
</ImageView>
<Line endX="100.0" endY="400.0" layoutX="381.0" layoutY="324.0" startX="100.0" startY="-325.0" />
<Line endX="425.0" endY="390.0" layoutX="391.0" layoutY="334.0" startX="425.0" startY="-335.0" />
<ComboBox fx:id="stocks" layoutX="38.0" layoutY="135.0" onAction="#itemPicked" prefHeight="46.0" prefWidth="421.0" promptText="Please Select and Item" />
<ScrollBar fx:id="currentP" layoutX="857.0" layoutY="108.0" onDragDetected="#getIndexC" orientation="VERTICAL" prefHeight="100.0" prefWidth="400.0" />
<Button fx:id="remove" layoutX="1007.0" layoutY="53.0" mnemonicParsing="false" onAction="#deleteItem" prefHeight="43.0" prefWidth="250.0" text="Remove From Order">
<font>
<Font name="Arial Black" size="20.0" />
</font>
</Button>
<ImageView fitHeight="72.0" fitWidth="79.0" layoutX="491.0" layoutY="74.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../../../Pictures/Uroburos-rootkit-Espionage-Russia.jpg" />
</image>
</ImageView>
<Label layoutX="491.0" layoutY="14.0" prefHeight="60.0" prefWidth="311.0" text="RootKit At your Service" textAlignment="JUSTIFY">
<font>
<Font name="Courier New Bold Italic" size="22.0" />
</font>
</Label>
<Label fx:id="message" layoutX="587.0" layoutY="87.0" text=""Protecting" your trade">
<font>
<Font name="Courier New Italic" size="15.0" />
</font>
</Label>
<Button fx:id="adding" layoutX="38.0" layoutY="81.0" mnemonicParsing="false" onAction="#addItem" text="Add Item To Order">
<font>
<Font name="Arial Black" size="20.0" />
</font>
</Button>
<TextField fx:id="quantity" layoutX="38.0" layoutY="194.0" onAction="#getQuantity" prefHeight="54.0" prefWidth="62.0" promptText=""1"">
<font>
<Font size="20.0" />
</font></TextField>
<Label layoutX="108.0" layoutY="196.0" text="Quantity" textFill="WHITE">
<font>
<Font name="Arial Black" size="34.0" />
</font>
</Label>
</children>
</Pane>
myController.java:
package Main;
import Items.Item;
import Items.ItemManager;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollBar;
import javafx.scene.control.TextField;
public class myController {
@FXML
private TextField quantity;
@FXML
private Button adding;
@FXML
private ScrollBar currentP;
@FXML
private Label message;
@FXML
public static ComboBox stocks = new ComboBox();
@FXML
private Button remove;
public static ObservableList<String> options = FXCollections.observableArrayList();
int count = 0;
private static ItemManager im = new ItemManager();
@FXML
void itemPicked(ActionEvent event) {
}
@FXML
void getIndexC(ActionEvent event) {
}
@FXML
void deleteItem(ActionEvent event) {
}
@FXML
void addItem(ActionEvent event) {
}
@FXML
void getQuantity(ActionEvent event) {
}
public void initialize() {
// TODO Auto-generated method stub
for(int i = 0; i < im.inventoryItems.size(); i++) {
options.add(im.inventoryItems.get(i).getName());
}
stocks.setItems(options);
}
}
Item.java:
package Items;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
public class Item {
//Items definitions
public static Item hammer = new Item(0, "Hammer", 5.0);
public static Item tape = new Item(1, "Tape", 3.0);
public static Item test = new Item(2, "Rare", 4.0);
protected int quantity, id;
protected String name;
protected BufferedImage sprite;
protected double price, totalPrice;
public static void updateTotalPrice(Item i) {
i.setTotalPrice(i.getQuantity()*i.getPrice());
}
public Item (int id, String name, double price) {
this.id = id;
this.name = name;
this.price = price;
//quantity = 1;
//this.totalPrice = price;
}
//Getters and Setters
public int getQuantity() {
return quantity;
}
public double getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(double totalPrice) {
this.totalPrice = totalPrice;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
ItemManager.java:
package Items;
import java.util.ArrayList;
public class ItemManager {
public static ArrayList<Item> inventoryItems;
public static void ItemManager() {
inventoryItems = new ArrayList<Item>();
inventoryItems.add(Item.hammer);
inventoryItems.add(Item.tape);
inventoryItems.add(Item.test);
}
public Item getItemFromID(int id) {
for (Item i : inventoryItems) {
if (i.getId() == id) {
return i;
}
}
return null;
}
public void addItem(Item i, int quantity) {
boolean flag = false;
for (Item item : inventoryItems) {
if (i.getId() == item.getId()) {
item.setQuantity(item.getQuantity()+quantity);
Item.updateTotalPrice(item);
flag = true;
//call the method to update the information box in the inventory GUI
break;
}
}
if (!flag) {
inventoryItems.add(i);
//call method for adding new info box in inventory GUI
}
}
public void removeItem(int id) {
for (Item item : inventoryItems) {
if (item.getId() == this.getItemFromID(id).getId()) {
item.setQuantity(item.getQuantity()-1);
if (item.getQuantity() <= 0) {
inventoryItems.remove(item);
}
break;
}
}
}
//Getters and Setters
}
答案 0 :(得分:0)
我建议改变一些事项:
private static myController uj = new myController();
private static ItemManager im = new ItemManager();
以及
uj.initialize();
应从Main
课程中删除。控制器的initialize
方法由FXMLLoader
自动调用。此外,uj
与您的fxml使用的实例不同。 im
字段是不必要的,因为它从未在类中使用过。
public static void ItemManager() {
inventoryItems = new ArrayList<Item>();
inventoryItems.add(Item.hammer);
inventoryItems.add(Item.tape);
inventoryItems.add(Item.test);
}
这既不是构造函数也不是创建对象的方法。它从未使用过,但它似乎服务于“静态构造函数”的目的。您应该使用静态初始化程序来初始化static
字段:
public final static ArrayList<Item> inventoryItems;
static {
inventoryItems = new ArrayList<Item>();
inventoryItems.add(Item.hammer);
inventoryItems.add(Item.tape);
inventoryItems.add(Item.test);
}
此外,static
类中存在ItemsManager
数据和非静态方法的不健康混合。我建议使用正确的
@FXML
public static ComboBox stocks = new ComboBox();
fxml元素不会注入静态字段。因此,该字段应更改为:
@FXML
private ComboBox stocks;
另外,我建议使用非静态options
字段或在static
初始化程序中初始化的字段,因为多次加载场景会导致相同的项目反复添加。
此外,如果您想保留ItemManager.inventoryItems
字段static
,im
字段是不必要的,您只使用该类的static
成员,这是不好的做法,也是不必要的您可以使用类名而不是实例:
public void initialize() {
// TODO Auto-generated method stub
for (int i = 0; i < ItemManager.inventoryItems.size(); i++) {
options.add(ItemManager.inventoryItems.get(i).getName());
}
stocks.setItems(options);
}