java 更新 JTextField 中的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6444098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Updating text in a JTextField
提问by Slayer0248
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.
K,所以与我的上一个问题不同,我已经多次主动尝试处理这个问题,但它仍然不起作用。
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.
基本上我正在尝试实现一个 JTextField。我已经向它添加了动作侦听器,并且文本的 getter 和 setter 正在工作,但是我输入的文本没有显示在文本字段中。我尝试将文本颜色设置为黑色,但没有帮助。老实说,我不确定问题是什么。
K here's the code.
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;
}
}
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.
此外,我将尝试使用图像更好地解释我的问题。抱歉,如果这不适用于您的操作系统。他们的 .tiffs 但我稍后会尝试通过图像转换来运行它们。出于某种原因,stackoverflow 不允许我发布有问题的图像,因此我将尝试通过其他网站创建指向它们的链接。带来不便敬请谅解。
When I run the code, this is displayed. See the image for that here.Basically so far it works as expected.
当我运行代码时,会显示出来。 请参阅此处的图像。基本上到目前为止它按预期工作。
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.
问题出现 在这里。getter 和 setter 正在工作,但我希望在用户输入文本时更新 JTextField,而不是不显示我在其中输入的任何内容。
回答by Jamie Curtis
回答by Adam Norberg
Quoting from the the Java 6 API on JTextField:
public JTextField()
Constructs a new
TextField
. A default model is created, the initial string isnull
, and the number of columns is set to 0.
public JTextField()
构造一个新的
TextField
. 创建了一个默认模型,初始字符串为null
,列数设置为 0。
(Emphasis added.)
(加了重点。)
If you are using a default constructor, then if you have not called setColumns(int)
, your JTextField has a column limit of 0 (regardless of the text field's width) and therefore will refuse all input from the user. I am inferring you are having trouble entering text as a user when the program is running, rather than trouble setting the text within the program and causing it to display?
如果您使用的是默认构造函数,那么如果您没有调用setColumns(int)
,则您的 JTextField 的列限制为 0(无论文本字段的宽度如何),因此将拒绝用户的所有输入。我推断您在程序运行时无法以用户身份输入文本,而不是在程序中设置文本并使其显示有问题?
Either use a form of the constructor that has a column limit, or use setColumns to specify a nonzero maximum after construction.
要么使用具有列限制的构造函数形式,要么在构造后使用 setColumns 指定非零最大值。
If this doesn't solve the issue, please provide a code sample, especially where you are constructing and initializing your JTextField.
如果这不能解决问题,请提供代码示例,尤其是在您构建和初始化 JTextField 的地方。
回答by sealz
The text is always defaulted to black so there no need to play with anything except setText.
文本始终默认为黑色,因此除了 setText 之外无需使用任何内容。
There are a number of things you could be asking here so.
你可以在这里问很多事情。
To set the text on load simply use setText at the top of your code.
要在加载时设置文本,只需使用代码顶部的 setText 即可。
public TestFrame() {
initComponents();
jTextField1.setText("Hello I am text in a box");
}
You can also have it respond to an event in the following way. Example is a button click.
您还可以通过以下方式让它响应事件。示例是按钮单击。
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
//Text Box changes from default
jTextField1.setText("The button was pushed!!");
}
Notice that it is all the same, I feel like you are making it a little more complicated than it actually is.
请注意,它都是一样的,我觉得你让它比实际复杂一些。