📄 calculator.java
字号:
}
try{
Integer.parseInt(input);
if(clsstatus)
{
textout.setText(input);
clsstatus=false;
}
else
{
textout.setText(textout.getText()+input);
}
}
catch(Exception ev)
{
//小数点比较特殊,输入后覆盖标志位不能取true,继续追加
if(input.equals("."))
{
int la=textout.getText().indexOf(".");
if(la==-1)
{
if(clsstatus)
{
textout.setText(input);
clsstatus=false;
}
else
{
textout.setText(textout.getText()+input);
}
}
}
//"="运算符,判断是否存在上次运算符,有,则计算出结果
//否则清楚记录,输出框不变
if(input.equals("="))
{
if(!comstatus)
{
clsstatus=true;
return;
}
lastcom();
comstatus=false;
clsstatus=true;
}
}
}
public void keyPressed(KeyEvent e)
{
int nKeycode=e.getKeyCode(); //记录键盘事件的keycode
if(nKeycode==8) //8为Backspace键
{
if(textout.getText().length()>1)
{
textout.setText(textout.getText().substring(
0,textout.getText().length()-1));
}
if(textout.getText().length()==1)
{
textout.setText("0");
}
}
if(nKeycode==10) //10为回车键,设为"="功能
{
if(!comstatus)
{
clsstatus=true;
return;
}
else
{
lastcom();
comstatus=false;
clsstatus=true;
}
}
//四则运算调用计算函数
//由于小键盘上的4个计算符经实践与字符"+","-","*","/"不同
//所以不能在KeyType()中使用getKeyChar()获得
if( nKeycode==107)
{
input="+";
mathgo();
clsstatus=true;
}
if(nKeycode==109 || nKeycode==45)
{
input="-";
mathgo();
clsstatus=true;
}
if(nKeycode==106)
{
input="*";
mathgo();
clsstatus=true;
}
if(nKeycode==111)
{
input="/";
mathgo();
clsstatus=true;
}
}
public void keyReleased(KeyEvent e)
{}
//自定义接口类型,用于切换计算器类型
private class ItemHandler implements ActionListener
{
public void actionPerformed( ActionEvent event )
{
if ( standard.isSelected() )
{
dispose();
new Standard();
}
if ( science.isSelected())
{
dispose();
new Science();
}
}
}
//参照课本453页,关于对话框
public class AboutDialog extends JDialog
{
public AboutDialog(JFrame owner)
{
super(owner,"关于",true);
Container contentPane=getContentPane();
Icon icon=new ImageIcon("pic\\javalogo.gif");
contentPane.add(new JLabel(icon),BorderLayout.WEST);
contentPane.add(new JLabel("<HTML><H1><I> 计算器</I></H1<HR>>"
+"Design by 赵腾</HTML>"),BorderLayout.CENTER); JPanel panel=new JPanel();
JButton ok=new JButton("Ok(O)");
ok.setMnemonic('o');
ok.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
dispose();
}
});
panel.add(ok);
contentPane.add(panel,BorderLayout.SOUTH);
this.setLocation(250,250);
setSize(250,150);
}
}
};
//科学型面板
//虽然可继承标准型省去变量定义.菜单和类型转换监听等代码,但要重定义几乎
//所有接口的函数,界面和监听,所以未采用继承
//除了界面,菜单,其他功能基本未实现
class Science extends JFrame
{
GridBagLayout gb;
GridBagConstraints gbc;
Container contentPane=getContentPane();
JRadioButtonMenuItem standard;
JRadioButtonMenuItem science;
private AboutDialog aboutdialog;
// private ExitDialog exitdialog;
public Science()
{
setTitle("科学型计算器");
gb=new GridBagLayout();
gbc=new GridBagConstraints();
contentPane.setLayout(gb);
String[] CC={"Backspace","CE","C"};
String[] NUM={"7","8","9","/","Mod","And","4","5","6","*","Or","Xor",
"1","2","3","-","Lsh","Not","0","+/-",".","+","=","Int",
"A","B","C","D","E","F"};
String[] MM={"Sta","F-E","(",")","MC","Ave","dms","Exp","ln","MR",
"Sum","sin","x^y","log","MS","s","cos","x^3","n!","M+","Dat",
"tan","x^2","1/x","pi"};
String[] LN={"十六进制","十进制","八进制","二进制"};
String[] COS={"角度","弧度","梯度"};
JButton[] butnum=new JButton[NUM.length];
JButton[] butcc=new JButton[CC.length];
JButton[] butmm=new JButton[MM.length];
JRadioButton[] rbln=new JRadioButton[LN.length];
JRadioButton[] rbcos=new JRadioButton[COS.length];
JTextField textout=new JTextField("0");
textout.setHorizontalAlignment(JTextField.RIGHT);
textout.setEditable(false);
textout.setBackground(Color.WHITE);
JCheckBox chkInv=new JCheckBox("Inv");
JCheckBox chkHyp=new JCheckBox("Hyp");
JPanel ccpanel=new JPanel();
for(int i=0;i<CC.length;i++)
{
butcc[i]=new JButton(CC[i]);
ccpanel.add(butcc[i]);
}
JPanel leftpanel=new JPanel();
leftpanel.setLayout(new GridLayout(5,5,5,5));
for(int i=0;i<MM.length;i++)
{
leftpanel.add(new JButton(MM[i]));
}
JPanel numpanel=new JPanel();
numpanel.setLayout(new GridLayout(5,6,5,5));
for(int i=0;i<NUM.length;i++)
{
butnum[i]=new JButton(NUM[i]);
numpanel.add(butnum[i]);
}
//2个单选按钮组
JPanel lnpanel =new JPanel();
lnpanel.setLayout(new GridLayout(1,4,5,5));
ButtonGroup lnbg=new ButtonGroup();
for(int i=0;i<LN.length;i++)
{
rbln[i]=new JRadioButton(LN[i]);
lnbg.add(rbln[i]);
lnpanel.add(rbln[i]);
}
rbln[0].setSelected(true);
JPanel cospanel=new JPanel();
cospanel.setLayout(new GridLayout(1,3,5,5));
ButtonGroup cosbg=new ButtonGroup();
for(int i=0;i<COS.length;i++)
{
rbcos[i]=new JRadioButton(COS[i]);
cosbg.add(rbcos[i]);
cospanel.add(rbcos[i]);
}
rbcos[0].setSelected(true);
JPanel nothing=new JPanel();
nothing.setLayout(new GridLayout(5,1));
nothing.add(new JLabel(" "));
gbc.fill=GridBagConstraints.HORIZONTAL;
addComponent(textout,4,0,12,1);
gbc.fill=GridBagConstraints.NONE;
addComponent(lnpanel,0,1,8,1);
gbc.fill=GridBagConstraints.NONE;
addComponent(cospanel,8,1,6,1);
gbc.fill=GridBagConstraints.NONE;
addComponent(chkInv,0,2,2,1);
gbc.fill=GridBagConstraints.NONE;
addComponent(chkHyp,3,2,2,1);
gbc.fill=GridBagConstraints.NONE;
addComponent(ccpanel,8,2,6,1);
gbc.fill=GridBagConstraints.NONE;
addComponent(leftpanel,0,3,5,5);
gbc.fill=GridBagConstraints.HORIZONTAL;
addComponent(nothing,7,3,1,5);
gbc.fill=GridBagConstraints.NONE;
addComponent(numpanel,8,3,6,5);
setResizable(false);
setLocation(200,200);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
int i = JOptionPane.showConfirmDialog(Science.this,"退出吗?","Message",JOptionPane.YES_NO_OPTION);
if (i == JOptionPane.YES_OPTION)
System.exit(0);
}
});
menu();
pack();
show();
}
public void menu()
{
JMenuBar mb = new JMenuBar();
setJMenuBar(mb);
JMenu fileMenu = new JMenu("文件(F)");
fileMenu.setMnemonic('F');
JMenu viewMenu = new JMenu("查看(V)");
viewMenu.setMnemonic('V');
JMenu helpMenu=new JMenu("帮助(H)");
helpMenu.setMnemonic('H');
Icon icon2=new ImageIcon("pic\\collapsed.gif");
JMenuItem exit=new JMenuItem("退出(X)",icon2);
exit.setMnemonic('X');
exit.addActionListener(new //退出菜单调用自定义的退出对话框
ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
int i = JOptionPane.showConfirmDialog(Science.this,"退出吗?","Message",JOptionPane.YES_NO_OPTION);
if (i == JOptionPane.YES_OPTION)
System.exit(0);
}
});
fileMenu.addSeparator();
fileMenu.add(exit);
mb.add(fileMenu);
//给类型选择的单选按钮添加动作监听
//若当前为标准型则只需给科学型的单选按钮添加监听
ActionListener itemhandler=new ItemHandler();
standard=new JRadioButtonMenuItem("标准型(T)");
standard.setMnemonic('T');
science=new JRadioButtonMenuItem("科学型(S)",true);
science.setMnemonic('S');
standard.addActionListener( itemhandler );
ButtonGroup bg=new ButtonGroup(); //单选按钮添加到按钮组中
bg.add(standard);
bg.add(science);
viewMenu.add(standard);
viewMenu.add(science);
mb.add(viewMenu);
//关于和退出都使用自定义的对话框
Icon icon1=new ImageIcon("pic\\addt.gif");
JMenuItem guanyu=new JMenuItem("关于(G)",icon1);
guanyu.setMnemonic('G');
guanyu.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
if(aboutdialog==null)
aboutdialog=new AboutDialog(Science.this);
aboutdialog.show();
}
});
helpMenu.add(guanyu);
mb.add(helpMenu);
}
public void addComponent
(Component c,int x,int y,int width,int height)
{
gbc.gridx=x;
gbc.gridy=y;
gbc.gridwidth=width;
gbc.gridheight=height;
gb.setConstraints(c,gbc);
contentPane.add(c);
}
private class ItemHandler implements ActionListener
{
public void actionPerformed( ActionEvent event )
{
if ( standard.isSelected() )
{
dispose();
new Standard();
}
if ( science.isSelected())
{
dispose();
new Science();
}
}
}
//参照课本453页,关于对话框
public class AboutDialog extends JDialog
{
public AboutDialog(JFrame owner)
{
super(owner,"关于",true);
Container contentPane=getContentPane();
Icon icon=new ImageIcon("pic\\javalogo.gif");
contentPane.add(new JLabel(icon),BorderLayout.WEST);
contentPane.add(new JLabel("<HTML><H1><I> 计算器</I></H1<HR>>"
+"Design by 赵腾</HTML>"),BorderLayout.CENTER);
JPanel panel=new JPanel();
JButton ok=new JButton("Ok(O)");
ok.setMnemonic('o');
ok.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
dispose();
}
});
panel.add(ok);
contentPane.add(panel,BorderLayout.SOUTH);
this.setLocation(250,250);
setSize(250,150);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -