简体   繁体   English

使用IntelliJ GUI Designer在运行时将JComponent添加到JPanel

[英]Adding JComponent to JPanel at runtime with IntelliJ GUI Designer

I have initialized my own JPanel inside the GUI Designer's JPanel but I still can't seem to add this JTextField to my newly created JPanel when the button is clicked. 我已经在GUI设计器的JPanel中初始化了自己的JPanel,但是当单击按钮时,似乎仍然无法将此JTextField添加到新创建的JPanel中。 I am getting no errors, have tried revalidating, validating, repainting and more. 我没有收到任何错误,已经尝试过重新验证,验证,重新粉刷等等。 I even set the layout to my panel as a BoxLayout as suggested from another user but that still didn't work. 我什至根据其他用户的建议,将布局设置为BoxLayout设置为面板上的布局,但这仍然行不通。

fieldsPanel is created using the GUI Designer, but I try to override it. fieldsPanel是使用GUI设计器创建的,但是我尝试覆盖它。

panel is my own code that I want to add to fieldsPanel . panel是我自己的代码,我想添加到fieldsPanel

public class Form extends JFrame {

private JPanel rootPanel;
private JPanel fieldsPanel;
private JPanel panel;

public Form() {
    fieldsPanel = new JPanel();
    panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

    setContentPane(rootPanel);

    pack();

    addFieldButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JTextField skuField = new JTextField();
            panel.add(skuField);
            fieldsPanel.add(panel);
            pack();
            repaint();
        }
    });

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setVisible(true);
}

First a comment on GUI designers. 首先对GUI设计人员发表评论。 GUI designers are great for RAD and for someone who can code, but with little experience in GUI devlopment that needs to do a once-off GUI project. GUI设计人员非常适合RAD和可以进行编码的人员,但是对GUI开发的经验很少,需要完成一次GUI项目。 In all other cases and perhaps even in the last mentioned case, it's a better long-term strategy to learn how to do GUI development using only code and not a designer tool such as those found in NetBeans and IntelliJ. 在所有其他情况下,甚至在最后提到的情况下,学习仅使用代码而不使用诸如NetBeans和IntelliJ中的设计器工具的GUI开发方法都是一个更好的长期策略。 The main reason is that it hides things from the developer - so when something goes wrong you can't see where the problem lies and seeing is the first (and most vital) step for debugging. 主要原因是它对开发人员隐藏了一些内容-因此,当出现问题时,您看不到问题出在哪里,而眼见为调试的第一步(也是最关键的一步)。 That's why developers spend hours implementing log files and stepping through programs with debuggers. 这就是为什么开发人员花费数小时来实现日志文件并使用调试器逐步执行程序的原因。 Having said that, onto the issue: 话虽如此,关于这个问题:

IntelliJ uses XML to generate the Java code for you. IntelliJ使用XML为您生成Java代码。 The XML is built behind the scenes when you use the designer tool. 使用设计器工具时,XML是在幕后构建的。 When you run your program, there is a method call 当您运行程序时,会有一个方法调用

$$$setupUI$$$(MainView.java)

that creates the Java code (MainView extends JDialog in this case). 它创建Java代码(在这种情况下,MainView扩展了JDialog)。 If you want to manually initialise an item, the correct way to do it is to check the box in the designer tool which says Custom Create 如果要手动初始化项目,则正确的方法是选中设计器工具中显示“自定义创建”的框

在此处输入图片说明

When this box is checked, a method is created in your code called createUIComponents. 选中此框后,将在您的代码中创建一个名为createUIComponents的方法。 In this method you can then add your custom creation code, for example: 然后,可以使用此方法添加自定义创建代码,例如:

    private void createUIComponents() {
    // TODO: place custom component creation code here
     fieldsPanel = new JPanel();
     panel = new JPanel();
}

So what you must remember when working with designers is you have to play by their rules. 因此,与设计师合作时必须记住的是,您必须遵守他们的规则。 Use the provided functionality. 使用提供的功能。 One final note, that createUIComponents method gets called the moment this object comes into scope - no sooner and no later than immediately. 最后一点,当该对象进入作用域时,即会立即调用createUIComponents方法-不得早于立即。

If you follow this path then your example needs to change into this: 如果您遵循此路径,那么您的示例需要更改为:

    public class Form extends JFrame {

private JPanel rootPanel;
private JPanel fieldsPanel;
private JPanel panel;

public Form() {

    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    setContentPane(rootPanel);
    pack();

    addFieldButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JTextField skuField = new JTextField();
            panel.add(skuField);
            fieldsPanel.add(panel);
            pack();
            repaint();
        }
    });

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
    setVisible(true);
}

private void createUIComponents() {
        // TODO: place custom component creation code here
        fieldsPanel = new JPanel();
        panel = new JPanel();
    }

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

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