2014-01-19 50 views
2

我正在開發一個項目。我的項目在一個包中包含幾個類。我的主課將Application擴展到它。我正在使用NetBeans IDE來開發我的項目。現在我需要編寫其他類而不將應用程序擴展到它。我該怎麼做?我在下面包括我的一個類的代碼。請建議如何編輯它。在不擴展的情況下編寫javafx程序應用程序

package welcomepage; 

import javafx.application.Application; 
import javafx.geometry.Insets; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.VBox; 
import javafx.scene.paint.Color; 
import javafx.scene.text.Font; 
import javafx.scene.text.FontWeight; 
import javafx.stage.Stage; 

public class About extends Application { 

    @Override 
    public void start(Stage stage) { 

     BorderPane border = new BorderPane(); 

     HBox hbox = addHBox(); 
     border.setTop(hbox); 
     border.setCenter(addVBox()); 
     border.setBottom(addHBox1()); 

     Scene scene = new Scene(border,700,450); 
     stage.setScene(scene); 
     stage.setResizable(false); 

    } 

    private HBox addHBox() { 

     HBox hbox = new HBox(); 
     hbox.setPadding(new Insets(15, 12, 15, 320)); 
     hbox.setSpacing(10); 
     hbox.setStyle("-fx-background-color: #336699;"); 

     Label lb1=new Label("ABOUT"); 
     lb1.setAlignment(Pos.CENTER); 
     lb1.setFont(Font.font("Trebuchet MS",FontWeight.BOLD,20)); 

     hbox.getChildren().addAll(lb1); 

     return hbox; 
    } 

    private VBox addVBox() { 

     VBox vbox = new VBox(); 
     vbox.setPadding(new Insets(20)); 
     vbox.setSpacing(5); 

     Label l1=new Label("C - MARK AND ATTENDANCE CALCULATOR"); 
     l1.setFont(Font.font("Calibri",FontWeight.BOLD,20)); 
     l1.setTextFill(Color.GREEN); 

     Label l2=new Label("\nSoftware to calculate C-mark and attendance easily.\n" 
       + "Supported in Windows XP or above.\n" 
       + "Developed using Java.\n" 
       + "Advantages : Simple user interface, Easy usage.\n\n" 
       + "Developed by :\n" 
       + "\t\t Adarsh P.S \n" 
       + "\t\t Akhilnath A.R \n" 
       + "\t\t Arjun P Das \n" 
       + "\t\t Tomin Jacob "); 
     l2.setFont(Font.font("Calibri",FontWeight.BOLD,18)); 
     l2.setTextFill(Color.GREEN); 

     vbox.getChildren().addAll(l1,l2); 
     return vbox; 
    } 

    private HBox addHBox1() 
    { 
     HBox hbox1 = new HBox(); 
     hbox1.setPadding(new Insets(15, 12, 15, 300)); 

     Button btn1=new Button("BACK"); 
     btn1.setFont(Font.font("Calibri",FontWeight.BOLD,18)); 
     btn1.setPrefSize(100,40); 

     hbox1.getChildren().addAll(btn1); 
     return hbox1; 
    } 

    public static void main(String[] args) { 
     launch(args); 
    }   
} 
+1

您需要擴展Application。你想要達到什麼目的? – Reimeus

+0

@Reimeus當我在Stackoverflow中問及時,我曾說過只將Application擴展到主類。他們還表示不會將應用程序擴展到其他類。這就是我問這個問題的原因。 –

+1

是的,這是有道理的,但其他classes_需要什麼? – Reimeus

回答

1

在項目窗口中,右鍵單擊想要新Java類的包。包裹看起來像一個小黃色的盒子,通常都是小寫的。選擇新的然後Java類。它會要求一個名字,像類名一樣使用UpperCase。新的選項卡將隨您的新課程一起打開。它不會擴展應用程序,並將使用默認模板。

要調用此類(稱爲NewClass),請在主類中執行此操作。

//import newpackage.NewClass //only if it's not in same package. 

//somwhere in code you need to create the new class 
NewClass newClass = new NewClass(); 

//now a button can call methods in newClass 
final Button button = new Button("Butt"); 
button.setOnAction(new EventHandler<ActionEvent>() { 
      @Override 
      public void handle(ActionEvent e) { 
       newClass.newMethod(); 
      } 
     }); 
+0

謝謝。這很有幫助。 –

+0

是否可以通過單擊按鈕來調用新創建的類中的函數? –

+0

是的,我會編輯答案 – brian

相關問題