计算器布局显示:
功能举例:
①13+20=33
②-7*14=-98:先输入7,再单击-/+将其变成负数,然后挨次单击×号、14与=
③7!=5040
④sin30°=0.5:先输入30,然后按下′″按钮转换成角度值,再按下sin即可显示结果
⑤sqrt(4)=2:利用鼠标点击4后再点击sqrt按键即可显示结果
⑥2*π=6.2832
……
源程序如下:javascript计算器代码
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
DecimalFormat;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JTextField;
public class Calucator extends JFrame {
private JTextField tf;
private JPanel panel1, panel2, panel3, panel4;
private JMenuBar myBar;
private JMenu menu1, menu2, menu3;
private JMenuItem editItem1, editItem2, help1, help2, help3;
private JRadioButtonMenuItem seeItem1, seeItem2;//单选框
private JCheckBoxMenuItem seeItem3;//复选框
private ButtonGroup bgb;
private String back;
private boolean IfResult = true, flag = false;
private String oper = ;
private double result = 0;
private Num numActionListener;
private DecimalFormat df;
public Calucator(){
super(科学计算器);//设置标题栏
df = new DecimalFormat();//保留四位小数
this.setLayout(new BorderLayout(10, 5));
panel1 = new JPanel(new GridLayout(1, 3, 10, 10));
panel2 = new JPanel(new GridLayout(5, 6, 5, 5));//5行6列
panel3 = new JPanel(new GridLayout(5, 1, 5, 5));
panel4 = new JPanel(new BorderLayout(5, 5));
/**
*菜单栏
*/
myBar = new JMenuBar();
menu1 = new JMenu(编辑);
menu2 = new JMenu(查看);
menu3 = new JMenu(匡助);
menu1.setFont(new Font(宋体, Font.PLAIN, 12));
menu2.setFont(new Font(宋体, Font.PLAIN, 12));
menu3.setFont(new Font(宋体, Font.PLAIN, 12));
/**
*编辑栏
*/
editItem1 = new JMenuItem(复制);
editItem2 = new JMenuItem(粘贴);
editItem1.setFont(new Font(宋体,Font.PLAIN,12)); editItem2.setFont(new Font(宋体,Font.PLAIN,12));
/**
*查看栏
*/
seeItem1 = new JRadioButtonMenuItem(科学型); seeItem2 = new JRadioButtonMenuItem(标准型); seeItem3 = new JCheckBoxMenuItem(数字分组);
seeItem1.setFont(new Font(宋体,Font.PLAIN,12)); seeItem2.setFont(new Font(宋体,Font.PLAIN,12)); seeItem3.setFont(new Font(宋体,Font.PLAIN,12));
/**
*匡助栏
*/
help1 = new JMenuItem(匡助主题);
help2 = new JMenuItem(关于计算器);
help1.setFont(new Font(宋体,Font.PLAIN,12)); help2.setFont(new Font(宋体,Font.PLAIN,12));
bgb = new ButtonGroup();//选项组
menu1.add(editItem1);
menu1.add(editItem2);
menu2.add(seeItem1);
menu2.add(seeItem2);
menu2.addSeparator();//添加一条分割线
menu2.add(seeItem3);
menu3.add(help1);
menu3.addSeparator();//添加一条分割线
menu3.add(help2);
myBar.add(menu1);
myBar.add(menu2);
myBar.add(menu3);
this.setJMenuBar(myBar);
numActionListener = new Num();//实现数字监听
/**
*文本域,即为计算器的屏幕显示区域
*/
tf = new JTextField();
tf.setEditable(false);//文本区域不可编辑
tf.setBackground(Color.white);//文本区域的背景
tf.setHorizontalAlignment(JTextField.RIGHT);//文字右对齐tf.setText();
tf.ateLoweredBevelBorder());
init();//对计算器进行初始化
}
/**
*初始化操作
*添加按钮
*/
private void init(){
addButton(panel1, , new Clear(), d);
addButton(panel1, , new Clear(), d);
addButton(panel1, , new Clear(), d);
addButton(panel2, , new Signs(), Color.magenta);
addButton(panel2, , new Signs(), Color.magenta);
addButton(panel2, , numActionListener, Color.blue);
addButton(panel2, , numActionListener, Color.blue);
addButton(panel2, , numActionListener, Color.blue);
addButton(panel2, ÷, new Signs(), d);
addButton(panel2, , new Signs(), Color.magenta);
addButton(panel2, , new Signs(), Color.magenta);
addButton(panel2, , numActionListener, Color.blue);
addButton(panel2, , numActionListener, Color.blue);
addButton(panel2, , numActionListener, Color.blue);
addButton(panel2, ×, new Signs(), d);
addButton(panel2, , new Signs(), Color.magenta);
addButton(panel2, , new Signs(), Color.magenta);
addButton(panel2, , numActionListener, Color.blue);
addButton(panel2, , numActionListener, Color.blue);
addButton(panel2, , numActionListener, Color.blue);
addButton(panel2, , new Signs(), d);
addButton(panel2, , new Signs(), Color.magenta);
addButton(panel2, , new Signs(), Color.magenta);
addButton(panel2, , numActionListener, Color.blue);
addButton(panel2, , new Clear(), Color.blue);
addButton(panel2, , new Dot(), Color.blue);
addButton(panel2, , new Signs(), d);
addButton(panel2, , new Signs(), Color.magenta);
addButton(panel2, , new Signs(), Color.magenta);
addButton(panel2, π, numActionListener, ange);
addButton(panel2, , numActionListener, ange);
addButton(panel2, ′″, new Signs(), ange);
addButton(panel2, , new Signs(), d);
JButton btns = new JButton(计算器);
btns.ateLoweredBevelBorder());
btns.setEnabled(false);//按钮不可操作
btns.setPreferredSize(new Dimension(20, 20));
panel3.add(btns);//加入按钮
addButton(panel3, , null, d);
addButton(panel3, , null, d);
addButton(panel3, , null, d);
addButton(panel3, , null, d);
panel4.add(panel1, BorderLayout.NORTH);
panel4.add(panel2, BorderLayout.CENTER);
this.add(tf, BorderLayout.NORTH);
this.add(panel3, BorderLayout.WEST);
this.add(panel4);
pack();
this.setResizable(false);//窗口不可改变大小
this.setLocation(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
*统一设置按钮的的使用方式
*@param panel
*@param name
*@param action
*@param color
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论