使用GUI Form快速创建简单界面

使用GUI Form快速创建简单界面

使用GUI Form快速创建简单界面


简述

GUI Form是IntelliJ IDEA提供的快速创建GUI界面的功能,通过拖拽组装组件、自动生成代码的方式完成GUI界面的绘制。

准备工作

  • 设置GUI代码生成的位置为source code,打开File | Settings | Editor | GUI Designer,并设置
    在这里插入图片描述

  • 引入(自动生成的GUI源码需要的)依赖

    <dependency>
        <groupId>com.intellij</groupId>
        <artifactId>forms_rt</artifactId>
        <version>7.0.3</version>
    </dependency>
    

使用示例

第一步:创建类及对应的form文件

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

第二步:给From中的组件JPanel起一个字段名(,否者下面在生成main方法时会报错)

在这里插入图片描述

第三步:拖动组件,在画板中完成UI

在这里插入图片描述
在这里插入图片描述

第四步:给组件添加监听

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

示例:

public class DemoGUI {
    private JPanel jPanel;
    private JTextField jTextField;
    private JLabel jLable;
    private JButton button;
    
    public DemoGUI() {
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String inputText = jTextField.getText();
                
                /// 弹出框
                JDialog jDialog = new JDialog();
                // 设置相对位置. null代表位于屏幕居中
                jDialog.setLocationRelativeTo(null);
                // 设置标题
                jDialog.setTitle("Information");
                // 设置可见性
                jDialog.setVisible(true);
                // 设置大小
                jDialog.setSize(200, 80);
                // 设置弹出框图标
                try {
                    InputStream inputStream = Objects.requireNonNull(DemoGUI.class.getResourceAsStream("/information.png"));
                    jDialog.setIconImage(ImageIO.read(inputStream));
                } catch (Exception exc) {
                    throw new UndeclaredThrowableException(exc);
                }
    
                // 给弹出框面板添加组件
                Container contentPane = jDialog.getContentPane();
                contentPane.add(new JLabel("hello~ " + inputText));
            }
        });
    }

第五步:生成main方法

在这里插入图片描述
在这里插入图片描述

第六步:运行main方法,(idea自动)生成GUI对应源码

生成源码:
在这里插入图片描述

提示:如果你想修改生成的GUI代码(即:你想避免每次运行main方法时都生成GUI代码),你只需要使java类没有对应的form文件即可:

  • 你可以在首次生成GUI源码后,删除掉对应的form文件,然后再修改GUI源码即可。
  • 你也可以在首次生成GUI源码后,直接复制一个新的java类出来(只复制java类不复制对应的form文件),然后再修改GUI源码即可。

观察效果:

在这里插入图片描述

点击【确定】,弹出新的对话框:
在这里插入图片描述

第七步:将项目打成可执行jar包,以便使用

  • 在pom中添加打包插件maven-shade-plugin

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>org.example</groupId>
        <artifactId>gui-form</artifactId>
        <version>1.0.0</version>
    
        <properties>
            <maven.compiler.source>8</maven.compiler.source>
            <maven.compiler.target>8</maven.compiler.target>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
    
        <dependencies>
            <!-- https://mvnrepository.com/artifact/com.intellij/forms_rt -->
            <dependency>
                <groupId>com.intellij</groupId>
                <artifactId>forms_rt</artifactId>
                <version>7.0.3</version>
            </dependency>
        </dependencies>
    
    
    	<build>
    	    <resources>
    	        <resource>
    	            <directory>src/main/resources</directory>
    	            <includes>
    	                <!--包含文件夹以及子文件夹下所有资源-->
    	                <include>**/*.*</include>
    	            </includes>
    	        </resource>
    	    </resources>
    
    	    <plugins>
    	        <plugin>
    	            <groupId>org.apache.maven.plugins</groupId>
    	            <artifactId>maven-shade-plugin</artifactId>
    	            <version>3.2.4</version>
    	            <configuration>
    	                <transformers>
    	                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
    	                        <resource>META-INF/spring.handlers</resource>
    	                    </transformer>
    	                    <transformer implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
    	                        <resource>META-INF/spring.factories</resource>
    	                    </transformer>
    	                    <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
    	                        <resource>META-INF/spring.schemas</resource>
    	                    </transformer>
    	                    <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
    	                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
    	                        <!-- main类 -->
    	                        <mainClass>com.example.guiform.DemoGUI</mainClass>
    	                    </transformer>
    	                </transformers>
    	            </configuration>
    	            <executions>
    	                <execution>
    	                    <phase>package</phase>
    	                    <goals>
    	                        <goal>shade</goal>
    	                    </goals>
    	                </execution>
    	            </executions>
    	        </plugin>
    	    </plugins>
    	</build>
    
    </project>
    
  • 打成jar包并运行

在这里插入图片描述
在这里插入图片描述


相关资料

  • 9
    点赞
  • 69
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值