我目前正在学习Java GUI,并且正在制作一个Java程序,您可以在其中编辑带有GUI文本的形状。我创建了一个JFrame,其中包含一个自定义的MyCanvas对象和一个文本字段。这个想法是MyCanvas对象具有MouseListener并将包含形状,单击这些形状将启用GUI中的文本字段,以便用户可以输入要在形状中显示的新消息。但是,我正在GUI类的main方法内使用Runnable运行GUI,并且由于MyCanvas类之外的文本框不在Runnable之外,所以无法启用该文本​​框。谁能帮助我实现这一目标?

这是我的代码(伪代码)的基本结构:

// GUI class
public class GUI extends JFrame implements ActionListener {
    private static MyCanvas c = new MyCanvas(); // canvas
    private static TextField editor = new TextField(); // text field

    public static void init() {
        // initialize GUI elements and disable text box
    }

    public static void enableTextBox() {
        // enables the text field
    }

    public static void disableTextBox() {
        // disables the text field
    }

    public void actionPerformed(ActionEvent e) {
        // get message from text box
    }

    public static void main(String[] args) {
        // Run the GUI frame
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                init();
                /* Add a RectTextBox (a Rectangle object with text inside,
                 * class defined elsewhere)
                 */
                c.addShape(new RectTextBox("Hello World");
            }
        }
    }
}

// Canvas class
class MyCanvas extends JPanel implements MouseListener {
    // ArrayList of blocks on the canvas
    ArrayList<RectTextBox> blocks = new ArrayList<RectTextBox>();

    // Ctor
    public MyCanvas() {
        // Initialize canvas and add MouseListener to this canvas
    }

    public void paintComponent(Graphics g) {
        // Paints all blocks on canvas
    }

    public void addShape(RectTextBox b) {
        // Adds the text box b to the blocks ArrayList
    }

    public void mouseClicked(MouseEvent e) {
        // check if any blocks from the ArrayList is clicked

        /* enables the text field from GUI to enter messages, then set the
         * message entered to the block
         */
    }
}

最佳答案

因此,您的开端很好。 MyCanvas具有可以被其他类调用的功能,例如addShape ...

// Canvas class
class MyCanvas extends JPanel implements MouseListener {
    // ArrayList of blocks on the canvas

    ArrayList<RectTextBox> blocks = new ArrayList<RectTextBox>();

    // Ctor
    public MyCanvas() {
        // Initialize canvas and add MouseListener to this canvas
    }

    public void paintComponent(Graphics g) {
        // Paints all blocks on canvas
    }

    public void addShape(RectTextBox b) {
        // Adds the text box b to the blocks ArrayList
    }

    public void mouseClicked(MouseEvent e) {
        // check if any blocks from the ArrayList is clicked

        /* enables the text field from GUI to enter messages, then set the
     * message entered to the block
         */
    }
}


接下来,您需要将MyCanvas的引用传递给想要对其执行某些操作的类,作为“真正”的基本示例……

public class EditorPane extends JPanel {
    private MyCanvas canvas;

    public EditorPane(MyCanvas canvas) {
        this.canvas = canvas;
        // Build UI as required
    }
}


然后,当EditorPane要进行更改时,可以使用canvas调用所需的方法。

然后,我们构建您的UI,您将创建MyCanvas的实例,将该引用传递给EditorPane并将其添加到UI中,例如...

EventQueue.invokeLater(new Runnable() {
    @Override
    public void run() {
        MyCanvas canvas = new MyCanvas();
        EditorPane editor = new EditorPane(canvas);

        JFrame frame = new JFrame();
        frame.add(canvas);
        frame.add(editor, BorderLayout.SOUTH);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
});


现在,就我个人而言,我会将其折叠成interface来防止EditorPaneMyCanvas做更多的事情,但这是另一回事了;)

10-08 19:24