作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在学习 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);
}
});
现在,就个人而言,我会将其中大部分内容折叠到界面
中,以防止EditorPane
对MyCanvas
执行更多操作,但这是另一天的话题了;)
关于java - 如何在 main 中的可运行对象之外对 JFrame GUI 进行更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51295742/
我是一名优秀的程序员,十分优秀!