Using Android Studio to develop executable Java application with interface

Using Android Studio to develop executable Java application with interface

1. Create Project and Module

Create a new project and then a new module select java library

2. Add Main method and test code

package com.example.javareplacecarcols;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class myClass {
    public static void main(String[] args){
        // 1. Create a top-level container (window)
        JFrame jf = new JFrame("Test Windows");          // create a window
        jf.setSize(800, 600);                       // Set window size
        jf.setLocationRelativeTo(null);             // Set the window position to the center of the screen
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); // Exit the program when the close button of the window is clicked (without this sentence, the program will not exit)

        // 2. Create an intermediate container (panel container)
        JPanel panel = new JPanel();                // Create a panel container, using the default layout manager

        // 3. Create a basic component (button) and add it to the panel container
        JButton btn = new JButton("Test Button");
        panel.add(btn);

        // 4. Set the panel container as the content panel of the window to the window
        jf.setContentPane(panel);

        // 5. Display window. The information created previously is in memory. Use jf.setVisible(true) to display the window in memory on the screen.
        jf.setVisible(true);
        //Button monitoring
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                System.out.println("onClice");
            }
        });
    }
}

3. Modify the jar package generated by the lib folder so that it can run by double clicking

After selecting the created module and running it, a. jar file will be generated in the lib folder
Open with compression software
Modify the MANIFEST.MF file in META-INF:

Manifest-Version: 1.0
Main-Class: com.example.javareplacecarcols.myClass //Add this line

Put the modified file back into the jar file, and then double-click the jar file to run.

Keywords: Java Android Windows

Added by lhaynes on Fri, 03 Apr 2020 04:56:06 +0300