首页 > 代码库 > Swing实现计算器GUI

Swing实现计算器GUI

package 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 < btn.length; i++) {
			btn[i] = new JButton(btnStr[i]);
			btn[i].addActionListener(buttonListener);
		}

	}

	/**
	 * 将各个小组件添加到容器中
	 */
	public void addComponent() {
		textArea.setEditable(false);
		textPanel.add(textArea);

		btnPanel.setLayout(gb);
		for (int i = 0; i < btn.length; i++) {
			int x = 1;
			if (i == 15) {
				gbc.gridheight = 2;
			} else if (i == 17) {
				gbc.gridwidth = 2;
			}
			gb.setConstraints(btn[i], gbc);
			btnPanel.add(btn[i]);
			gbc.gridwidth = 1;
			gbc.gridheight = 1;
			gbc.gridx += x;
			if ((i + 1) % 4 == 0) {
				gbc.gridx = 0;
				gbc.gridy += 1;
			}

		}

		add(BorderLayout.NORTH, textPanel);

		add(BorderLayout.CENTER, btnPanel);

		bar.add(mune);

		mune.add(itemhelp);
		mune.add(itemcaculater);
	}

	// 实例化一个监听器,用来监听按钮点击事件
	private ActionListener buttonListener = new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent e) {

			String str = ((JButton) e.getSource()).getText();
			JOptionPane.showMessageDialog(null, "You clicked " + str
					+ " button");
		}
	};

	// 同上,用来监听菜单上的点击事件
	private ActionListener menuListener = new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent e) {

			String str = ((JMenuItem) e.getSource()).getText();
			JOptionPane.showMessageDialog(null, "You clicked " + str
					+ " button");
		}
	};

	public static void main(String[] args) {
		new NewCalculator();
	}
}