2011-06-22 46 views
-1

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,而不是显示我已输入的任何内容。

+0

我也是,因为你没有提供任何信息。 –

+0

SSCCE会很好。这里没有什么可说的。 – jzd

回答

2

你想要做this?!?

JTextField text = new JTextField(); 

text.setText("ttttttttttttexxxt"); 
1

the Java 6 API on JTextField报价:

public JTextField()

构造一个新的TextField。创建一个默认的模型,初始字符串null列数设置为0

(着重号。)

如果您使用的是默认的构造函数,然后如果你没有调用setColumns(int),那么JTextField的列限制为0(不管文本字段的宽度如何),因此将拒绝来自用户的所有输入。我推断你在程序运行时以用户身份输入文本时遇到麻烦,而不是在程序中设置文本并导致它显示时出现问题?

要么使用具有列限制的构造函数的形式,要么使用setColumns在构造后指定非零最大值。

如果这不能解决问题,请提供代码示例,特别是在构建和初始化JTextField的位置。

+0

对于构造函数,我使用这个,我认为是设置列,但我可能是错的。 nameField = new JTextField(25);我尝试了setColumns,它似乎没有帮助解决这个问题。不过谢谢。 – Slayer0248

+0

这正是构造函数应该做的 - 将字段设置为25列。请提供您的代码,您正在初始化JTextField的位置,然后我们可以提供帮助;现在,没有足够的信息来解决问题。 –

0

该文本总是默认为黑色,所以没有必要玩除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!!");   
} 

请注意,它们都是一样的,我觉得你让它比实际上更复杂一点。

0

使用正常的TextField而不是JTextfield。根据this post这是问题。我不是专家,但我遇到了完全相同的问题,并且似乎与ACM库的使用有关。