我刚刚开始使用JavaFX并且一直在尝试添加一个事件,当你按下“发送”按钮时,该事件会将文本添加到textarea并清除文本字段。但是,我似乎无法在handle方法中检查事件的来源。
我试图寻找一个解决方案,但其他人似乎没有遇到同样的问题 - 要么就是我错过了一些明显的问题。
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ApplicationMain extends Application implements EventHandler<ActionEvent>{
Stage window;
// Main Method
public static void main(String[] args) {
launch(args);
}
// Scene Method
@SuppressWarnings("static-access")
@Override
public void start(Stage primaryStage) {
// Window Stuff
window = primaryStage;
window.setTitle("Chat Application");
// Setup Grid Layout
GridPane grid = new GridPane();
grid.setAlignment(Pos.TOP_LEFT);
grid.setHgap(10);
grid.setStyle("-fx-background-color: #272828;");
// MenuBar
MenuBar menu = new MenuBar();
menu.setPrefWidth(1000);
menu.setPrefHeight(20);
// Creation of File + Help
Menu file = new Menu("File");
Menu help = new Menu("Help");
// Add the Menus to the MenuBar
menu.getMenus().add(file);
menu.getMenus().add(help);
// Add MenuBar to Scene
menu.setVisible(true);
grid.add(menu, 0, 0);
// Text Area Stuff
TextArea area = new TextArea();
area.setPrefWidth(1000);
area.setPrefHeight(700);
area.setEditable(false);
area.setStyle("-fx-control-inner-background: #313233;");
// Add Text Area to Grid
grid.add(area, 0, 1);
// Text Field
TextField enter = new TextField();
enter.setPromptText("Type here...");
enter.setMaxWidth(920);
enter.setMaxHeight(30);
enter.setStyle("-fx-padding: 5px;");
// Button
Button send = new Button("Send!");
// Set the Handler for the Send Button Event
send.setOnAction(this);
// Use of HBox to Space out Text Field & Send Button
HBox row = new HBox();
row.setSpacing(10);
row.setHgrow(enter, Priority.ALWAYS);
row.getChildren().addAll(enter, send);
// Use of VBox to Space out Text Field
VBox box = new VBox();
box.setSpacing(10);
box.setPadding(new Insets(10));
box.getChildren().add(row);
// Add HBox in VBox to Grid
grid.add(box, 0, 2);
// Scene Stuff
Scene scene = new Scene(grid, 1000, 750);
window.setScene(scene);
// Display the Window
window.show();
}
// Event Handler
@Override
public void handle(ActionEvent event) {
if (event.getSource() == send) {
}
}
}
每当我尝试检查源是否是“发送”按钮时,它都不会显示 - 就好像该方法无法访问它一样。我不确定如何解决这个问题。
这段代码有一些问题,但我们可以解决它没有问题。
首先学习命名约定并坚持下去,因为@kleopatra说如果你的谷歌java命名约定你将会超载许多结果阅读一些
接下来你不应该调用Stage
一个window
已经有另一个具有该名称的对象,所以它可能会让其他人感到困惑,但如果只是为了它,那我猜对了
如果你有一个错误修复它不会忽略它,我不会像你做的那样@SuppressWarnings("static-access")
send.setOnAction(this);
不是处理事件的方法删除你的implements EventHandler<ActionEvent>
你可以使用事件处理程序设置它像这样
send.setOnAction(event -> sendToTextArea(enter.getText(), area));
这就是您调用的方法应该是什么样子
private void sendToTextArea(String string, TextArea textArea){
//textArea.setText(string);Use setText if you want to set the whole area to something
textArea.appendText(string+"\n");//Use appendText to append add new line because chat app
}
其他一切看起来不错,这就是你的最终产品应该是什么样子
public class Main extends Application {
private Stage stage;
@Override
public void start(Stage primaryStage) {
// stage Stuff
stage = primaryStage;
stage.setTitle("Chat Application");
// Setup Grid Layout
GridPane grid = new GridPane();
grid.setAlignment(Pos.TOP_LEFT);
grid.setHgap(10);
grid.setStyle("-fx-background-color: #272828;");
// MenuBar
MenuBar menu = new MenuBar();
menu.setPrefWidth(1000);
menu.setPrefHeight(20);
// Creation of File + Help
Menu file = new Menu("File");
Menu help = new Menu("Help");
// Add the Menus to the MenuBar
menu.getMenus().add(file);
menu.getMenus().add(help);
// Add MenuBar to Scene
menu.setVisible(true);
grid.add(menu, 0, 0);
// Text Area Stuff
TextArea area = new TextArea();
area.setPrefWidth(1000);
area.setPrefHeight(700);
area.setEditable(false);
area.setStyle("-fx-control-inner-background: #313233;");
// Add Text Area to Grid
grid.add(area, 0, 1);
// Text Field
TextField enter = new TextField();
enter.setPromptText("Type here...");
enter.setMaxWidth(920);
enter.setMaxHeight(30);
enter.setStyle("-fx-padding: 5px;");
// Button
Button send = new Button("Send!");
// Set the Handler for the Send Button Event
send.setOnAction(event -> sendToTextArea(enter, area));
// Use of HBox to Space out Text Field & Send Button
HBox row = new HBox();
row.setSpacing(10);
row.setHgrow(enter, Priority.ALWAYS);
row.getChildren().addAll(enter, send);
// Use of VBox to Space out Text Field
VBox box = new VBox();
box.setSpacing(10);
box.setPadding(new Insets(10));
box.getChildren().add(row);
// Add HBox in VBox to Grid
grid.add(box, 0, 2);
// Scene Stuff
Scene scene = new Scene(grid, 1000, 750);
stage.setScene(scene);
// Display the stage
stage.show();
}
private void sendToTextArea(TextField textField, TextArea textArea){
//textArea.setText(string);Use setText if you want to set the whole area to something
//textArea.clear();and .clear to clear all text from the TextArea
textArea.appendText(textField.getText()+"\n");//Use appendText to append add new line because chat app
textField.clear();
}
public static void main(String[] args) { launch(args); }
}
制作你的TextArea
和TextField
类变量:
private TextArea area;
private TextField enter;
更改初始化:
// Text Area Stuff
area = new TextArea();
// Text Field
enter = new TextField();
和你的事件处理程序:
// Event Handler
@Override
public void handle(ActionEvent event) {
area.setText(enter.getText());
}
如果任何这些变化不明确,请不要犹豫。