简体   繁体   中英

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. As of now, the JTextField is accepting text but nothing shows. No cursor, no text.

I've tried using textfield.setEditable(true), textfield.setEnable(true) and different fore and background colors but nothing happens.

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().

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 .

Addendum: As @Tor comments, java.awt.TextField may be acceptable under later versions of the JRE.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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