将JavaFX TextField添加到JPanel



我有一个JPanel cPanel和一个从JDK 8的javafx.scene.control.TextField导入的TextField resultField。现在我想cPanel.add(resultField,BorderLayout.NORTH),Eclipse给我一个错误:

The method add(String, Component) in the type Container is not applicable for the arguments (TextField, String)

如何将resultField正确添加到cPanel

但是JTextField不支持提示文本

您可以使用文本提示类。

我允许您指定当文本字段为空时显示的提示。一旦键入文本,就会删除提示。

提示实际上是一个JLabel,因此您可以自定义字体、forground等。:

JTextField tf7 = new JTextField(10);
TextPrompt tp7 = new TextPrompt("First Name", tf7);
tp7.setForeground( Color.RED );

您可以下载swingx-core-1.6.2.jar并使用它使swing Text Fields Prompt成为文本。

    PromptSupport.setPrompt("User ID", userNameField);
    PromptSupport.setFontStyle(Font.BOLD, userNameField);
    PromptSupport.setForeground(Color.BLACK, userNameField);
    PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.HIDE_PROMPT,
            userNameField);

从这里下载:http://www.java2s.com/Code/JarDownload/swingx/swingx-core-1.6.2.jar.zip

最新更新