The idea is as follows:
Create a class and use extends to inherit the form class JFrame;
Create a JFrame object and use the setVisible() method of the JFrame class to set the form to be visible;
In the constructor, use the super() method to inherit the constructor of the parent class;
Use the setTitle() method to set the title of the form;
Use the setBounds() method to set the display position and size of the form;
Use the setDefaultCloseOperation() method to set the action of the form close button to exit;
Create a grid layout manager object using GridLayout;
Use the setHgap() method of the GridLayout class to set the horizontal spacing of the components;
Use the setVgap() method of the GridLayout class to set the vertical spacing of the component;
Create JPanel container object;
Setting the container through the setLayout() method of the JPanel class uses the grid layout manager;
Create a string-type two-dimensional array and initialize its value to the value displayed on the corresponding button on the calculator;
Create a JButton-type two-dimensional array and allocate the space corresponding to the previous string-type two-dimensional array;
Iterate through the string-type two-dimensional array, assign it to the corresponding buttons in the JButton-type two-dimensional array to each element, and add events to each button so that the corresponding value is displayed in the text input box when the button is clicked. , and finally use the add() method of the JPanel class to add the button to the panel.
The code copy is as follows:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
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.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
public class ButtonArrayExample extends JFrame { // Inherit the form class JFrame
/**
*
*/
private static final long serialVersionUID = 6626440733001287873L;
private JTextField textField;
public static void main(String args[]) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Throwable e) {
e.printStackTrace();
}
ButtonArrayExample frame = new ButtonArrayExample();
frame.setVisible(true); // Set the form to be visible, default to invisible
}
public ButtonArrayExample() {
super(); // Inherit the constructor of the parent class
BorderLayout borderLayout = (BorderLayout) getContentPane().getLayout();
borderLayout.setHgap(20);
borderLayout.setVgap(10);
setTitle("Button Array Implementation Calculator Interface"); // Set the title of the form
setBounds(100, 100, 290, 282); // Set the display position and size of the form
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Set the form close button to exit
textField = new JTextField();
textField.setHorizontalAlignment(SwingConstants.TRAILING);
textField.setPreferredSize(new Dimension(12, 50));
getContentPane().add(textField, BorderLayout.NORTH);
textField.setColumns(10);
final GridLayout gridLayout = new GridLayout(4, 0); // Create a grid layout manager object
gridLayout.setHgap(5); // Set the horizontal spacing of components
gridLayout.setVgap(5); // Set the vertical spacing of components
JPanel panel = new JPanel(); // Get container object
panel.setLayout(gridLayout); // Set the container to use grid layout manager
getContentPane().add(panel, BorderLayout.CENTER);
String[][] names = { { "1", "2", "3", "+" }, { "4", "5", "6", "-" }, { "7", " 8", "9", "×" }, { ".", "0", "=", "÷" } };
JButton[][] buttons = new JButton[4][4];
for (int row = 0; row < names.length; row++) {
for (int col = 0; col < names.length; col++) {
buttons[row][col] = new JButton(names[row][col]); // Create button object
buttons[row][col].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
String text = textField.getText();
textField.setText(text + button.getText());
}
});
panel.add(buttons[row][col]); // Add button to the panel
}
}
}
}