1

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

在文本字段中输入名称以添加框。我的问题是我在 JTextField 中输入的文本虽然已记录(因为它显示在新框中)并没有显示在 JTextField 本身中,所以我在点击“添加”并阅读之前看不到我输入的内容在盒子本身上。

这是我的代码:

    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>();



}
4

2 回答 2

3

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

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

于 2012-11-17T17:06:35.160 回答
0

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

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

于 2016-07-28T21:11:41.053 回答