Java 更新JTextField中的文本
K、 因此,与我的上一个问题不同,我已经积极尝试处理这个问题很多次了,但它仍然不起作用 基本上,我正在尝试实现一个JTextField。我已经向它添加了动作侦听器,文本的getter和setter正在工作,但是我输入的文本没有显示在textfield中。我尝试将文本颜色设置为黑色,但没有效果。老实说,我不确定问题出在哪里 这是密码Java 更新JTextField中的文本,java,jtextfield,Java,Jtextfield,K、 因此,与我的上一个问题不同,我已经积极尝试处理这个问题很多次了,但它仍然不起作用 基本上,我正在尝试实现一个JTextField。我已经向它添加了动作侦听器,文本的getter和setter正在工作,但是我输入的文本没有显示在textfield中。我尝试将文本颜色设置为黑色,但没有效果。老实说,我不确定问题出在哪里 这是密码 import acm.program.*; import java.awt.event.*; import javax.swing.*; public class
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;
}
另外,我将尝试使用图像更好地解释我的问题。抱歉,如果这在您的操作系统上不起作用。他们的.tiff,但我稍后会尝试通过图像转换运行它们。出于某种原因,stackoverflow不允许我发布有问题的图片,所以我将尝试通过其他网站链接这些图片。很抱歉给您带来不便
当我运行代码时,会显示这一点。
基本上到目前为止,它的工作如预期
问题出现了
.
getter和setter正在工作,但是我希望在用户输入文本时更新JTextField,而不是不显示我在其中输入的任何内容。你想做什么
引述:
公共JTextField
构造一个新的文本字段。将创建默认模型,初始字符串为null,列数设置为0
重点补充
如果您使用的是默认构造函数,那么如果您没有调用setColumnsint,那么无论文本字段的宽度如何,JTextField的列限制都为0,因此将拒绝用户的所有输入。我推断您在程序运行时作为用户输入文本时遇到问题,而不是在程序中设置文本并使其显示时遇到问题
使用具有列限制的构造函数形式,或者使用setColumns在构造后指定非零最大值
如果这不能解决问题,请提供一个代码示例,尤其是在构建和初始化JTextField时。文本始终默认为黑色,因此不需要使用除setText之外的任何内容 在这里,你可能会问很多问题 要设置加载时的文本,只需在代码顶部使用setText
public TestFrame() {
initComponents();
jTextField1.setText("Hello I am text in a box");
}
您还可以让它以以下方式响应事件。例如,单击按钮
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//Text Box changes from default
jTextField1.setText("The button was pushed!!");
}
注意,这一切都是一样的,我觉得你把事情弄得比实际情况复杂了一点 使用普通文本字段而不是JTextfield。据认为,这就是问题所在。我不是专家,但我遇到了完全相同的问题,这似乎与ACM库的使用有关。我也是,因为您没有提供任何信息。SSCCE会很好。这里没有太多的内容。对于构造函数,我使用了这个,我认为这是在设置列,尽管我可能错了。nameField=新的JTextField25;我尝试了setColumns,但似乎对解决问题没有帮助。谢谢。这正是构造函数应该做的——将字段设置为25列。请在初始化JTextField的地方提供代码,然后我们可能会提供帮助;现在,没有足够的信息来解决这个问题。
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//Text Box changes from default
jTextField1.setText("The button was pushed!!");
}