Intellij (Swing) GUI not compiling because of Gradle

后端 未结 4 2212
被撕碎了的回忆
被撕碎了的回忆 2020-12-18 16:13

I have a Gradle project and I am trying to create a GUI (Swing, using Intellij); however, I keep receiving an error on compile. The exact same GUI code can be run on a stand

相关标签:
4条回答
  • 2020-12-18 16:36

    Just change execution from Gradle to Intellij won't fix the main problem because it will keep not working when the Gradle jar were built.

    I did the following to fix the problem and keep using Gradle.

    Into Editor -> GUI Designer -> Generate GUI into, Choose Java Source

    add Intellij forms dependency to your gradle project

    implementation 'com.intellij:forms_rt:7.0.3'
    

    Go the the GUI for editor and do some change (like create a new component or change a label) this way Intellij will generate the necessary code.

    Run the main class

    0 讨论(0)
  • 2020-12-18 16:46

    This seems to be an issue between Gradle and Intellij. A solution is to migrate to Maven. The .form file (generated by Intellij) for the GUI should be initialised at the line frame.setContentPane(new ApplicationGUI().rootPanel); but this does not happen. There are no issues when using Maven.

    0 讨论(0)
  • 2020-12-18 16:55

    Solved by changing intellij settings:

    I've checked:

    • automatically import ...
    • build and run using - set - intellij
    • run tests using - set - intellij
    • Gradle JVM - set - Use project JDK

    My code was:

    package com.mygdx.game.desktop;
    
    import javax.swing.*;
    import com.badlogic.gdx.backends.lwjgl.LwjglAWTCanvas;
    import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
    import com.mygdx.game.MyGdxGame;
    import java.awt.*;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    public class MainForm {
        private JPanel mainpanel_1;
        private JPanel visualizationPanel_1;
        private JButton button1;
    
        private static MyGdxGame visualization;
        private JFrame mainFrame;
    
        public MainForm(JFrame frame) {
            mainFrame = frame;
        }
    
        public static void main(String[] args) throws ClassNotFoundException, UnsupportedLookAndFeelException, InstantiationException, IllegalAccessException {
    
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    
            SwingUtilities.invokeLater(() -> {
                JFrame frame = new JFrame("My First Swing Example");
                frame.setResizable(false);
                frame.setMinimumSize(new Dimension(1300, 850));
                frame.setSize(1300, 850);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
                MainForm mf = new MainForm(frame);
                if(mf.mainpanel_1 != null) {
                    frame.setContentPane(mf.mainpanel_1);
                }
    
                visualization = new MyGdxGame();
    
                LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
                config.width = 1200;
                config.height = 800;
                config.forceExit = false;
                config.resizable = false;
                LwjglAWTCanvas lwjglCanvas = new LwjglAWTCanvas(visualization, config);
                mf.visualizationPanel_1.add(lwjglCanvas.getCanvas());
    
                frame.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent windowEvent) {
                        lwjglCanvas.stop();
                        System.exit(0);
                    }
    
    
                });
    
                frame.pack();
    
                // Setting the frame visibility to true
                frame.setVisible(true);
            });
        }
    
    
    }
    
    0 讨论(0)
  • 2020-12-18 17:02

    As mentioned in the comments, your rootPanel is never initialized thus it's null. JFrame has a jpanel contentPane already by default so you don't really need the below line, unless you intend to replace it :

    frame.setContentPane(new ApplicationGUI().rootPanel);
    

    public void setContentPane(Container contentPane)

    Sets the contentPane property.This method is called by the constructor. Swing's painting architecture requires an opaque JComponentin the containment hiearchy. This is typically provided by thecontent pane. If you replace the content pane it is recommended youreplace it with an opaque JComponent.

    Below's some sample code creating a frame and adding a label to it.

    public class ApplicationGUI{
        public static void main(String[] args) {
            JLabel aLabel = new JLabel("some text");
            JFrame frame = new JFrame("ApplicationGUI");
            frame.getContentPane().add(aLabel);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(300,300);
            frame.setVisible(true);
        }
    }
    
    0 讨论(0)
提交回复
热议问题