gpt4 book ai didi

java - 如何在 main 中的可运行对象之外对 JFrame GUI 进行更改?

转载 作者:行者123 更新时间:2023-12-02 11:01:36 24 4
gpt4 key购买 nike

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

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

// 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);
}
});

现在,就个人而言,我会将其中大部分内容折叠到界面中,以防止EditorPaneMyCanvas执行更多操作,但这是另一天的话题了;)

关于java - 如何在 main 中的可运行对象之外对 JFrame GUI 进行更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51295742/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com