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