Use java language to make a form (pop-up window) to collect user input

foreword

       A recent request has a logical link: a value needs to be taken out from a locally saved xml file, and this value will change. Then the project manager asked me, can you make a small tool, so that users can directly write the changed value into the file through the interface operation, without having to find the file path, and edit the file? My first reaction turned out to be, I don't want to do it. But it's not impossible, it can be achieved using JFrame.

1. Introduction to JFrame

       JFrame refers to a computer language - the basic idea of ​​​​java GUI programs is based on JFrame, which is the object of the window on the screen, which can be maximized, minimized, and closed.
       The three basic building blocks of Swing: labels, buttons, and text fields; but you need a place to put them, and you want your users to know what to do with them. The JFrame class solves this problem -- it's a container that allows programmers to add other components to it, organize them, and present them to the user. JFrame is actually more than just for programmers to put components into it and present it to the user. Compared to its apparent simplicity, it's actually the most complex component in the Swing package. To keep components as simple as possible, JFrame acts as a bridge between operating system-independent Swing components and the operating system on which they actually run. JFrame is registered as a window in the native operating system, and by doing so, you can get many familiar characteristics of operating system windows: minimize/maximize, resize, move.

Only part of the construction method
       is given, which will be used in this case. For more methods, please refer to the official website information

JFrame() Constructs a new form that is initially invisible.
JFrame(String title) Creates a new, initially invisible Frame with the specified title.

2. Implementation steps:

  • 1. Create a new Java class
  • 2. Declare three controls [Define three components JLabel, JTextField and JButton]
    JLabel jlabel;
    JTextField jtf1;
    JButton jb1, jb2;
  • 3. Add a panel [define a JPanel control, and then add the control to the JPanel in turn]
        pane = new JPanel();
        pane.setBounds(10, 10, 400, 300);
        pane.setLayout(null);
        jlabel = new JLabel("设备主秘匙:");
        jlabel.setBounds(30, 40, 80, 30);
        jtf1 = new JTextField();
        jtf1.setBounds(110, 40, 260, 30);
        pane.add(jlabel);
        pane.add(jtf1);
  • 4. Window plus controls
        jb1.addActionListener(new ActionListener() {
    
    
            @Override
            public void actionPerformed(ActionEvent e) {
    
    
            }
        });
  • 5. Instantiate objects

  • 6. Save and run

3. Complete code

       The following is a small case, which can be rewritten according to this case.
       Main points of concern:
              1. How to make this form?
              2. How to get the content entered by the user?
       Only when the value entered by the user is obtained can further operations be performed.


/**
 * @author zyz
 * @version 1.0
 * @data 2023/7/16 15:06
 * @Description: 使用java制作一个窗体,用来收集用户输入的内容
 */

public class Form extends JFrame {
    
    
    JLabel jlabel;
    JTextField jtf1;
    JButton jb1, jb2;
    JPanel pane;

    private String PackageConfig = "";
    private String MainKey = "";

    public String getMainKey() {
    
    
        return MainKey;
    }

    public void setMainKey(String mainKey) {
    
    
        MainKey = mainKey;
    }

    public String getPackageConfig() {
    
    
        return this.PackageConfig;
    }

    public void setPackageConfig(String packageConfig) {
    
    
        this.PackageConfig = packageConfig;
    }

    /**
     * 构造函数
     *
     * @param title
     */
    public Form(String title) {
    
    
        super(title);
        this.getContentPane().setLayout(null);
        this.setBounds(500, 500, 460, 460);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        pane = new JPanel();
        pane.setBounds(10, 10, 400, 300);
        pane.setLayout(null);
        jlabel = new JLabel("设备主秘匙:");
        jlabel.setBounds(30, 40, 80, 30);
        jtf1 = new JTextField();
        jtf1.setBounds(110, 40, 260, 30);
        pane.add(jlabel);
        pane.add(jtf1);


        jb1 = new JButton("保存");
        jb1.setBounds(130, 100, 70, 30);
        pane.add(jb1);

        jb2 = new JButton("取消");
        jb2.setBounds(220, 100, 70, 30);
        pane.add(jb2);
        this.add(pane);


        /**
         * 处理文本内容
         * true 包含中文字符  false 不包含中文字符
         */
        jb1.addActionListener(new ActionListener() {
    
    
            @Override
            public void actionPerformed(ActionEvent e) {
    
    
                //正则表达式
                Pattern p = Pattern.compile("[\u4E00-\u9FA5|\\!|\\,|\\。|\\(|\\)|\\《|\\》|\\“|\\”|\\?|\\:|\\;|\\【|\\】]");
                String inputMainKey = jtf1.getText().trim();

                if (jtf1.getText().trim().length() == 0) {
    
    
                    JOptionPane.showMessageDialog(null, "输入内容不能为空!!!");
                } else {
    
    
                    Matcher m = p.matcher(inputMainKey);
                    if (m.find()) {
    
    
                        JOptionPane.showMessageDialog(null, "输入内容不合法,应该为数字+英文组合!!!");
                    } else {
    
    
                        JOptionPane.showMessageDialog(null, "修改成功!!!");
                    }
                }
            }

        });

        this.setVisible(true);
    }


    public static void main(String[] args) {
    
    
        Form c = new Form("修改主秘匙的值");
    }
}

4. Realize the effect

       The page might be ugly, but it works. The page layout can be designed according to your needs.

insert image description here

Validate the input content,

insert image description here

5. Afterword

       What I want to do is to encrypt the data entered by the user and save it to a local xml file. At the same time, I need to modify the files in different paths according to different systems. I also need to package this written java tool into a jar package. Make it so that users can directly click to run it. At the same time, it also needs to be considered. If jdk or jre is not installed in the user system, this problem must also be considered. Fortunately, the company project already includes jdk. I just need to write a bat script and point the j path to the jdk path in the project.

       Tip : If you use the third-party jar package you introduced in your project, you must ensure that the imported jar package is included in the final project jar package [you can use the packaging tool that comes with IDEA, or you can use maven to package it]. Otherwise, when running the jar package, it will report the problem that the class cannot be found.

For specific packaging, please refer to this blog: Maven uses IDEA’s own tools to package, and at the same time import the jar package under lib, double-click the jar package to run it directly

Guess you like

Origin blog.csdn.net/weixin_43304253/article/details/131750743