java按钮控件数组实现计算器界面示例分享
时间:2021-09-12 22:37:59
思路如下:
创建一个类,通过extends使其继承窗体类JFrame;
创建一个JFrame对象,使用JFrame类的setVisible()方法设置窗体可见;
在构造函数中,使用super()方法继承父类的构造方法;
使用setTitle()方法设置窗体的标题;
使用setBounds()方法设置窗体的显示位置及大小;
使用setDefaultCloseOperation()方法设置窗体关闭按钮的动作为退出;
使用GridLayout创建网格布局管理器对象;
使用GridLayout类的setHgap()方法设置组件的水平间距;
使用GridLayout类的setVgap()方法设置组件的垂直间距;
创建JPanel容器对象;
通过JPanel类的setLayout()方法设置容器采用网格布局管理器;
创建一个字符串型二维数组,初始化其值为计算器上对应按钮上显示的值;
创建一个JButton型二维数组,并为其分配和之前的字符串型二维数组对应的空间;
遍历字符串型二维数组,对它的每个元素都将其赋值给JButton型二维数组中的对应按钮,并对每个按钮添加事件,使得点击按钮时在文本输入框中显示对应的值,最后使用JPanel类的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 { // 继承窗体类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]); // 将按钮添加到面板中
}
}
}
}
标签:java,按钮,计算器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
android6.0权限动态申请框架permissiondispatcher的方法
2023-07-31 10:51:57
Java 常量池的实例详解
2023-12-04 00:51:28
Java VisualVM监控远程JVM(详解)
2022-07-10 15:59:15
![](https://img.aspxhome.com/file/2023/8/61588_0s.jpg)
Java Annotation(Java 注解)的实现代码
2023-01-09 00:36:17
![](https://img.aspxhome.com/file/2023/2/61532_0s.png)
c#中WinForm用OpencvSharp实现ROI区域提取的示例
2023-03-22 13:54:10
![](https://img.aspxhome.com/file/2023/1/68071_0s.jpg)
简单了解Spring中常用工具类
2021-09-24 10:44:28
Spring Boot 2.0 配置属性自定义转换的方法
2021-10-18 12:07:44
SpringBoot实现动态定时任务的示例代码
2022-06-22 19:27:39
Java GUI编程实现在线聊天室
2022-04-07 22:50:09
MyBatis环境资源配置实现代码详解
2023-08-05 08:59:02
Java实现配置加载机制
2023-11-26 09:03:38
JavaWeb 中Cookie实现记住密码的功能示例
2023-04-06 05:06:48
![](https://img.aspxhome.com/file/2023/2/69102_0s.png)
SpringBoot中读取application.properties配置文件的方法
2023-10-20 17:29:05
![](https://img.aspxhome.com/file/2023/4/66494_0s.png)
Java类的初始化顺序知识点总结
2021-09-23 23:19:26
![](https://img.aspxhome.com/file/2023/8/63748_0s.png)
Java中List与数组之间的相互转换
2023-09-10 03:44:28
使用IDEA创建servlet JavaWeb 应用及使用Tomcat本地部署的实现
2023-09-11 06:04:09
![](https://img.aspxhome.com/file/2023/4/57814_0s.jpg)
python只需30行代码就能记录键盘的一举一动
2023-06-27 18:22:12
![](https://img.aspxhome.com/file/2023/3/82403_0s.gif)
简单实现Java web服务器
2023-10-11 16:47:13
![](https://img.aspxhome.com/file/2023/6/64536_0s.jpg)
Kafka Producer中的消息缓存模型图解详解
2022-05-03 06:00:13
![](https://img.aspxhome.com/file/2023/2/61012_0s.jpg)
不使用Math.random方法生成随机数(随机数生成器)
2021-11-28 05:08:46