JavaFX在禁用的TextArea

时间:2017-06-13 10:13:00

标签: java css javafx

我尝试编程一个客户端 - 服务器 - 聊天,在我的客户端,我得到了一个TextArea用于所有消息。 TextArea已禁用,所以我想知道如何让它看起来正常"。

我找到了解决方案,如下所示:

.text-input > .scroll-pane:disabled 
{
    -fx-opacity: 1; 
}

但这不起作用,因为我不知道应该写什么,而不是.scroll-pane

这是我的fxml:



<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.Cursor?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onKeyPressed="#handelSendKey" prefHeight="400.0" prefWidth="600.0" stylesheets="@application.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainController">
   <cursor>
      <Cursor fx:constant="DEFAULT" />
   </cursor>
   <top>
      <MenuBar styleClass="menubar" stylesheets="@application.css" BorderPane.alignment="CENTER">
         <menus>
            <Menu mnemonicParsing="false" styleClass="menu" text="Connection">
              <items>
                <MenuItem id="joinRoomBtn" mnemonicParsing="false" onAction="#handelConnectButton" styleClass="connetionBtn" text="Join Chatroom" />
                <MenuItem id="changeName" mnemonicParsing="false" onAction="#changeName" styleClass="changeNameBtn" text="Change name" />
                 <MenuItem id="leaveRoomBtn" mnemonicParsing="false" text="Leave Chatroom" />
                 <MenuItem id="exitApplicationBtn" mnemonicParsing="false" styleClass="exitApplication" text="Exit Application" />
              </items>
            </Menu>
         </menus>
      </MenuBar>
   </top>
   <right>
      <ScrollPane id="scrollbarPane" hbarPolicy="NEVER" minHeight="-Infinity" minWidth="50" prefWidth="120.0" />
   </right>
   <center>
      <VBox minWidth="100" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
         <children>
            <TextArea fx:id="messageArea" diabled="true" minWidth="100" prefHeight="200.0" prefWidth="200.0" styleClass="messageArea" stylesheets="@application.css" VBox.vgrow="ALWAYS" />
            <HBox prefHeight="26.0" prefWidth="600.0">
               <children>
                  <TextField fx:id="sendTextField" prefHeight="25.0" prefWidth="436.0" styleClass="textField" stylesheets="@application.css" HBox.hgrow="ALWAYS" />
                  <Button fx:id="sendBtn" mnemonicParsing="false" onAction="#handleSendButton" styleClass="sendBtn" stylesheets="@application.css" text="↵" />
               </children>
               <opaqueInsets>
                  <Insets />
               </opaqueInsets>
            </HBox>
         </children>
      </VBox>
   </center>
</BorderPane>
&#13;
&#13;
&#13;

这是我目前的代码:

package application;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket; 
import java.util.Optional;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.TextInputDialog;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

public class MainController
{
Socket client;
BufferedReader re;
PrintWriter wr;

String name = "User";

@FXML
private TextField sendTextField;

@FXML
private TextArea messageArea;

@FXML
private Button sendBtn;

public void handleSendButton()
{
    sendMessageToServer();
}

public void handelSendKey(KeyEvent event)
{
    if (event.getCode() == KeyCode.ENTER)
    {
        sendMessageToServer();
    }
}

public void handelConnectButton()
{
    connectToServer();
    Thread t = new Thread(new MessagesFromServerListener());
    t.start();
}

public void changeName()
{
    setName(openNameDialog());
}

private void setName(String name)
{
    this.name = name;
}

private String openNameDialog()
{
    TextInputDialog dialog = new TextInputDialog();
    dialog.setTitle("Change your name");

    Optional<String> result = dialog.showAndWait();
    String entered = "none.";

    if (result.isPresent())
    {

        entered = result.get();
    }

    return entered;
}

private String openIPDialog()
{
    TextInputDialog dialog = new TextInputDialog();
    dialog.setTitle("Type in the Server IP");

    Optional<String> result = dialog.showAndWait();
    String entered = "none.";

    if (result.isPresent())
    {

        entered = result.get();
    }

    return entered;
}

public void sendMessageToServer()
{
    if (!(sendTextField.getText().isEmpty()) && sendTextField.getText() != null
            && sendTextField.getText().trim().length() > 0)
    {
        wr.println(name + ": " + sendTextField.getText());
        wr.flush();

        sendTextField.setText("");
        messageArea.requestFocus();
    }
}

public boolean connectToServer()
{
    try
    {
        client = new Socket(openIPDialog(), 33377);
        re = new BufferedReader(new InputStreamReader(client.getInputStream()));
        wr = new PrintWriter(client.getOutputStream());
        appendMessage("Network connection established.");
        return true;
    } catch (Exception e)
    {
        appendMessage("Could not establish network connection.");
        return false;
    }
}

private void appendMessage(String string)
{
    messageArea.appendText(string + "\n");
}

public class MessagesFromServerListener implements Runnable
{
    @Override
    public void run()
    {
        String message;

        try
        {
            while ((message = re.readLine()) != null)
            {
                appendMessage(message);
            }
        } catch (IOException e)
        {
            appendMessage("Nachricht konnte nicht empfangen werden!");
            e.printStackTrace();
        }
    }
}
}

0 个答案:

没有答案