Let’s take a look at the effect first
1.Source code
Without further ado, let’s get straight to the code:
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class ColorPicker extends JFrame { private JButton colorButton; private JPanel colorPanel; public ColorPicker() { setTitle("Color Picker"); setSize(400, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Set fonts that support Chinese character set Font chineseFont = new Font("Microsoft Yahei", Font.PLAIN, 16); colorButton = new JButton("Select color"); colorButton.setFont(chineseFont); // Use Chinese supported fonts colorPanel = new JPanel(); colorPanel.setPreferredSize(new Dimension(200, 100)); colorPanel.setBorder(BorderFactory.createEtchedBorder()); colorButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Color selectedColor = JColorChooser.showDialog(null, "selected color", colorPanel.getBackground()); if (selectedColor != null) { colorPanel.setBackground(selectedColor); } } }); setLayout(new BorderLayout()); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); buttonPanel.add(colorButton); JPanel mainPanel = new JPanel(); mainPanel.setLayout(new BorderLayout()); mainPanel.add(buttonPanel, BorderLayout.NORTH); mainPanel.add(colorPanel, BorderLayout.CENTER); add(mainPanel, BorderLayout.CENTER); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { ColorPicker colorPicker = new ColorPicker(); colorPicker.setVisible(true); colorPicker.setLocationRelativeTo(null); // Display in the center of the screen } }); } }
The code is interpreted as follows:
import javax.swing.*; // Import the Swing package, which is Java's library for creating graphical user interfaces (GUIs) import java.awt.*; // Import the AWT package, which contains the classes and methods required to build and draw user interfaces import java.awt.event.ActionEvent; // Import the ActionEvent class, which is a class of event processing in Java and is used to handle user interface events import java.awt.event.ActionListener; // Import the ActionListener interface, which contains methods for handling user interface events public class ColorPicker extends JFrame { // Define a class named ColorPicker, which inherits from the JFrame class (Java window class) private JButton colorButton; // Define a private JButton object to create a button that displays "Select Color" private JPanel colorPanel; // Define a private JPanel object to display the selected color public ColorPicker() { // Define the constructor of the ColorPicker class setTitle("Color Picker"); // Set the title of the window to "Color Picker" setSize(400, 200); //Set the window size to 400x200 pixels setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Set the default operation when the window is closed to exit the program //Set fonts that support Chinese character set Font chineseFont = new Font("Microsoft Yahei", Font.PLAIN, 16); // Create a font object, using the "Microsoft Yahei" font, normal style (no bold, italics, etc.), with a size of 16 colorButton = new JButton("Select Color"); // Create a new JButton object and display the text "Select Color" colorButton.setFont(chineseFont); //Set the font defined above for this button colorPanel = new JPanel(); // Create a new JPanel object to display the selected color colorPanel.setPreferredSize(new Dimension(200, 100)); // Set the preferred size of this panel to 200x100 pixels colorPanel.setBorder(BorderFactory.createEtchedBorder()); // Set a border effect for this panel, the style is an etched style border colorButton.addActionListener(new ActionListener() { //Add an action listener for this button, and trigger the corresponding event processing method when the button is clicked @Override public void actionPerformed(ActionEvent e) { // This is the event handling method, which will be called when the button is clicked Color selectedColor = JColorChooser.showDialog(null, "Select Color", colorPanel.getBackground()); // Use the JColorChooser class to display a dialog box to let the user select a color, and the selected color will be returned (selectedColor) if (selectedColor != null) { // If the user selects a color (that is, selectedColor is not null) colorPanel.setBackground(selectedColor); // Set the background color of this panel to the selected color } } }); setLayout(new BorderLayout()); // Set the layout for the window. The BorderLayout layout is used here. You can place components in the north, south, east, west and center positions. JPanel buttonPanel = new JPanel(); // Create a new JPanel object for placing the "Select Color" button buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); //Set the layout for this button panel. The FlowLayout layout is used here. The buttons will be arranged horizontally in the center (centered) buttonPanel.add(colorButton); // Add the "Select Color" button to this button panel JPanel mainPanel = new JPanel(); // Create a new JPanel object as the main panel mainPanel.setLayout(new BorderLayout()); // Set the layout for this main panel, the BorderLayout layout is used here
Just run ColorPicker, the effect is as follows
2. Package the jar into exe
1. First, package the ColorPicker into a jar package, and then package it successfully through the idea editor package.
2. Download and install JSmooth
JSmooth is an open source Java application primarily used to create native Windows launchers for Java applications. It can package Java applications into an independent executable file (.exe), allowing users to run Java applications directly without prior installation of a Java Virtual Machine (JVM). The advantage of JSmooth is that it can automatically recognize and utilize the installed JVM, and you can run Java applications through the JSmooth packaged exe file even on a computer without a JVM installed. In addition, JSmooth can also optimize Java applications to improve their operating efficiency, including compressing application size, improving startup speed, etc. In addition to the above functions, JSmooth also has some other features. For example, it supports customizing application icons, version information, etc., making packaged applications more beautiful and easier to use. In addition, JSmooth also provides functions such as logging and error reporting to facilitate developers for debugging and troubleshooting. In short, JSmooth is a very practical tool that can help developers deploy Java applications to the Windows operating system more conveniently. It also provides many additional functions to optimize and monitor the running status of Java applications.
3. Double-click to open JSmooth
4. Click on the skeleton, and I select Windows Wrapper from the skeleton (select according to the needs of my program)
5. Click on the executable file and fill in the file name (path) and icon of the generated EXE file.
6. Select the type of main class and the path to the jar
7. Click the small gear to generate the exe file
Follow “One Code First” and reply “jsmooth” to get it
Get the JSmooth installation package and color picker