Java button control array implementation calculator interface sample sharing

  • 2020-04-01 03:00:45
  • OfStack

The idea is as follows:

Create a class that extends to inherit from the form class JFrame.
Create a JFrame object and use the setVisible() method of the JFrame class to make the form visible.
In the constructor, the super() method is used 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 GridLayout 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 components.
Create the JPanel container object;
The setLayout() method of the JPanel class is used to set the container to use the grid layout manager.
Creates a string two-dimensional array and initializes its value to the value displayed on the corresponding button on the calculator.
Create a jbutt-type 2d array and allocate the space corresponding to the previous string 2d array.
Iterate over the string 2d array, assign each element to the corresponding button in the jbutt-type 2d array, and add an event to each button, so that the corresponding value is displayed in the text input box when the button is clicked. Finally, add the button to the panel using the add() method of JPanel class.


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 { //Inherits 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); //Sets the form to be visible, not visible by default
    }

    public ButtonArrayExample() {
        super(); //Inherits the constructor of the parent class
        BorderLayout borderLayout = (BorderLayout) getContentPane().getLayout();
        borderLayout.setHgap(20);
        borderLayout.setVgap(10);
        setTitle(" Button array implementation calculator interface  "); //Sets the title of the form
        setBounds(100, 100, 290, 282); //Sets the display position and size of the form
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Sets the action of 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 the grid layout manager object
        gridLayout.setHgap(5); //Sets the horizontal spacing of the components
        gridLayout.setVgap(5); //Sets the vertical spacing of the components
        JPanel panel = new JPanel(); //Get the container object
        panel.setLayout(gridLayout); //The setup container USES the grid layout manager
        getContentPane().add(panel, BorderLayout.CENTER);
        String[][] names = { { "1", "2", "3", " + " }, { "4", "5", "6", " - " }, { "7", "8", "9", " x " }, { ".", "0", "=", " present " } };
        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]); //Adds a button to the panel
            }
        }
    }

}


Related articles: