首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >JavaFX按钮事件处理程序编译错误

JavaFX按钮事件处理程序编译错误
EN

Stack Overflow用户
提问于 2017-09-10 12:57:41
回答 1查看 1K关注 0票数 0

我的一段代码

代码语言:javascript
复制
package scr;

import java.awt.event.ActionEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Control;
import javafx.scene.control.Dialogs;
import javafx.scene.control.ListView;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class Client extends Application implements Runnable{

    @FXML 
    private TextArea chatHistory; 
    private TextField txtMessage ;




    // define the socket and io streams
    Socket client;
    DataInputStream dis;
    DataOutputStream dos;


    @Override
    public void start(Stage stage) throws Exception {
        String input = Dialogs.showInputDialog(stage, "Please enter your name:", "Input Dialog", "title");
        BorderPane root = new BorderPane(); 
            VBox vb = new VBox();
            vb.setSpacing(10);

            TextArea chatHistory = new TextArea();
            vb.getChildren().add(chatHistory);


            TextField txtMessage=new TextField() ;
            vb.getChildren().add(txtMessage);

            Button btnSend = new Button();
            btnSend.setText("Send");
            vb.getChildren().add(btnSend);

            ListView<String> lvList = new ListView<String>();
         // lvList.setItems(items);
            lvList.setMaxHeight(Control.USE_PREF_SIZE);

            vb.setPadding(new Insets(10, 10, 10, 10));
            root.setTop(vb);
            root.setRight(lvList);
              // Set margin for top area.
            BorderPane.setMargin(vb, new Insets(10, 10, 10, 10));

下一条语句的错误是

代码语言:javascript
复制
Multiple markers at this line
    - Bound mismatch: The type ActionEvent is not a valid substitute for the bounded parameter <T    extends Event> of the type EventHandler<T>
    - The method setOnAction(EventHandler<ActionEvent>) in the type ButtonBase is not applicable     for the arguments (new EventHandler<ActionEvent>(){})
代码语言:javascript
复制
 //btnSend event
            btnSend.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent event){

                        try {
                            dos.writeInt(ServerConstants.CHAT_MESSAGE); // determine the type of message to be sent
                            dos.writeUTF(txtMessage.getText()); // message payload

                            dos.flush(); // force the message to be sent (sometimes data can be buffered)
                        }
                        catch (IOException e){
                            e.printStackTrace();
                        }

             }
            });
EN

回答 1

Stack Overflow用户

发布于 2017-09-10 14:12:35

因为您尝试将awt包的节点添加到javaFx组件“ActionEvent”中。将import java.awt.event.ActionEvent;更改为import javafx.event.ActionEvent

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46137435

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档