Making a hello world app in a Form in Intellij

有些话、适合烂在心里 提交于 2019-12-20 09:21:19

问题


I'm trying to make a hello world form in Intellij. I've created the form, but the question now is what code to make in main() to make the form run and show up?

PS: all the tutorials around seem to only focus on "how to do forms on intellij" not in "how to actually make it run, then".

Thanks


回答1:


  1. Go to the class with the same name as the form.
  2. Press the keyboard shortcut for "Generate". It's Ctrl+N on Mac OS X, Alt+Ins on Windows. Alternatively, from the menu, select menu Code → Generate.

  3. Select "Form main()".

Now the main method is written and inserted for you. It will look something like this:

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



回答2:


I just did my first Intellij Swing App. Steve McLeod has the right instructions, however, when I tried to generate the main method using Alt+Insert => Generate main, I received an error message about one of my panels not being bound. So I clicked on the gui designer page (.form), selected my top panel, and gave it a name.

Everything else was named for me, but for some reason, the panel name was blank. Once I did that, I was able to switch over to the form .java class, press "Alt+Insert" and generate a constructor (not required, but needed).

From there, I followed Steve's advice to generate a main method. One thing that threw me off was the expectation that my Intellij generated .java class would extend or implement something swing related - it didn't. Swing only shows up in the Intellij generated main method (besides the private variables).




回答3:


Check this tut while it is realy step-by-step:

JetBrains JavaFX HelloWorld



来源:https://stackoverflow.com/questions/5396738/making-a-hello-world-app-in-a-form-in-intellij

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!