Java 如何将消息从FXML控制器类发送到主应用程序类
各位好,飞越者 我有一个问题“我有一个fxml文件”CrawlerView.fxml“和它的控制器”CrawlerController.java”,还有一个主应用程序类,我想从控制器类向主应用程序类发送消息(数据库插入)(因为我不能在GUI/fxml控制器类中执行业务逻辑).所以我的问题是:是否可以将消息从fxml控制器类发送到主应用程序类“ 代码如下 有什么帮助吗 先发制人`Java 如何将消息从FXML控制器类发送到主应用程序类,java,fxml,ooad,Java,Fxml,Ooad,各位好,飞越者 我有一个问题“我有一个fxml文件”CrawlerView.fxml“和它的控制器”CrawlerController.java”,还有一个主应用程序类,我想从控制器类向主应用程序类发送消息(数据库插入)(因为我不能在GUI/fxml控制器类中执行业务逻辑).所以我的问题是:是否可以将消息从fxml控制器类发送到主应用程序类“ 代码如下 有什么帮助吗 先发制人` Crawler.java CrawlerView.fxml 履带式控制器 package app.mode
Crawler.java
CrawlerView.fxml
履带式控制器
package app.model.main;
导入java.net.URL;
导入java.sql.Statement;
导入java.util.ResourceBundle;
导入java.util.Stack;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.fxml.fxml;
导入javafx.fxml.Initializable;
导入javafx.scene.control.Button;
导入javafx.scene.control.TextField;
公共类CrawlerController实现可初始化
{
@FXML按钮搜索;
@FXML文本字段URL;
@Override//FXMLLoader在初始化完成时调用此方法
公共void初始化(URL fxmlFileLocation,ResourceBundle资源){
断言搜索!=null:“fx:id=\“myButton\”未被注入:检查FXML文件“simple.FXML”;
SEARCH.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件)
{
//如何向主应用程序类发送消息
}
});}
}
package app.model.main;
import java.io.IOException;
import java.sql.Statement;
import java.util.Stack;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Crawler extends Application {
private Stage MStage;
private BorderPane rootLayout;
@FXML Button SEARCH;
@FXML TextField URL;
static Conection conection;
static url_entries URLS;//self implemented classes
static word_count count;//self implemented classes
static Statement stmt;
//static public Stack DATABASE;
public static void test(){}
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage MStage)
{
try {
FXMLLoader RootLoader = new FXMLLoader();
FXMLLoader CrawlerLoader = new FXMLLoader();
RootLoader.setLocation(Crawler.class.getResource("view/RootView.fxml"));
CrawlerLoader.setLocation(Crawler.class.getResource("view/CrawlerView.fxml"));
rootLayout = (BorderPane) RootLoader.load();
AnchorPane CV = (AnchorPane) CrawlerLoader.load();
rootLayout.setCenter(CV);
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
MStage.setScene(scene);
MStage.show();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="628.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="app.model.main.CrawlerController">
<children>
<SplitPane dividerPositions="0.14424635332252836" layoutX="8.0" layoutY="-2.0" orientation="VERTICAL" prefHeight="628.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<Label layoutX="105.0" layoutY="14.0" prefHeight="52.0" prefWidth="388.0" text="Khurasani Web Crawler" textFill="#792323">
<font>
<Font name="Bauhaus 93" size="34.0" />
</font>
</Label>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<TextField id="URL" fx:id="URL" alignment="CENTER" blendMode="DARKEN" layoutX="110.0" layoutY="25.0" prefHeight="34.0" prefWidth="359.0" text="Enter URL">
<font>
<Font name="Berlin Sans FB Demi Bold" size="17.0" />
</font>
<effect>
<InnerShadow choke="0.56" color="#722424" height="26.93" radius="12.965" width="26.93" />
</effect>
</TextField>
<Button id="SEARCH" fx:id="SEARCH" layoutX="247.0" layoutY="82.0" mnemonicParsing="false" prefHeight="27.0" prefWidth="79.0" text="Search" textFill="#5e2929">
<font>
<Font name="Arial Black" size="15.0" />
</font>
</Button>
</children>
</AnchorPane>
</items>
</SplitPane>
package app.model.main;
import java.net.URL;
import java.sql.Statement;
import java.util.ResourceBundle;
import java.util.Stack;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
public class CrawlerController implements Initializable
{
@FXML Button SEARCH;
@FXML TextField URL;
@Override // This method is called by the FXMLLoader when initialization is complete
public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
assert SEARCH != null : "fx:id=\"myButton\" was not injected: check your FXML file 'simple.fxml'.";
SEARCH.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event)
{
//How to send message to main application class
}
});}
}