简体   繁体   English

如何在 IntelliJ IDEA 社区版中创建一个新的 Swing 应用程序?

[英]How do I create a new Swing app in IntelliJ IDEA Community edition?

I have created a new project using the Create New Project Wizard, by choosing "create project from scratch" but it's completely empty (no java classes at all, so I manually created a new swing form inside the empty project).我使用创建新项目向导创建了一个新项目,方法是选择“从头开始创建项目”,但它完全是空的(根本没有 java 类,所以我在空项目中手动创建了一个新的 swing 表单)。

In many other IDEs I have used there is a way to click once, and get a new "new Gui project", and I usually expect it in the "File -> New Project" wizard or something, comparable.在我使用过的许多其他 IDE 中,有一种方法可以单击一次,并获得一个新的“新 Gui 项目”,我通常希望它出现在“文件 -> 新项目”向导或类似的东西中。

There is a new project wizard in the IntelliJ IDEA IDE, but it only seems it can create a blank project, and then I can manually add a form to it. IntelliJ IDEA IDE里面有个新建项目向导,不过好像只能创建一个空白项目,然后我可以手动给它添加一个表单。 So I did that.所以我就那样做了。 But then, that lacks any of the usual Java code that you would expect it to have, to open up that form and show it as an application.但是,它缺少您期望它具有的任何常用 Java 代码,无法打开该表单并将其显示为应用程序。

I am trying to understand the features and capabilities of IntelliJ IDEA, and it seems strong as a very fast and efficient editor and debugger and build system GUI wrapper around the ANT build system, but I was wondering if there are more "RAD" features that I have merely overlooked.我试图了解 IntelliJ IDEA 的特性和功能,作为一个非常快速和高效的编辑器和调试器以及围绕 ANT 构建系统的构建系统 GUI 包装器,它似乎很强大,但我想知道是否有更多的“RAD”功能我只是忽略了。 I have done a bit of googling and reading the docs, but I haven't found much about using IntelliJ IDEA to build a GUI application in Java.我已经做了一些谷歌搜索和阅读文档,但我没有找到太多关于使用 IntelliJ IDEA 在 Java 中构建 GUI 应用程序的信息。

Where I'm currently stuck is when I tried to build and run my empty project with an empty form in it, and I get to some kind of "Run" target configuration screen, and I tried clicking the [+] icon, and adding "MyForm01" which is the empty swing form I created, and it says in a dialog box "MyForm01 is not acceptable".我目前陷入困境的地方是当我尝试构建并运行其中包含空表单的空项目时,我进入了某种“运行”目标配置屏幕,我尝试单击 [+] 图标,然后添加“MyForm01”是我创建的空 swing 表单,它在对话框中显示“MyForm01 不可接受”。 I know enough java to know that the basic "GUI app skeleton code" is not being auto-generated by the IDE. I could go and copy and paste something from the inte.net, but my interest here is in knowing whether the tool can automatically be used to build a GUI, with a workflow as simple as other RAD-style GUI builder tools, including NetBeans, which is the java tool I am most comfortable using, or Delphi, which is my main everyday tool, which is Pascal based, rather than Java.我知道 java 知道基本的“GUI 应用程序骨架代码”不是由 IDE 自动生成的。我可以 go 并从 inte.net 复制和粘贴一些东西,但我的兴趣在于了解该工具是否可以自动用于构建 GUI,其工作流程与其他 RAD 风格的 GUI 构建工具一样简单,包括 NetBeans,这是我最习惯使用的 java 工具,或者 Delphi,这是我的主要日常工具,它是基于 Pascal 的,而不是 Java。

There is no such thing as GUI project in IDEA. IDEA 中没有 GUI 项目这样的东西。 You can add GUI forms there at any time you need, just by RightClick -> New -> GUI Form.您可以随时在那里添加 GUI 表单,只需通过右键单击 -> 新建 -> GUI 表单。 You can create GUI app from it just by adding main() method into the form binding class.只需将main()方法添加到表单绑定类中,您就可以从中创建 GUI 应用程序。 IDEA does the job for you if you hit Alt-Ins (or menu Code->Generate) when in the binding class editor.如果您在绑定类编辑器中点击 Alt-Ins(或菜单 Code->Generate),IDEA 会为您完成这项工作。 The only requirement for this is to place correct name for the form's root panel.唯一的要求是为表单的根面板放置正确的名称。

You also should check this manual to discover some other things: https://www.jetbrains.com/help/idea/designing-gui-major-steps.html .您还应该查看本手册以发现其他一些内容: https : //www.jetbrains.com/help/idea/designing-gui-major-steps.html Anyway, the GUI builder is pretty intuitive.无论如何,GUI 构建器非常直观。

The following instruction applies to IntelliJ Idea 14.以下说明适用于 IntelliJ Idea 14。

  1. Enable UI Designer启用 UI 设计器
    1. File → Settings → Plugins → UI Designer文件 → 设置 → 插件 → UI 设计器
  2. Enable generate Java class启用生成 Java 类
    1. File → Settings → Editor → GUI Designer → change Generate GUI into “Java source code”文件 → 设置 → 编辑器 → GUI 设计器 → 将生成 GUI 更改为“Java 源代码”
  3. Go to the project view, and right click the package name where you want the generated Java class to be stored转到项目视图,然后右键单击要存储生成的 Java 类的包名称
    1. On the context menu, choose New → GUI Form, and set the bound class在上下文菜单上,选择 New → GUI Form,并设置绑定类
    2. In the designer, it will automatically put a Jpanel on the window.在设计器中,它会自动在窗口上放置一个 Jpanel。 Choose Jpanel, and first set the Layout manager to GridLayoutManager(IntelliJ).选择Jpanel,首先将Layout manager设置为GridLayoutManager(IntelliJ)。
    3. Adding components as needed, make sure to give some value to “field name”.根据需要添加组件,确保为“字段名称”赋予一些值。 This value will become the name of this component in the form class.该值将成为该组件在表单类中的名称。
    4. You can preview the form by right click and then choose preview.您可以通过右键单击并选择预览来预览表单。
    5. When you are done, change the layout manager for Jpanel to “GridBagLayout”.完成后,将 Jpanel 的布局管理器更改为“GridBagLayout”。
  4. Click on Build → Make Project to generate Java source codes and store them in the bound Java class.点击 Build → Make Project 生成 Java 源代码并存储在绑定的 Java 类中。

IntelliJ IDEA is first of all a very smart editor, not a RAD tool like Delphi. IntelliJ IDEA 首先是一个非常智能的编辑器,而不是像 Delphi 那样的 RAD 工具。

However, it has a powerful gui editor - just right click on any package in the Project view on the left, and select New GUI form or New Dialog , and it will let you create GUI.但是,它有一个强大的 gui 编辑器 - 只需右键单击左侧 Project 视图中的任何包,然后选择New GUI formNew Dialog ,它就会让您创建 GUI。 This question provides more info about running the gui. 这个问题提供了有关运行 gui 的更多信息。

Creating a New GUI Form or Dialog创建新的 GUI 窗体或对话框

To create a new GUI form创建新的 GUI 表单

1.On the main menu, choose File | 1.在主菜单上,选择文件| New, as described in the section Creating Files.新建,如创建文件部分所述。

2.On the pop-up menu, choose GUI Form. 2.在弹出的菜单中,选择GUI Form。

3.Specify the name of the new form, and select the layout manager. 3.指定新表单的名称,并选择布局管理器。

4.Check the option Create bound class, and specify the bound class name. 4.勾选创建绑定类选项,并指定绑定类名。

5.Click OK. 5. 单击确定。

To create a new dialog创建新对话框

1.On the main menu, choose File | 1.在主菜单上,选择文件| New, as described in the section Creating Files.新建,如创建文件部分所述。

2.On the pop-up menu, choose Dialog. 2.在弹出菜单中,选择对话框。

3.Specify the name of the new dialog. 3.指定新对话框的名称。

4.Specify the following options: 4.指定以下选项:

Generate main() method that creates and shows dialog.生成用于创建和显示对话框的 main() 方法。

Generate OK handler.生成 OK 处理程序。

Generate Cancel handler.生成取消处理程序。

5.Click OK. 5. 单击确定。

To create a File Template from a GUI form从 GUI 表单创建文件模板

1.Create a GUI form or open it in the GUI Designer. 1. 创建一个 GUI 窗体或在 GUI 设计器中打开它。

2.On the Main menu, choose Tools | 2.在主菜单上,选择工具| Save File as Template.将文件另存为模板。 The File Templates dialog displays the source code of the form. “文件模板”对话框显示表单的源代码。 The extension of the new template is .form 3.Specify the template name, and click OK.新模板的扩展名为.form 3.指定模板名称,单击“确定”。

To create a new GUI form from a File Template从文件模板创建新的 GUI 表单

1.In the Project tool window, right-click the package where you want to create the new form. 1.在项目工具窗口中,右键单击要创建新表单的包。 2.On the context menu choose New. 2.在上下文菜单上选择新建。 The submenu contains items for all defined File Templates, including GUI forms.子菜单包含所有定义的文件模板的项目,包括 GUI 表单。 3.Select the desired GUI form template on the context menu, and give it a name when prompted. 3. 在上下文菜单上选择所需的 GUI 表单模板,并在出现提示时为其命名。

Creating Snapshots创建快照

The Snapshot feature enables you to convert GUI forms created by some other means into IntelliJ IDEA GUI Designer Forms.快照功能使您能够将通过其他方式创建的 GUI 表单转换为 IntelliJ IDEA GUI 设计器表单。 All you need to do is run your application and save a part of the running application's component tree as a GUI Designer form.您需要做的就是运行您的应用程序并将正在运行的应用程序的组件树的一部分保存为 GUI 设计器表单。 From then on, you can use the GUI Designer to extend or modify the form.从那时起,您可以使用 GUI 设计器来扩展或修改表单。

You can work with the layout and code in the GUI Designer just as you do with Forms originally created with the GUI Designer.您可以在 GUI 设计器中使用布局和代码,就像在最初使用 GUI 设计器创建的表单一样。

To take a Snapshot of an existing dialog拍摄现有对话框的快照

1.Open the module that contains source code for the dialog you want to capture. 1.打开包含要捕获的对话框的源代码的模块。

2.Select the Application Run Configuration, make sure that the option Enable capturing form snapshots is checked, and run the application. 2. 选择应用程序运行配置,确保选中启用捕获表单快照选项,然后运行应用程序。

3.In the running application, perform whatever interaction is necessary to open the dialog you want to capture. 3.在正在运行的应用程序中,执行打开要捕获的对话框所需的任何交互。

4.In the Project tool window, right-click the target package and choose New | 4.在Project工具窗口中,右键单击目标包并选择New | Form Snapshot on the context menu.上下文菜单上的表单快照。 If the application is not yet running, IntelliJ IDEA prompts you to run it, and then prepare the application for taking snapshot: enter image description here如果应用程序尚未运行,IntelliJ IDEA 会提示您运行它,然后准备应用程序进行快照:在此处输入图像描述

The Create Form Snapshot dialog box appears.出现创建表单快照对话框。

5.In the IntelliJ IDEA's Create Form Snapshot dialog, select the top-level component to be included in the snapshot, enter the form name, and click Create Snapshot button. 5.在IntelliJ IDEA的Create Form Snapshot对话框中,选择要包含在快照中的顶层组件,输入表单名称,点击Create Snapshot按钮。 The new form created this way is not bound to any class.以这种方式创建的新表单不绑定到任何类。 You have to bind it to a class yourself.您必须自己将其绑定到一个类。 enter image description here在此处输入图片说明

Opening Existing GUI Forms打开现有的 GUI 表单

1.Open form files the same way you open source code or text files for editing. 1.打开表单文件的方式与打开源代码或文本文件进行编辑的方式相同。 Locate the file in the appropriate navigation view, select it, and do one of the following:在相应的导航视图中找到该文件,选择它,然后执行以下操作之一:

-Double-click. - 双击。

-Choose Jump to Source on the context menu. - 在上下文菜单上选择跳转到源。

-Press F4. - 按 F4。

Each form opens in a separate tab in the same space occupied by the Editor for source code files.每个表单都在单独的选项卡中打开,该选项卡位于编辑器为源代码文件占用的相同空间中。 You can have source code/text files and form files open simultaneously.您可以同时打开源代码/文本文件和表单文件。

Here's my basic shell for a one-window GUI application:这是我的单窗口 GUI 应用程序的基本 shell:

import java.awt.BorderLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

/**
 * <p>Created by IntelliJ IDEA.
 * <p>Date: 1/25/23
 * <p>Time: 7:59 PM
 *
 * @author Miguel Mu\u00f1oz
 */
public class GenericUIApp extends JPanel {
  public static void main(String[] args) {
    JFrame frame = new JFrame("Generic UI App");
    frame.add(new GenericUIApp());
    frame.setLocationByPlatform(true);
    frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    frame.pack(); // Sets the window to fit the natural size of its content pane
    frame.setVisible(true);
  }
  
  private GenericUIApp() {
    super(new BorderLayout());
    // replace this code with your UI code.
    add(new JLabel("Put your UI Contents here."));
    setBorder(BorderFactory.createMatteBorder(100, 100, 100, 100, getBackground()));
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM