2011-06-22 48 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庫的使用有關。