Swing实现计算器GUI
2020-12-13 02:42
标签:class blog code java tar ext Swing实现计算器GUI,搜素材,soscw.com Swing实现计算器GUI 标签:class blog code java tar ext 原文地址:http://blog.csdn.net/superstonne/article/details/29359977package swing;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
/**
*
* @author He Jinlong
* @version 2.0 第一个版本没有使用网格布局,显示出来的效果很糟糕,通过使用网格布局,终于实现了想要的结果
*/
public class NewCalculator extends JFrame {
// 定义菜单栏
private JMenuBar bar;
private JMenu mune;
private JMenuItem itemhelp;
private JMenuItem itemcaculater;
// 建立2个大的容器
private JPanel textPanel;// 存放显示屏的容器
private JPanel btnPanel;// 存放按钮的容器
private GridBagLayout gb;// 定义网格布局
private GridBagConstraints gbc;// 定义网格的布局的大管家
private JButton btn[] = new JButton[18];// 定义按钮数组
private JTextArea textArea;// 定义显示屏
private String btnStr[] = new String[18];// 定义按钮内容
/**
* 构造方法,用来初始化计算器
*/
public NewCalculator() {
init();
addComponent();
setJMenuBar(bar);
setResizable(false);
setLocation(500, 220);
setTitle("我的计算器");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize(240, 300);
}
/**
* 初始化各个小组件
*/
public void init() {
bar = new JMenuBar();
mune = new JMenu("帮助");
itemhelp = new JMenuItem("帮助");
itemcaculater = new JMenuItem("关于计算机");
itemhelp.addActionListener(menuListener);
itemcaculater.addActionListener(menuListener);
textPanel = new JPanel();
btnPanel = new JPanel();
gb = new GridBagLayout();
gbc = new GridBagConstraints();
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(2, 2, 2, 2);
gbc.gridx = 0;
gbc.gridy = 0;
textArea = new JTextArea(3, 20);
btnStr = new String[] { " c ", " EDL ", " * ", " / ", " 1 ",
" 2 ", " 3 ", " + ", " 4 ", " 5 ", " 6 ", " - ", " 7 ",
" 8 ", " 9 ", " = ", " 0 ", " . " };
for (int i = 0; i
下一篇:Command Network