思路如下:
創(chuàng)建一個(gè)類(lèi),通過(guò)extends使其繼承窗體類(lèi)JFrame;
創(chuàng)建一個(gè)JFrame對(duì)象,使用JFrame類(lèi)的setVisible()方法設(shè)置窗體可見(jiàn);
在構(gòu)造函數(shù)中,使用super()方法繼承父類(lèi)的構(gòu)造方法;
使用setTitle()方法設(shè)置窗體的標(biāo)題;
使用setBounds()方法設(shè)置窗體的顯示位置及大小;
使用setDefaultCloseOperation()方法設(shè)置窗體關(guān)閉按鈕的動(dòng)作為退出;
使用GridLayout創(chuàng)建網(wǎng)格布局管理器對(duì)象;
使用GridLayout類(lèi)的setHgap()方法設(shè)置組件的水平間距;
使用GridLayout類(lèi)的setVgap()方法設(shè)置組件的垂直間距;
創(chuàng)建JPanel容器對(duì)象;
通過(guò)JPanel類(lèi)的setLayout()方法設(shè)置容器采用網(wǎng)格布局管理器;
創(chuàng)建一個(gè)字符串型二維數(shù)組,初始化其值為計(jì)算器上對(duì)應(yīng)按鈕上顯示的值;
創(chuàng)建一個(gè)JButton型二維數(shù)組,并為其分配和之前的字符串型二維數(shù)組對(duì)應(yīng)的空間;
遍歷字符串型二維數(shù)組,對(duì)它的每個(gè)元素都將其賦值給JButton型二維數(shù)組中的對(duì)應(yīng)按鈕,并對(duì)每個(gè)按鈕添加事件,使得點(diǎn)擊按鈕時(shí)在文本輸入框中顯示對(duì)應(yīng)的值,最后使用JPanel類(lèi)的add()方法將按鈕添加到面板中。
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 { // 繼承窗體類(lèi)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); // 設(shè)置窗體可見(jiàn),默認(rèn)為不可見(jiàn)
}
public ButtonArrayExample() {
super(); // 繼承父類(lèi)的構(gòu)造方法
BorderLayout borderLayout = (BorderLayout) getContentPane().getLayout();
borderLayout.setHgap(20);
borderLayout.setVgap(10);
setTitle("按鈕數(shù)組實(shí)現(xiàn)計(jì)算器界面 "); // 設(shè)置窗體的標(biāo)題
setBounds(100, 100, 290, 282); // 設(shè)置窗體的顯示位置及大小
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 設(shè)置窗體關(guān)閉按鈕的動(dòng)作為退出
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); // 創(chuàng)建網(wǎng)格布局管理器對(duì)象
gridLayout.setHgap(5); // 設(shè)置組件的水平間距
gridLayout.setVgap(5); // 設(shè)置組件的垂直間距
JPanel panel = new JPanel(); // 獲得容器對(duì)象
panel.setLayout(gridLayout); // 設(shè)置容器采用網(wǎng)格布局管理器
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]); // 創(chuàng)建按鈕對(duì)象
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]); // 將按鈕添加到面板中
}
}
}
}