为什么我工作的JTextField中输入的文本不可见/可选?

时间:2012-11-17 16:58:59

标签: java swing jtextfield

我目前正在为初学者学习CS106A Stanford Java课程。我被困在第7号讲义中,要求我创建一个简单的程序,在画布上绘制带有GLabel的GRects,然后允许我拖动它们,再次删除它们或清除整个画布。为了添加这样的框,我在SOUTH中添加了一个JTextField来输入框的名称,以及ADD / REMOVE / CLEAR按钮。

在文本字段中输入名称以添加框工作。我的问题是我在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>();



}

2 个答案:

答案 0 :(得分:3)

您将5个不同的组件(标签,文本字段和3个按钮)添加到布局中的相同位置(边框布局的南部)。

您应该使用FlowLayout将这5个组件添加到另一个JPanel,然后将此面板添加到主面板的SOUTH。

答案 1 :(得分:0)

对不起我的英语 - 这不是我的第一语言。 当我尝试使用JTextField时,我在NameSurfer(编程作业№6CS106A)中遇到了这种“奇怪的行为”,但是在作业№7部分作业中(与作者指出的相同),一切都与JTextField一致。

我注意到如果我在NORTH / WEST区域使用JTextField,JTextField工作正常,对于SOUTH / EAST,我使用TextField。

相关问题