2012-11-17 26 views
1

我目前正在爲初學者做CS106A斯坦福大學Java課程。我被困在第7號講義中,要求我創建一個簡單的程序,在畫布上繪製一個GLabel對象,然後讓我拖動它們,再次移除它們或清除整個畫布。爲了添加這樣一個框,我在SOUTH中添加了一個JTextField,以輸入框的名稱和ADD/REMOVE/CLEAR按鈕。爲什麼我的工作JTextField中輸入的文本不可見/可選?

在文本框中輸入名稱以添加一個框。我的問題是,我輸入到JTextField中的文本雖然被記錄(因爲它顯示在新框中),但它並沒有顯示在JTextField本身中,所以我沒有看到我輸入的內容,直到我點擊「ADD」,然後閱讀它在箱子本身。

這裏是我的代碼:

package handout07Interactors; 

import java.awt.Color; 
import java.awt.event.ActionEvent; 
import java.util.HashMap; 

import javax.swing.*; 

import acm.graphics.*; 
import acm.program.*; 

@SuppressWarnings("serial") 
public class Box_Diagram extends GraphicsProgram{ 

    public void init() { 
     displayButtons(); 
     addActionListeners(); 

     //TODO make draggable 

    } 

    public void actionPerformed(ActionEvent e) { 

     if (e.getActionCommand().equals("ADD")) { 

      //create box 
      GCompound canvasBox = createBox(tf.getText()); 

      //add box to HashMap 
      map.put(tf.getText(), canvasBox); 

      //add box to canvas 
      int x = (int) (getWidth() - canvasBox.getWidth())/2; 
      int y = (int) (getHeight() - canvasBox.getHeight())/2; 
      add(canvasBox, x, y); 

     } else if (e.getActionCommand().equals("REMOVE")) { 

      //remove box with name 
      if (map.get(tf.getText()) != null) {remove(map.get(tf.getText()));}; //if box exists, remove it 

     } else { 

      for(String name: map.keySet()) 
      { 
       remove(map.get(name)); 
      } 



     } 

    } 

    private GCompound createBox(String text) { 

     // GCompound 
     GCompound box = new GCompound(); 

     // create GRect 
     GRect rect = new GRect(BOX_WIDTH, BOX_HEIGHT); 
     box.add(rect); 

     // add GLabel 
     GLabel label = new GLabel(text); 
     int x = (int) (rect.getWidth() - label.getWidth())/2; 
     int y = 30; //manual entry, somehow calculation didn't work as it does for width 
     box.add(label, x, y); 

     map.put(text, box); 

     return box; 

    } 

    private void displayButtons() { 

     //label 
     add(new JLabel("Name:"), SOUTH); 

     //textfield 
     tf = new JTextField(30); 
     tf.addActionListener(this); 
     add(tf, SOUTH); 

     //ADD REMOVE CLEAR 
     add(new JButton("ADD"), SOUTH); 
     add(new JButton("REMOVE"), SOUTH); 
     add(new JButton("CLEAR"), SOUTH); 

    } 

    //IVARS 
    private JTextField tf; 
    public static final int BOX_WIDTH = 100; 
    public static final int BOX_HEIGHT = 50; 
    public HashMap<String, GCompound> map = new HashMap<String, GCompound>(); 



} 

回答

3

你在佈局中添加5個不同的組件(標籤,文本字段,以及3個按鈕)到同一個地方(你的邊框佈局的南段) 。

您應該將這5個組件添加到另一個JPanel中,例如使用FlowLayout,然後將此面板添加到主面板的SOUTH中。

+0

我看到如何改進佈局,你是對的。它實際上並不漂亮,但會影響JTextField的功能嗎?還是純粹的美容? – Jim

+1

我不知道,但我猜想有5個組件繪製在另一個之上會影響JTextField的功能,是的。 –

+0

我發現問題可以通過簡單地使用普通的TextField而不是JTextField來解決。可能是與ACM庫的使用有關的問題。這個代碼是爲CS106A分配的,在這個分配中,講師給我們發送了一個可能是舊版本的ACM庫。這一定是問題所在。感謝您的幫助,我很高興終於找到了解決方案! – Jim

0

對不起,我的英語 - 它不是我的第一語言。 當我嘗試使用JTextField時,我在NameSurfer(編程任務№6CS106A)中遇到了這種「奇怪的行爲」,但是在作業№7Section Assignments(作者指出的那個作業)中,所有內容都與JTextField一致。

我注意到,如果我在NORTH/WEST地區使用JTextField JTextField工作正常,對於SOUTH/EAST我使用TextField。

相關問題