我目前正在学习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
来防止EditorPane
对MyCanvas
做更多的事情,但这是另一回事了;)