2017-02-25 53 views
-2

我想在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; 

} 

}

+2

请创建一个[MCVE]。现在你有300多行代码甚至不完整。摆脱所有与您的问题无关的代码并使用开源图像,以便其他人可以复制您的代码并执行它。请注意,图像路径是不太正确的(src'文件夹不可能在运行时访问),因此您应该仔细检查是否有例外。 –

回答

0

当您访问封装内的资源,你不包括src但有开始使用/例如不同的文件夹结构:

Eclipse Project 
    |__ src/ 
     |___ com/ 
     |  |___ image1.png 
     |  |___ stackoverflow/ 
     |    |___ image2.png 
     |    |___ MainGUI.java 
     |___ image3.png 

在JavaFX应用程序,你会喜欢访问下列资源:

Image image1 = new Image(MainGUI.class.getResourceAsStream("/com/image1.png")); 
Image image2 = new Image(MainGUI.class.getResourceAsStream("/com/stackoverflow/image2.png")); 
Image image3 = new Image(MainGUI.class.getResourceAsStream("/image3.png")); 

// So in your case, you would change your line to this: 
img1 = new Image(MainGUI.class.getResourceAsStream("/add_medication_button.png")); 
+0

注意,虽然OP表明没有例外。如果路径错了,会有一个例外...... –

+0

他在代码中提到'//这终止了我的代码,我认为这意味着异常。 –

+1

“它不显示错误但终止”,但我认为它可能会...... –