简体   繁体   English

如何使JTextField文本可见?

[英]How to make JTextField text visible?

My goal is to write something that is visible to the user in the JTextField and to display this text in the console. 我的目标是在JTextField中编写对用户可见的内容,并在控制台中显示此文本。 As of now, the JTextField is accepting text but nothing shows. 到目前为止,JTextField正在接受文本,但未显示任何内容。 No cursor, no text. 没有光标,没有文本。

I've tried using textfield.setEditable(true), textfield.setEnable(true) and different fore and background colors but nothing happens. 我尝试使用textfield.setEditable(true),textfield.setEnable(true)和不同的前景色和背景色,但是什么也没发生。

Curiously I can use textField.setText("Random text") and it shows, but I cant delete it or edit this when the program is running and it's not included in the outputs from getText(). 奇怪的是,我可以使用textField.setText(“ Random text”)并显示它,但是当程序运行并且不能包含在getText()的输出中时,我无法删除它或对其进行编辑。

This is the program : 这是程序:

import acm.program.*;

import java.awt.Color;
import java.awt.event.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class TextFieldTest extends ConsoleProgram implements SomeConstants {

public void init() {
    setSize(APPLICATION_WIDTH, APPLICATION_HEIGHT);

    textField = new JTextField(20);
    add(textField, SOUTH);
    textField.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == textField)
        println("Hi, " + textField.getText());
}

private JTextField textField;
}

If you're working with acm.program.ConsoleProgram , you may need to use Java SE 5 (version 1.5), as suggested here . 如果你正在使用acm.program.ConsoleProgram ,您可能需要使用Java SE 5(1.5版本),作为建议在这里

Addendum: As @Tor comments, java.awt.TextField may be acceptable under later versions of the JRE. 附录:作为@Tor注释,在更高版本的JRE中, java.awt.TextField是可以接受的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM