K,所以与我上一个问题不同,我一直积极尝试多次处理此问题,但仍然无法正常工作。更新JTextField中的文本
基本上我试图实现一个JTextField。我已经添加了动作侦听器,并且文本的获取者和设置者正在工作,但我输入的文本未显示在文本字段中。我试图将文字颜色设置为黑色,但没有帮助。老实说,我不确定问题是什么。
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不会让我发布有问题的图像,所以我会试着通过其他网站尝试与他们建立链接。抱歉给你带来不便。
当我运行代码时,会显示此代码。 See the image for that here. 基本上到目前为止,它按预期工作。
问题出现 here。 获取者和设置者正在工作,但我希望在用户输入文本时更新JTextField,而不是显示我已输入的任何内容。
我也是,因为你没有提供任何信息。 –
SSCCE会很好。这里没有什么可说的。 – jzd