由于Gradle无法编译Intellij(Swing)GUI [英] Intellij (Swing) GUI not compiling because of Gradle
问题描述
我有一个Gradle项目,并且正在尝试创建GUI(Swing,使用Intellij);但是,我一直收到编译错误.完全相同的GUI代码可以在标准Java项目上运行,并且可以很好地编译.
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'
}
错误
任务:ApplicationGUI.main()失败 线程主"中的异常java.awt.IllegalComponentStateException:contentPane不能设置为null. 在javax.swing.JRootPane.setContentPane(JRootPane.java:621) 在javax.swing.JFrame.setContentPane(JFrame.java:698) 在gui.ApplicationGUI.main(ApplicationGUI.java:11)
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)
在我的设置中,我将GUI设计器设置为Java源代码.
In my settings I have the GUI Designer set to Java Source Code.
我在做什么错了?
欢呼
我可以确认上述代码在Maven中可以100%正常工作..
I can confirm that the above code works 100% fine with Maven..
推荐答案
通过更改intellij设置来解决:
Solved by changing intellij settings:
我已检查:
- 自动导入...
- 使用-set-intellij构建并运行
- 使用-set-intellij运行测试
- Gradle JVM-设置-使用项目JDK
我的代码是:
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);
});
}
}
这篇关于由于Gradle无法编译Intellij(Swing)GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!