简体   繁体   English

访问IntelliJ上GUI设计器中创建的组件

[英]Accessing components created in GUI designer on IntelliJ

Although I've used Swing before I've never used a GUI designer and I'm having trouble accessing components I've dropped onto my panel from within my source code. 虽然在我从未使用过GUI设计器之前我已经使用过Swing,但是我在访问组件时遇到了麻烦,我已经从源代码中删除了我的面板。

I created a new project and chose to create a GUI form. 我创建了一个新项目,并选择创建一个GUI表单。 I then created the main method using the 'generate' option and now I have this code in my 'helloWorld.java' file. 然后我使用'generate'选项创建了main方法,现在我在'helloWorld.java'文件中有了这个代码。

public class helloWorld {


private JPanel myForm;
private JLabel text;

public static void main(String[] args) {
    JFrame frame = new JFrame("helloWorld");
    frame.setContentPane(new helloWorld().myForm);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setPreferredSize(new Dimension(800, 600));
    frame.pack();
    frame.setVisible(true);
    }
}

I then added a JLabel in the designer with the field name title which added an attribute to the head of my helloWorld class. 然后我在设计器中添加了一个带有字段名称title的JLabel,它为我的helloWorld类的头部添加了一个属性。 I now want to set the text on the field name after the program has run. 我现在想在程序运行后在字段名称上设置文本。

If I create the JLabel instance with a new string as an argument and add it to my JFrame then the program crashes with a null pointer exception. 如果我使用新字符串作为参数创建JLabel实例并将其添加到我的JFrame,则程序会因空指针异常而崩溃。

If I create a JLabel with no arguments and call setText on it and then repaint on the JFrame, nothing happens. 如果我创建一个没有参数的JLabel并在其上调用setText然后在JFrame上重新绘制,则没有任何反应。

I guess to some up my problem in a single line: How do you access components that I have created using the GUI designer? 我想在一行中解决我的问题:如何访问我使用GUI设计器创建的组件?

First, IntelliJ is a bit special in that it hides a lot of the boilerplate code for you, so your source code looks actually simpler than what is really going on under the hood. 首先,IntelliJ有点特别之处在于它为您隐藏了很多样板代码,因此您的源代码看起来实际上比实际内容更简单。

Basically, when you use the IntelliJ GUI builder, you end up with a source code corresponding to your form that looks like this: 基本上,当您使用IntelliJ GUI构建器时,最终会得到与您的表单相对应的源代码,如下所示:

public class DialogEditView {

    private JPanel mainPanel;
    private JLabel labelDescription;
    private JLabel labelExample;
    private JComboBox comboboxDEJC;

}

To be able to access these, you can simply add getters to that source file: 为了能够访问这些,您只需将getter添加到该源文件:

public class DialogEditView {

    private JPanel mainPanel;
    private JLabel labelDescription;
    private JLabel labelExample;
    private JComboBox comboboxDEJC;

    public JPanel getMainPanel() {
        return mainPanel;
    }

    // etc.

}

Once again, IntelliJ either modifies the source code or modifies the class files automagically for you (you can go in the Settings / GUI Builder to test the two options and see what they do). 再一次,IntelliJ可以修改源代码或自动修改类文件(您可以进入Settings / GUI Builder来测试这两个选项并查看它们的作用)。

How do you access components that I have created using the GUI designer? 如何访问我使用GUI设计器创建的组件?

You can go to the source code file corresponding to your GUI and add getters. 您可以转到与GUI对应的源代码文件并添加getter。 Make sure to name your components... 确保为您的组件命名......

The automatically generated initialization code in your binding class looks like: 绑定类中自动生成的初始化代码如下所示:

 private void $$$setupUI$$$() {}

For more informations about IntelliJ's Initialization Code see this Jetbrains documentation: Creating Form Initialization Code 有关IntelliJ初始化代码的更多信息,请参阅此Jetbrains文档: 创建表单初始化代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM