Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
781 views
in Technique[技术] by (71.8m points)

java - Intellij (Swing) GUI not compiling because of Gradle

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 standard Java project and compiles fine.

GUI

package gui;
import javax.swing.*;

public class ApplicationGUI {
    private JPanel rootPanel;
    private JLabel testLabel;

    public static void main(String[] args) {
        JFrame frame = new JFrame("ApplicationGUI");
        frame.setContentPane(new ApplicationGUI().rootPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

Gradle

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'

    compile 'ca.uhn.hapi.fhir:hapi-fhir-base:3.7.0'
    compile 'ca.uhn.hapi.fhir:hapi-fhir-structures-dstu3:3.7.0'
    compile 'ca.uhn.hapi.fhir:hapi-fhir-client:3.7.0'
}

Error

Task :ApplicationGUI.main() FAILED Exception in thread "main" java.awt.IllegalComponentStateException: contentPane cannot be set to null. at javax.swing.JRootPane.setContentPane(JRootPane.java:621) at javax.swing.JFrame.setContentPane(JFrame.java:698) at gui.ApplicationGUI.main(ApplicationGUI.java:11)

In my settings I have the GUI Designer set to Java Source Code.

What am I doing wrong?

Cheers

EDIT1:

I can confirm that the above code works 100% fine with Maven..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Screenshot of IntelliJ

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);
        });
    }


}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...