How to create GUI forms in IDEA correctly?

I'm trying to create a form in IDEA using the default plugin. IDE version 11.0.1.

I do this:

  1. Creating a project.
  2. I create a form in it.
  3. In the settings, I specify Generate GUI into Java source code.
  4. Then I add a form, add buttons, etc.
  5. I start the project - some lines are generated.

Here is the code:

import javax.swing.*;
import java.awt.*;

/**
 * Created by IntelliJ IDEA.
 * User: Jakeroid
 * Date: 30.01.12
 * Time: 22:52
 * To change this template use File | Settings | File Templates.
 */
public class MainForm {
    private JButton button1;
    private JTextField textField1;
    private JTextArea textArea1;

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new com.intellij.uiDesigner.core.GridLayoutManager(2, 3, new Insets(0, 0, 0, 0), -1, -1));
        button1 = new JButton();
        button1.setText("Button");
        panel1.add(button1, new com.intellij.uiDesigner.core.GridConstraints(1, 2, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_HORIZONTAL, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        textField1 = new JTextField();
        panel1.add(textField1, new com.intellij.uiDesigner.core.GridConstraints(1, 0, 1, 2, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_HORIZONTAL, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        textArea1 = new JTextArea();
        panel1.add(textArea1, new com.intellij.uiDesigner.core.GridConstraints(0, 0, 1, 3, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_BOTH, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, null, new Dimension(150, 50), null, 0, false));
    }
}

The application does not start, error:

Cannot resolve method.

The lines generated by IDEA are highlighted.

Author: angry, 2012-01-30

2 answers

Try it in general like this:

public class MainForm extends JFrame{
    private JButton button1;
    private JTextField textField1;
    private JTextArea textArea1;

    private void setupUI() {
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new com.intellij.uiDesigner.core.GridLayoutManager(2, 3, new Insets(0, 0, 0, 0), -1, -1));
        button1 = new JButton();
        button1.setText("Button");
        panel1.add(button1, new com.intellij.uiDesigner.core.GridConstraints(1, 2, 1, 1, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_HORIZONTAL, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_SHRINK | com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_CAN_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        textField1 = new JTextField();
        panel1.add(textField1, new com.intellij.uiDesigner.core.GridConstraints(1, 0, 1, 2, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_WEST, com.intellij.uiDesigner.core.GridConstraints.FILL_HORIZONTAL, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        textArea1 = new JTextArea();
        panel1.add(textArea1, new com.intellij.uiDesigner.core.GridConstraints(0, 0, 1, 3, com.intellij.uiDesigner.core.GridConstraints.ANCHOR_CENTER, com.intellij.uiDesigner.core.GridConstraints.FILL_BOTH, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, com.intellij.uiDesigner.core.GridConstraints.SIZEPOLICY_WANT_GROW, null, new Dimension(150, 50), null, 0, false));
        setSize(500, 400);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
}
    public static void main(String [] args){  
          SwingUtilities.invokeLater(
                    new Runnable(){ 
                    public void run(){new MainForm().setupUI();}
      }
    );
    }
}

And in general, I advise you to read at least this article or buy book Swing-effective user interfaces edition-2 Ivan Portyankin.

 1
Author: Kobayashi_Maru, 2012-01-31 20:33:33

Necropost and all that, but I'll share my bike 3 iterations. In the course of the project, it was necessary to create a lot of internal windows, so I decided to go through the abstract class. The window itself is created by the JIdea framework, just adding the inheritance of the abstract class. Redefining the getRootPanel () method, in which we return the topmost JPanel, which will become our contentPane. Then, in the external class, just call ManagebleWindow. getInstance(НужныйНамКлассОкна.class) and that's it.

In on the bike v. 2, I just inherited JInternalFrame and put all the initialization in the redefined setVisible () method, because the constructor works out before the GUI itself is generated, but this bike did not survive mvn:clean - it just stopped running and had to manually pull each window with all sorts of tambourines to make it start working (what happened there I never figured out), and this bike calmly experiences mvn: clean

import javax.swing.*;
import java.awt.*;


public abstract class ManageableWindow extends JInternalFrame {

public ManageableWindow(String title) {
    super(title, true, true, true, true);
}

protected void init(JPanel contentPane) {
    setContentPane(contentPane);

    createForm();
    createMenu();
    createListeners();

    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    setMinimumSize(getMinimumDimension());
    pack();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    setLocation((screenSize.width - getWidth()) / 2, (screenSize.height - getHeight()) / 2);
}

protected Dimension getMinimumDimension() {
    return new Dimension(500, 500);
}

protected abstract JPanel getBasePanel();

protected abstract void createMenu();

protected abstract void createForm();

protected abstract void createListeners();

public Object getParam() {
    return null;
}

public static ManageableWindow getInstance(Class<? extends ManageableWindow> aClass) {
    ManageableWindow window = null;
    try {
        window = aClass.newInstance();
        window.init(window.getBasePanel());
    } catch (InstantiationException e) {
        //e.printStackTrace();
    } catch (IllegalAccessException e) {
        //e.printStackTrace();
    }
    return window;
}

}
 1
Author: Ruslan Neruka, 2015-06-10 22:42:24