JavaFXでImageViewを使用して画像を追加しようとしています。画像を追加しようとする前にコードが完全に正常に動作していましたが、エラーは表示されませんが、実行前に終了します。私はEclipseを使用しています。そのため、すべてのWeb Designパッケージをインストールしました。それはその理由であり、助けにはなりませんでした。ここでJavaFX:画像を追加しようとするとプログラムが実行されずに終了する
が私のコードで、画像が追加されている唯一のトップビットが重要である:
public class MainGUI extends Application{
Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn10;
Text info1, error1, error2, error3, error4, error5, form1, form2, form3, form4, form5, form6, form7, form8, form9, form10;
Label lbl1, lbl2, lbl3, lbl4, lbl5;
TextField input1, input2, input3, input4, input5, input6, input7, input8, input9, input10;
GridPane pane1, pane2, pane3, pane4, pane5;
Scene scene1, scene2, scene3, scene4, scene5;
ImageView imgView1;
Image img1;
Stage stage;
public void start(Stage primaryStage) {
stage = primaryStage;
imgView1 = new ImageView();//this terminates my code
img1 = new Image(MainGUI.class.getResourceAsStream("src/add_medication_button.png"));//this terminates my code
imgView1.setImage(img1);//this terminates my code
btn1 = new Button("Add Existing Medication");
btn2 = new Button("Add New Medication");
btn3 = new Button("Submit Medication");
btn4 = new Button("Submit Medication");
btn5 = new Button("Back");
btn6 = new Button("Back");
btn7 = new Button("Back");
btn8 = new Button("Current Medication Details");
btn9 = new Button("Quit");
btn10 = new Button("Back");
info1 = new Text(getMedName());
form1 = new Text("Pharmaceutical Company: ");
form2 = new Text("Medication: ");
form3 = new Text("Strength (mg): ");
form4 = new Text("Barcode: ");
form5 = new Text("No. Tablets per Package: ");
form6 = new Text("Pharmaceutical Company: ");
form7 = new Text("Medication: ");
form8 = new Text("Strength (mg): ");
form9 = new Text("Barcode: ");
form10 = new Text("No. Tablets per Package: ");
error1 = new Text();
error2 = new Text();
error3 = new Text();
error4 = new Text();
error5 = new Text();
input1 = new TextField();
input2 = new TextField();
input3 = new TextField();
input4 = new TextField();
input5 = new TextField();
input6 = new TextField();
input7 = new TextField();
input8 = new TextField();
input9 = new TextField();
input10 = new TextField();
btn1.setOnAction(e -> ButtonClicked(e));
btn2.setOnAction(e -> ButtonClicked(e));
btn3.setOnAction(e -> ButtonClicked(e));
btn4.setOnAction(e -> ButtonClicked(e));
btn5.setOnAction(e -> ButtonClicked(e));
btn6.setOnAction(e -> ButtonClicked(e));
btn7.setOnAction(e -> ButtonClicked(e));
btn8.setOnAction(e -> ButtonClicked(e));
btn9.setOnAction(e -> ButtonClicked(e));
btn10.setOnAction(e -> ButtonClicked(e));
lbl1 = new Label("~~~~~~~~~~~~~~~PMA Main Page~~~~~~~~~~~~~~~~~~");
lbl2 = new Label("~~~~~~~~~~Add Existing Medication~~~~~~~~~~~~~");
lbl3 = new Label("~~~~~~~~~~~~Add New Medication~~~~~~~~~~~~~~~~");
lbl4 = new Label("~~~~~~~~~~~~Medication Details~~~~~~~~~~~~~~~~~~~~");
lbl5 = new Label("~~~~~~~~~~Traffic Light Evaluation~~~~~~~~~~~~");
pane1 = new GridPane();
pane2 = new GridPane();
pane3 = new GridPane();
pane4 = new GridPane();
pane1.setVgap(10);
pane2.setVgap(10);
pane3.setVgap(10);
pane4.setVgap(10);
pane1.add(lbl1, 0, 0, 2, 1);
pane1.add(info1, 1, 1, 1, 4);
pane1.add(btn1, 0, 1);
pane1.add(btn2, 0, 2);
pane1.add(btn8, 0, 3);
pane1.add(btn9, 0, 4);
pane2.add(lbl2, 0, 0, 2, 1);
pane2.add(form1, 0, 1);
pane2.add(form2, 0, 2);
pane2.add(form3, 0, 3);
pane2.add(form4, 0, 4);
pane2.add(form5, 0, 5);
pane2.add(input1, 1, 1);
pane2.add(input2, 1, 2);
pane2.add(input3, 1, 3);
pane2.add(input4, 1, 4);
pane2.add(input5, 1, 5);
pane2.add(btn3, 1, 6);
pane2.add(btn5, 0, 6);
pane3.add(lbl3, 0, 0, 2, 1);
pane3.add(form6, 0, 1);
pane3.add(form7, 0, 2);
pane3.add(form8, 0, 3);
pane3.add(form9, 0, 4);
pane3.add(form10, 0, 5);
pane3.add(input6, 1, 1);
pane3.add(input7, 1, 2);
pane3.add(input8, 1, 3);
pane3.add(input9, 1, 4);
pane3.add(input10, 1, 5);
pane3.add(btn4, 1, 6);
pane3.add(btn6, 0, 6);
pane4.add(lbl4, 0, 0, 2, 1);
pane4.add(btn10, 0, 2);
scene1 = new Scene(pane1, 500, 400);
scene2 = new Scene(pane2, 500, 400);
scene3 = new Scene(pane3, 500, 400);
scene4 = new Scene(pane4, 500, 400);
primaryStage.setTitle("PMA System");
primaryStage.setScene(scene1);
primaryStage.show();
}
public void ButtonClicked(ActionEvent e) {
if(e.getSource()==btn1)
stage.setScene(scene2);
else if(e.getSource()==btn2)
stage.setScene(scene3);
else if(e.getSource()==btn3) {
String newString = getFormData();
addToMedication(newString);
String newMeds = getMedName();
System.out.println(newMeds);
info1.setText("");
info1 = new Text(getMedName());
pane1.add(info1, 1, 1, 1, 4);
input1.setText("");
input2.setText("");
input3.setText("");
input4.setText("");
input5.setText("");
stage.setScene(scene1);
}
else if(e.getSource()==btn4) {
String newString = getFormData();
addToMedication(newString);
String newMeds = getMedName();
System.out.println(newMeds);
info1.setText("");
info1 = new Text(clearMedDetails());
pane1.add(info1, 1, 1, 1, 4);
stage.setScene(scene1);
}
else if(e.getSource()==btn5) {
input1.setText("");
input2.setText("");
input3.setText("");
input4.setText("");
input5.setText("");
stage.setScene(scene1);
}
else if(e.getSource()==btn6) {
input6.setText("");
input7.setText("");
input8.setText("");
input9.setText("");
input10.setText("");
stage.setScene(scene1);
}
else if(e.getSource()==btn7)
stage.setScene(scene1);
else if(e.getSource()==btn8) {
createTable();
stage.setScene(scene4);
}
else if(e.getSource()==btn9)
System.exit(0);
else if(e.getSource()==btn10) {
stage.setScene(scene1);
}
else
stage.setScene(scene1);
}
public String getFormData() {
String pharmComp = input1.getText();
String medication = input2.getText();
String strength = input3.getText();
String barCode = input4.getText();
String tablets = input5.getText();
int barCodeLength = barCode.length();
try {
float strengthFloat = Float.parseFloat(strength);
} catch (Exception e) {
System.err.println("Strength field should be a float not string.");
}
try {
int barCodeInt = Integer.parseInt(barCode);
} catch (Exception e) {
System.err.println("Barcode field should be an integer not string.");
}
try {
int tabletsInt = Integer.parseInt(tablets);
} catch (Exception e) {
System.err.println("Tablets should be an integer not string.");
}
if(barCodeLength == 13){
} else {
System.err.println("Barcode must be 13 digits long.");
}
String newMedString = "";
newMedString = newMedString + pharmComp + "," + medication + "," + strength + "," + barCode + "," + tablets;
System.out.println(newMedString);
return newMedString;
}
public String getMedName() {
String filename = "userMed.txt";
String currentmed = "";
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
String currentline;
while ((currentline = br.readLine()) != null) {
String[] arrayline = currentline.split(",");
String toprint = arrayline[1];
System.out.println(toprint);
currentmed = currentmed + toprint + "\n";
}
}
catch (IOException e) {
e.printStackTrace();
}
return currentmed;/*this should just return names of medication*/
}
public String clearMedDetails() {
return "";
}
public void addToMedication(String addThis) {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter("userMed.txt",true));
bw.newLine();
bw.write(addThis);
bw.flush();
}
catch (IOException e){
e.printStackTrace();
}
finally {
if (bw != null) try {
bw.close();
}
catch (IOException e2) {
e2.printStackTrace();
}
}
populateTable(addThis);
}
public void createTable() {
TableView<Medication> table = new TableView<Medication>();
// Create column UserName (Data type of String).
TableColumn<Medication, String> pharmCompCol //
= new TableColumn<Medication, String>("Pharmaceutical Company");
// Create column Email (Data type of String).
TableColumn<Medication, String> medicationCol//
= new TableColumn<Medication, String>("Medication");
// Create column FullName (Data type of String).
TableColumn<Medication, String> strengthMgCol//
= new TableColumn<Medication, String>("Strength (mg)");
// Create 2 sub column for FullName.
TableColumn<Medication, String> barCodeCol//
= new TableColumn<Medication, String>("Barcode");
TableColumn<Medication, String> noTabletsCol //
= new TableColumn<Medication, String>("No. of Tablets");
// Defines how to fill data for each cell.
// Get value from property of UserAccount. .
pharmCompCol.setCellValueFactory(new PropertyValueFactory<>("pharmComp"));
medicationCol.setCellValueFactory(new PropertyValueFactory<>("medication"));
strengthMgCol.setCellValueFactory(new PropertyValueFactory<>("strengthMg"));
barCodeCol.setCellValueFactory(new PropertyValueFactory<>("barCode"));
noTabletsCol.setCellValueFactory(new PropertyValueFactory<>("noTablets"));
// Display row data
ObservableList<Medication> list = populateTable("");
table.setItems(list);
table.getColumns().addAll(pharmCompCol, medicationCol, strengthMgCol, barCodeCol, noTabletsCol);
pane4.add(table, 0, 1);
}
public ObservableList<Medication> populateTable(String newRow) {
String filename = "userMed.txt";
String currentmed = "";
ObservableList<Medication> list = FXCollections.observableArrayList();
if(newRow != "") {
String[] newline = newRow.split(",");
Medication newUser = new Medication(newline[0],newline[1],newline[2],newline[3],newline[4]);
list.add(newUser);
}
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
String currentline;
while ((currentline = br.readLine()) != null) {
String[] arrayline = currentline.split(",");
Medication user = new Medication(arrayline[0],arrayline[1],arrayline[2],arrayline[3],arrayline[4]);
list.add(user);
}
}
catch (IOException e) {
e.printStackTrace();
}
return list;
}
}
[MCVE]を作成してください。今は完全ではない300行以上のコードがあります。あなたの質問に関係のないすべてのコードを取り除き、オープンソースイメージを使用して、他の人があなたのコードをコピーして実行することができます。イメージへのパスが正しいことはほとんどありません( 'src'フォルダは実行時にアクセスできない可能性が高いため)、例外がないことを再度確認する必要があります。 –