更新JTextField中的文本 [英] Updating text in a JTextField

查看:79
本文介绍了更新JTextField中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

K,所以与上一个问题不同,我一直很主动地尝试处理此问题多次,但仍无法正常工作.

K, so unlike with my last question, I've been proactive about trying to deal with this problem a number of times, and it's still not working.

基本上,我正在尝试实现JTextField.我已经添加了动作侦听器,并且文本的getter和setter可以正常工作,但是我输入的文本未显示在文本字段中.我尝试将文本颜色设置为黑色,但没有帮助.老实说,我不确定是什么问题.

Basically I'm trying implement a JTextField. I've added the action listener to it and the getters and setters for the text are working, but text that I'm enter isn't showing up in the textfield. I tried setting the text color to black and that didn't help. Honestly, I'm not sure what the issue is.

K这是代码.

import acm.program.*;
import java.awt.event.*;
import javax.swing.*;

public class NameSurfer extends Program implements NameSurferConstants {
//Change back to program after this
/* Method: init() */
/**
 * This method has the responsibility for reading in the data base
 * and initializing the interactors at the bottom of the window.
 */
public void init() {
    // You fill this in, along with any helper methods //
    createUI();
    addActionListeners();
}

/* Method: actionPerformed(e) */
/**
 * This class is responsible for detecting when the buttons are
 * clicked, so you will have to define a method to respond to
 * button actions.
 */
public void actionPerformed(ActionEvent e) {
    // You fill this in //
    if(e.getSource() == nameField || e.getSource() == graphName) {
        drawNameGraph(nameField.getText());
    } else if(e.getSource() == clearGraph) {
        clearNameGraph();
    }
}

 /* Method: createUI() */
 /**
  * This method sets up and adds the interactors at the bottom of the window*/
private void createUI() {
    nameField = new JTextField(25); 
    nameField.setColumns(25);
    nameField.addActionListener(this);
    graphName = new JButton("Graph");
    clearGraph = new JButton("Clear");
    graph=new NameSurferGraph();
    add(new JLabel("Name"), SOUTH);
    add(nameField, SOUTH);
    add(graphName, SOUTH);
    add(clearGraph, SOUTH);
    add(graph);
    //println(db.fileEntries.size());
}

/* Method: drawNameGraph(str) */
/** Draws the graph of the name entered in nameField
 * */
private void drawNameGraph(String str) {
    //println(str);


    NameSurferEntry entered = db.findEntry(str);
    if(entered != null) {
        //println("Graph: " + entered.toString());
        graph.addEntry(entered);
        nameField.setText("str");

    } else {
        graph.badEntry(str);
    }
    //nameField.setText("");
}

/* Method: clearNameGraph() */
private void clearNameGraph() {
    graph.clear();
}

private NameSurferDataBase db = new NameSurferDataBase(NAMES_DATA_FILE);

/**TextField where the names get entered*/
private JTextField nameField;

/**button to graph name popularity*/
private JButton graphName;

/**Clears graph*/
private JButton clearGraph;

private NameSurferGraph graph;

}

我还将尝试使用图像更好地解释我的问题.很抱歉,如果这不适用于您的操作系统.他们的.tiffs,但是我稍后将尝试通过图像转换来运行它们.由于某些原因,stackoverflow不允许我发布有问题的图像,因此我将尝试通过一些其他网站来建立指向它们的链接.不便之处,敬请谅解.

Also I'm going to try to explain my question better using images. Sorry if this don't work on your OS. Their .tiffs but I'll try to run them through image conversion later on. For some reason, stackoverflow isn't letting me post the images in question, so I'm going to try to do some links to them instead through some other site. Sorry for the inconvenience.

运行代码时,将显示该代码. 在此处查看图片. 到目前为止,基本上可以按预期进行.

When I run the code, this is displayed. See the image for that here. Basically so far it works as expected.

出现问题 此处. 该getter和setter可以正常工作,但是我希望在用户输入文本时更新JTextField,而不是不显示我输入的任何内容.

The problem arises here. The getters and setters are working, but I'ld like to have the JTextField updated when the user enters the text, as opposed to not displaying anything that I've got entered in it.

推荐答案

您是否要尝试

这篇关于更新JTextField中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆