Home  >  Article  >  Java  >  Java button control array implementation of calculator interface example sharing

Java button control array implementation of calculator interface example sharing

高洛峰
高洛峰Original
2017-01-20 17:03:351850browse

The idea is as follows:

Create a class and make it inherit the form class JFrame through extends;
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 construction method 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;
Use GridLayout to create a grid layout manager object;
Use the setHgap() method of the GridLayout class to set the horizontal spacing of the component;
Use The setVgap() method of the GridLayout class sets the vertical spacing of the component;
Create a JPanel container object;
Set the container to use the grid layout manager through the setLayout() method of the JPanel class;
Create a string type II Dimensional array, 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;
Traverse characters String two-dimensional array, assign each element of it to the corresponding button in the JButton two-dimensional 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 Use the add() method of the JPanel class to add buttons to the panel.

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 { // 继承窗体类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); // 设置窗体可见,默认为不可见
    }

    public ButtonArrayExample() {
        super(); // 继承父类的构造方法
        BorderLayout borderLayout = (BorderLayout) getContentPane().getLayout();
        borderLayout.setHgap(20);
        borderLayout.setVgap(10);
        setTitle("按钮数组实现计算器界面 "); // 设置窗体的标题
        setBounds(100, 100, 290, 282); // 设置窗体的显示位置及大小
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗体关闭按钮的动作为退出
        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); // 创建网格布局管理器对象
        gridLayout.setHgap(5); // 设置组件的水平间距
        gridLayout.setVgap(5); // 设置组件的垂直间距
        JPanel panel = new JPanel(); // 获得容器对象
        panel.setLayout(gridLayout); // 设置容器采用网格布局管理器
        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]); // 创建按钮对象
                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]); // 将按钮添加到面板中
            }
        }
    }

}

For more java button control array implementation calculator interface examples to share related articles, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn