gpt4 book ai didi

java - Swing 到 Javafx 转换

转载 作者:行者123 更新时间:2023-12-02 03:02:49 38 4
gpt4 key购买 nike

我不确定这是否是一个愚蠢的问题,但我需要有关我正在创建的程序的帮助。我想为这个即时通讯应用程序制作一个登录屏幕,我已经创建了该应用程序,但我是在 javaFX 中制作的。问题是,由于这个即时通讯应用程序很旧,所以我用 swing 创建了它。由于 swing 中没有场景,而 javafx 中有场景,因此当我登录时,我通常会更改场景,仅此而已。这就是为什么我将此程序从 swing 更改为 javafx 但我遇到了一些错误,如果有人好心告诉我我做错了什么,我将不胜感激。感谢并抱歉这么多余,记住我只是九年级的初学者。

确切的错误在第56行

The method ActionListener(new ActionListener(){}) is undefined for the type TextField 

package messengerClient;
import java.io.*;
import java.net.*;
import java.awt.event.*;
import javax.swing.*;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.*;
import javafx.stage.*;
import javafx.stage.Stage;
import javax.swing.JFrame;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import java.awt.*;
import java.awt.event.ActionEvent;

public class ClientFX extends Application {

private TextField userText;
private TextArea chatWindow;
private ObjectOutputStream output;
private ObjectInputStream input;
private String message = "";
private String serverIP;
private Socket connection;

Stage window;
Scene scene1,scene2;

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

@Override
public void start(Stage primaryStage) throws Exception {
window = primaryStage;
window.setTitle("Client - Instant Messenger!");
ClientFX juan = new ClientFX("127.0.0.1");
juan.startRunning();
}
//CONSTRUCTOR
public ClientFX(String host){
serverIP = host;
ScrollPane sp = new ScrollPane();
sp.setContent(chatWindow);
userText = new TextField();
userText.setEditable(false);
userText.ActionListener(
new ActionListener(){
@Override
public void actionPerformed(ActionEvent event){
sendMessage(event.getActionCommand());
userText.setText("");
}
}
);
BorderPane layout1 = new BorderPane();
layout1.setTop(userText);
layout1.setCenter(chatWindow);
chatWindow = new TextArea();
scene2 = new Scene(layout1,500,300);
window.setScene(scene2);
window.show();
}

//CONNECT TO SERVER
public void startRunning(){
try{
connectToServer();
setupStreams();
whileChatting();
}catch(EOFException eofException){
showMessage("\n Client terminated connection");
}catch(IOException ioException){
ioException.printStackTrace();
}finally{
closeCrap();
}
}

//CONNECT TO SERVER
private void connectToServer() throws IOException{
showMessage("Attempting connection... \n");
connection = new Socket(InetAddress.getByName(serverIP), 6789);
showMessage("You have succesfully connected to: " + connection.getInetAddress().getHostName());
}

//SET UP STREAMS TO SEND AND RECIVE MESSAGES
private void setupStreams() throws IOException{
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
showMessage("\nYour streams are now ready to go! \n");
}

//WHILE CHATTING WITH THE SERVER
private void whileChatting() throws IOException{
ableToType(true);
do{
try{
message = (String) input.readObject();
showMessage("\n" + message);
}catch(ClassNotFoundException classnotfoundException){
showMessage("\nI do not know that object type");
}
}while(!message.equals("Server - END"));
}

//CLOSE THE STREAMS AND THE SOCKETS
private void closeCrap(){
showMessage("\nClosing streams and the sockets down...");
ableToType(false);
try{
output.close();
input.close();
connection.close();
}catch(IOException ioException){
ioException.printStackTrace();
}
}

//SEND MESSAGES TO THE SERVER
private void sendMessage(String message){
try{
output.writeObject("CLIENT - " + message);
output.flush();
showMessage("\nCLIENT - " + message);
}catch(IOException ioException){
chatWindow.appendText("\nThat data type cannot be sent, sorry!");
}
}

//UPDATE CHAT WINDOW
private void showMessage(final String m){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
chatWindow.appendText(m);
}
}
);
}

//GIVES USER PERMISION TO TYPE INTO THE TEXT BOX
private void ableToType(final boolean tof){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
userText.setEditable(tof);
}
}
);
}
}

最佳答案

问题是你正在混合 swing 和 javafx。您正在导入两者:

import java.awt.event.*;
import javax.swing.*;
import javafx.application.Application;
...
import javafx.scene.Scene;
import javafx.scene.control.Button;

例如,在 ClientFX 构造函数中,您尝试将 javax.swing.JTextField (swing 组件)添加到 javafx.scene.BorderPane (javafx 组件)。 JTextField 不扩展 javafx.scene.Node,因此不能将其提供给 BorderPane.setTop(Node)。

基本上,对于任何 swing 组件(通常以 J、JTextField、JTextArea 等开头),找到 javafx 等效组件(例如 javafx.scene.control.TextField、javafx.scene.control.TextArea)并学习如何使用它们(他们将有不同的 api)。最后,您不应该有 java.awt 或 javax.swing 的 import 语句。

更新

正如已经说过的,您需要学习如何使用新的 fx api。不仅包名称、类和方法发生了变化,而且编程概念也可能发生变化。要移植 JTextField 行为,您可以使用 TextField 并使用 EventHandler 实现调用 setOnAction 方法(与 swing 概念非常相似):

userText.setOnAction(new EventHandler<javafx.event.ActionEvent>()
{
@Override
public void handle(javafx.event.ActionEvent event)
{
//
}
});

关于java - Swing 到 Javafx 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42195503/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com