[英]Share objects in classes Java(FX)
好的,大家好,希望您過得愉快。
因此,我對Java
還是很Java
,但是我了解大多數基礎知識,因此至少不是一個精湛的菜鳥。 我正在開發JavaFX
示例應用程序,僅用於測試。 所以我遇到了一個問題,我有一個Main class
在其中創建了界面,非常簡單,只是一個帶有1個button
的scene
。
主要代碼:
public class Main extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Hi I'm a title");
//Initialize the Button Object
Button button = new Button();
button.setText("Bite me");
//Call the Handlers Class, with the name handlers
Handlers handlers = new Handlers();
/*
Here would go the code to pass the 'button' object to Handlers class
So then I can do whatever I need in there
*/
//The Button event, managed by the 'handle' method in Handlers class, that's why the 'handlers' in the
//parentheses
button.setOnAction(handlers);
//Just the scenery, not relevant
StackPane pane = new StackPane();
pane.getChildren().add(button);
Scene scene = new Scene(pane, 300,250);
primaryStage.setScene(scene);
primaryStage.show();
}
}
處理程序代碼:
public class Handlers implements EventHandler<ActionEvent> {
//Very sad try of a constructor, so as to pass the 'button' object from Main class
//In here, however I have no idea as to how to pass an Object type and how to receive it
public void Handlers(){
}
//The handle method to manage the 'button.setOnAction' event
//This is where I need the 'button' object to compare the source of the event
//to that specific button, so as to prevent that every single button does the same thing
//'button' object from Main class should go ".equals(button)"
@Override
public void handle(ActionEvent event) {
Main main = new Main();
if (event.getSource().equals()){
}
}
}
我向該按鈕添加了一個事件,所以我創建了一個Handlers class
來處理(dah) Main class
上的所有事件,當我嘗試將'button'對象從Main
傳遞到Main時,問題變得很丑陋Handlers
這樣我就可以獲取其來源,以便每個按鈕都不會做相同的事情。
我的問題是: 如何將“按鈕” object
從Main
傳遞給Handlers
? 我知道我可以使用Constructor
,唯一的問題是我仍然不太了解Constructor
函數的功能,也沒有正確使用參數。
我已經在這些論壇上閱讀了大約1個小時,以尋找解決方案,而且我很確定自己已經遇到過該解決方案,但是由於我的無知,無法理解它。 最清楚的例子是:
很抱歉發布了這么長的帖子,但是...我需要幫助:(
祝你有美好的一天:D
在這里有幾處錯誤,建議您在嘗試完全解決應用程序概念之前,了解一些有關的知識。 但我可以回答您的問題,並為您提供一些鏈接以幫助您開始使用。
介紹
這是有關main()
,通常您只有一個應用程序。
第一部分
研究類繼承。 在JavaFX應用程序中,您的主類通常會extend Application
而main()
方法通常會調用Application.launch()
,由於訪問控制 ,該方法可用於您的主類。 在幕后, launch()
調用Application.start()
。 因為您要繼承Application
所有內容,所以您需要實現abstract
方法start()
(可能是,但請不要在此引用我的意思start()
以完成您的所有UI工作,請參見覆蓋方法和相關內容。 超載 。 因此,在每個JavaFX應用程序都需要調用的start()
,然后創建一個可以分配給Stage
上多個Button
的按鈕Handler
。
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Hi I'm a title");
EventHandler eventHandler = new BiteHandler("You've been bitten");
Button biteMeButton = new Button();
biteMeButton.setText("Bite me");
Button biteMeAgainButton = new Button();
biteMeAgainButton.setText("Bite me again");
biteMeButton.setOnAction(eventHandler)
biteMeAgainButton.setOnAction(eventHandler);
StackPane pane = new StackPane();
pane.getChildren().add(biteMeButton);
pane.getChildren().add(biteMeAgainButton);
Scene scene = new Scene(pane, 300,250);
primaryStage.setScene(scene);
primaryStage.show();
}
}
第二部分
這是BiteHandler
類。 通過這種構造,檢查出多態性 。 您可能擁有一個BiteHandler
, ChewHandler
和LickHandler
,由於多態性,它們都可以存儲在List<EventHandler>
。
通過設置EventHandler
的方式,實質上是在調用EventHandler
調用handle()
方法,即單擊按鈕的on動作。 因此,在調用處理程序時,我們需要做一些事情。 由於我不知道如何為嘴巴做動畫,所以我只是將這個東西打印到傳遞給控制台的消息中。 在這里,我對構造函數是什么發表評論。
public class BiteHandler implements EventHandler<ActionEvent> {
private String message_;
/**
* Think of a constructor as any other method, however, it is special
* in that it is named after the class. Because it is named
* after the class, the compiler knows that the first thing
* that needs to be done when instantiating this class. A
* constructor is "automatic work" that "constructs" your object.
* In this class, when you instantiate it, a message is required.
* this message is stored in a "private class member" to be accessed
* later on when the bite() method is called
*/
public BiteHandler(String message) {
message_ = message;
}
@Override
public void handle(ActionEvent event) {
bite();
}
public void bite() {
System.out.println(message_);
}
}
結局
在您的應用程序上,您將有2個按鈕。 當您單擊任一按鈕時,短語“您已被咬”會打印到控制台。 這是因為每個按鈕都具有相同的BiteHandler
。 相當缺乏所有工作的光彩,但您需要做大量的功課才能完全理解這個答案
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.