Try   HackMD

Java FX 於 Apache NetBeans IDE 執行辦法

版本確認(比照B217)

Apache NetBeans IDE 22 (上方 Help -> about)

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

NetBeans 版本為 22
JDK 為 22.0.1

1. 創建專案

File -> New Project -> Java with Maven -> SimpleJavaFX Maven Archetype -> Next 

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

改名後-> Finish

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

創建完之後會有四個檔案

  1. Source Packages
  2. Dependencies
  3. Java Dependencies
  4. Project Files

其中我們的java範例檔案會放在Source Packages裡面

Source Packages裡面可以發現會有 com.mycompany.你的專案名子 (如果你在創建專案的地方有改名或是Group id的話可能會和我的不一樣)

com.mycompany.你的專案名子中會有兩個範例的java檔案

  1. App.java
  2. Systeminfo.java
    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

我們需要執行App.java檔案才可以執行Systeminfo.java

2. 示範課本程式碼

以下用一個課本的程式碼來做執行 P.564 (LISTING 14.1 MyJavaFX.java)

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

public class MyJavaFX extends Application {
  @Override // Override the start method in the Application class
  public void start(Stage primaryStage) {
    // Create a button and place it in the scene
    Button btOK = new Button("OK");
    Scene scene = new Scene(btOK, 200, 250);
    primaryStage.setTitle("MyJavaFX"); // Set the stage title
    primaryStage.setScene(scene); // Place the scene in the stage
    primaryStage.show(); // Display the stage
  }
  
  /**
   * The main method is only needed for the IDE with limited
   * JavaFX support. Not needed for running from the command line.
   */
  public static void main(String[] args) { 
    launch(args);
  }
}

<default package>裡新增java並將上述的程式碼貼上, 檔案名:MyJavaFX.java

此時如果你直接run MyJavaFX.java的話會發現沒辦法執行

這是因為我們需要創建一個單獨的 Main 類來運行我的 Main

所以我們只需要新增這段java檔案即可,我將檔案命名為run

import javafx.application.Application;

public class run {
    public static void main(String[] args) { 
        MyJavaFX.main(args);
    }
}

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

此時去執行run.java就可以發現MyJavaFX.java已經被執行了

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →


替代方案 vscode (考試時需用B217的NetBeans)

請先去下載javaFX Support

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

創建專案,並搜尋javaFX

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

此時的終端機跑一下之後會出現這個,請按enter
Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

請輸入Y然後按enter
Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

稍等一下後就可以看到demo的程式碼了
Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

此時我們直接在這裡創建上面的範例MyJavaFX.java
按照圖片的方法創建選擇class並輸入檔名MyJavaFX
Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

然後執行MyJavaFX(點擊右上角即可執行)
結果會如下,可以發現英文會跑掉,但如果用中文就不會
Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

這時只需要在程式碼新增一行,直接在按鈕上設置明確的字體即可
然後再重新執行

btOK.setFont(Font.font("Arial"));

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →