文件名称:java的小程序
文件大小:21KB
文件格式:JAVA
更新时间:2015-11-01 04:20:29
java xiaochengxu
import java.util.Scanner;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.imageio.*;
public class Jsq{
public static void main(String [] as){
new JsqFrame();
}
}
class JsqFrame extends JFrame implements KeyListener{
private String s000 = "image/000.png";
private String s001 = "image/001.png";
private String s002 = "image/002.png";
private String s003 = "image/003.png";
private String s004 = "image/004.png";
private String s005 = "image/005.png";
private String s006 = "image/006.png";
private String s007 = "image/007.png";
private String s008 = "image/008.png";
private String s009 = "image/009.png";
private String sdian = "image/dian.png";
private String sjia = "image/jia.png";
private String sjian = "image/jian.png";
private String scheng = "image/cheng.png";
private String schu = "image/chu.png";
private String spf = "image/pf.png";
private String skf = "image/kf.png";
private String szkh = "image/zkh.png";
private String sykh = "image/ykh.png";
private String sht = "image/ht.png";
private String sqc = "image/qc.png";
private String sdy = "image/dy.png";
private String sbz = "image/help.png";
private boolean dian = true;
private boolean zkh = true;
private boolean qc = false;
private boolean dengyu = true;
private boolean i_=false;
private int p = 0;
private StackTally pt = new StackTally();
private StackNum pn = new StackNum();
private JButton blaf0 = new JButton(new ImageIcon(getClass().getResource(s000)));
private JButton blaf1 = new JButton(new ImageIcon(getClass().getResource(s001)));
private JButton blaf2 = new JButton(new ImageIcon(getClass().getResource(s002)));
private JButton blaf3 = new JButton(new ImageIcon(getClass().getResource(s003)));
private JButton blaf4 = new JButton(new ImageIcon(getClass().getResource(s004)));
private JButton blaf5 = new JButton(new ImageIcon(getClass().getResource(s005)));
private JButton blaf6 = new JButton(new ImageIcon(getClass().getResource(s006)));
private JButton blaf7 = new JButton(new ImageIcon(getClass().getResource(s007)));
private JButton blaf8 = new JButton(new ImageIcon(getClass().getResource(s008)));
private JButton blaf9 = new JButton(new ImageIcon(getClass().getResource(s009)));
private JButton blafzkh = new JButton(new ImageIcon(getClass().getResource(szkh)));
private JButton blafykh = new JButton(new ImageIcon(getClass().getResource(sykh)));
private JButton blafdian = new JButton(new ImageIcon(getClass().getResource(sdian)));
private JButton blafjia = new JButton(new ImageIcon(getClass().getResource(sjia)));
private JButton blafjian = new JButton(new ImageIcon(getClass().getResource(sjian)));
private JButton blafcheng = new JButton(new ImageIcon(getClass().getResource(scheng)));
private JButton blafchu = new JButton(new ImageIcon(getClass().getResource(schu)));
private JButton blafpf = new JButton(new ImageIcon(getClass().getResource(spf)));
private JButton blafkf = new JButton(new ImageIcon(getClass().getResource(skf)));
private JButton blafht = new JButton(new ImageIcon(getClass().getResource(sht)));
private JButton blafqc = new JButton(new ImageIcon(getClass().getResource(sqc)));
private JButton blafdy = new JButton(new ImageIcon(getClass().getResource(sdy)));
private JButton blafbz = new JButton(new ImageIcon(getClass().getResource(sbz)));
private JTextField txt = new JTextField("0");
private JTextArea txta = new JTextArea(4,10);
public JsqFrame(){
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JScrollPane scr = new JScrollPane(txta);
p1.setBorder(BorderFactory.createLoweredBevelBorder());
p1.setLayout(new GridBagLayout());
blaf0.setBackground(new Color(225,225,225));
blaf0.setPreferredSize(new Dimension(45,26));
blaf1.setPreferredSize(new Dimension(40,26));
blaf2.setPreferredSize(new Dimension(40,26));
blaf3.setPreferredSize(new Dimension(40,26));
blaf4.setPreferredSize(new Dimension(40,26));
blaf5.setPreferredSize(new Dimension(40,26));
blaf6.setPreferredSize(new Dimension(40,26));
blaf7.setPreferredSize(new Dimension(40,26));
blaf8.setPreferredSize(new Dimension(40,26));
blaf9.setPreferredSize(new Dimension(40,26));
blafdian.setPreferredSize(new Dimension(40,26));
blafzkh.setPreferredSize(new Dimension(40,26));
blafykh.setPreferredSize(new Dimension(40,26));
blafjia.setPreferredSize(new Dimension(40,26));
blafjian.setPreferredSize(new Dimension(40,26));
blafcheng.setPreferredSize(new Dimension(40,26));
blafchu.setPreferredSize(new Dimension(40,26));
blafkf.setPreferredSize(new Dimension(40,26));
blafpf.setPreferredSize(new Dimension(40,26));
blafqc.setPreferredSize(new Dimension(40,26));
blafht.setPreferredSize(new Dimension(40,26));
blafdy.setBackground(new Color(225,225,225));
blafdy.setPreferredSize(new Dimension(40,50));
blafbz.setBackground(new Color(225,225,225));
blafbz.setPreferredSize(new Dimension(40,26));
// blaf1.registerKeyboardAction()
LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,0,0,1,1,blafbz);
LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,1,0,1,1,blafzkh);
LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,2,0,1,1,blafykh);
LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,3,0,1,1,blafht);
LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,4,0,1,1,blafqc);
LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,0,1,1,1,blaf7);
LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,1,1,1,1,blaf8);
LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,2,1,1,1,blaf9);
LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,3,1,1,1,blafpf);
LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,4,1,1,1,blafkf);
LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,0,2,1,1,blaf4);
LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,1,2,1,1,blaf5);
LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,2,2,1,1,blaf6);
LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,3,2,1,1,blafcheng);
LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,4,2,1,1,blafchu);
LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,0,3,1,1,blaf1);
LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,1,3,1,1,blaf2);
LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,2,3,1,1,blaf3);
LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,3,3,1,1,blafjian);
LayoutUtil.add(p1,GridBagConstraints.BOTH,GridBagConstraints.CENTER,0,0,4,3,1,2,blafdy);
LayoutUtil.add(p1,GridBagConstraints.HORIZONTAL,GridBagConstraints.CENTER,0,0,0,4,2,1,blaf0);
LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,2,4,1,1,blafdian);
LayoutUtil.add(p1,GridBagConstraints.NONE,GridBagConstraints.CENTER,0,0,3,4,1,1,blafjia);
getContentPane().add(p1,BorderLayout.SOUTH);
p2.setBorder(BorderFactory.createLoweredBevelBorder());
p2.setLayout(new GridBagLayout());
txt.setHorizontalAlignment(JTextField.RIGHT);
txt.setEditable(false);
txt.setFont(new Font("font",Font.ITALIC,14));
txta.setFont(new Font("font",Font.ITALIC,14));
txta.setEditable(false);
txta.setLineWrap(true);
LayoutUtil.add(p2,GridBagConstraints.HORIZONTAL,GridBagConstraints.SOUTH,1,1,0,3,1,1,txt);
LayoutUtil.add(p2,GridBagConstraints.HORIZONTAL,GridBagConstraints.SOUTHWEST,1,1,0,0,1,2,scr);
getContentPane().add(p2,BorderLayout.CENTER);
scr.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
MouseListener al = new LookAndMouseListener();
// KeyListener kl = new LookAndKeyListener();
blaf0.addMouseListener(al);
blaf1.addMouseListener(al);
blaf2.addMouseListener(al);
blaf3.addMouseListener(al);
blaf4.addMouseListener(al);
blaf5.addMouseListener(al);
blaf6.addMouseListener(al);
blaf7.addMouseListener(al);
blaf8.addMouseListener(al);
blaf9.addMouseListener(al);
blafzkh.addMouseListener(al);
blafykh.addMouseListener(al);
blafdian.addMouseListener(al);
blafjia.addMouseListener(al);
blafjian.addMouseListener(al);
blafcheng.addMouseListener(al);
blafchu.addMouseListener(al);
blafpf.addMouseListener(al);
blafkf.addMouseListener(al);
blafht.addMouseListener(al);
blafqc.addMouseListener(al);
blafdy.addMouseListener(al);
blafbz.addMouseListener(al);
// blaf0.addKeyListener(kl);
p1.setBackground(new Color(225,225,225));
try{
Image image = ImageIO.read(getClass().getResource("image/tubiao.png"));
this.setIconImage(image);
}catch(Exception e){
}
this.setBounds(400,200,230,290);
this.setTitle("计算器");
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.show();
}
class LookAndMouseListener implements MouseListener{
public void mouseEntered(MouseEvent e){
Object sjy = e.getSource();
if(sjy==blaf0){
s000="image/100.png";
blaf0.setIcon(new ImageIcon(getClass().getResource(s000)));
}
if(sjy==blaf1){
s001="image/101.png";
blaf1.setIcon(new ImageIcon(getClass().getResource(s001)));
}
if(sjy==blaf2){
s002="image/102.png";
blaf2.setIcon(new ImageIcon(getClass().getResource(s002)));
}
if(sjy==blaf3){
s003="image/103.png";
blaf3.setIcon(new ImageIcon(getClass().getResource(s003)));
}
if(sjy==blaf4){
s004="image/104.png";
blaf4.setIcon(new ImageIcon(getClass().getResource(s004)));
}
if(sjy==blaf5){
s005="image/105.png";
blaf5.setIcon(new ImageIcon(getClass().getResource(s005)));
}
if(sjy==blaf6){
s006="image/106.png";
blaf6.setIcon(new ImageIcon(getClass().getResource(s006)));
}
if(sjy==blaf7){
s007="image/107.png";
blaf7.setIcon(new ImageIcon(getClass().getResource(s007)));
}
if(sjy==blaf8){
s008="image/108.png";
blaf8.setIcon(new ImageIcon(getClass().getResource(s008)));
}
if(sjy==blaf9){
s009="image/109.png";
blaf9.setIcon(new ImageIcon(getClass().getResource(s009)));
}
if(sjy==blafdian){
sdian="image/1dian.png";
blafdian.setIcon(new ImageIcon(getClass().getResource(sdian)));
}
if(sjy==blafzkh){
szkh="image/1zkh.png";
blafzkh.setIcon(new ImageIcon(getClass().getResource(szkh)));
}
if(sjy==blafykh){
sykh="image/1ykh.png";
blafykh.setIcon(new ImageIcon(getClass().getResource(sykh)));
}
if(sjy==blafjia){
sjia="image/1jia.png";
blafjia.setIcon(new ImageIcon(getClass().getResource(sjia)));
}
if(sjy==blafjian){
sjian="image/1jian.png";
blafjian.setIcon(new ImageIcon(getClass().getResource(sjian)));
}
if(sjy==blafcheng){
scheng="image/1cheng.png";
blafcheng.setIcon(new ImageIcon(getClass().getResource(scheng)));
}
if(sjy==blafchu){
schu="image/1chu.png";
blafchu.setIcon(new ImageIcon(getClass().getResource(schu)));
}
if(sjy==blafpf){
spf="image/1pf.png";
blafpf.setIcon(new ImageIcon(getClass().getResource(spf)));
}
if(sjy==blafkf){
skf="image/1kf.png";
blafkf.setIcon(new ImageIcon(getClass().getResource(skf)));
}
if(sjy==blafht){
sht="image/1ht.png";
blafht.setIcon(new ImageIcon(getClass().getResource(sht)));
}
if(sjy==blafqc){
sqc="image/1qc.png";
blafqc.setIcon(new ImageIcon(getClass().getResource(sqc)));
}
if(sjy==blafdy){
sdy="image/1dy.png";
blafdy.setIcon(new ImageIcon(getClass().getResource(sdy)));
}
if(sjy==blafbz){
sbz="image/1help.png";
blafbz.setIcon(new ImageIcon(getClass().getResource(sbz)));
}
}
public void mouseExited(MouseEvent e){
Object sjy = e.getSource();
if(sjy == blaf0){
s000="image/000.png";
blaf0.setIcon(new ImageIcon(getClass().getResource(s000)));
}
if(sjy == blaf1){
s001="image/001.png";
blaf1.setIcon(new ImageIcon(getClass().getResource(s001)));
}
if(sjy == blaf2){
s002="image/002.png";
blaf2.setIcon(new ImageIcon(getClass().getResource(s002)));
}
if(sjy == blaf3){
s003="image/003.png";
blaf3.setIcon(new ImageIcon(getClass().getResource(s003)));
}
if(sjy == blaf4){
s004="image/004.png";
blaf4.setIcon(new ImageIcon(getClass().getResource(s004)));
}
if(sjy == blaf5){
s005="image/005.png";
blaf5.setIcon(new ImageIcon(getClass().getResource(s005)));
}
if(sjy == blaf6){
s006="image/006.png";
blaf6.setIcon(new ImageIcon(getClass().getResource(s006)));
}
if(sjy == blaf7){
s007="image/007.png";
blaf7.setIcon(new ImageIcon(getClass().getResource(s007)));
}
if(sjy == blaf8){
s008="image/008.png";
blaf8.setIcon(new ImageIcon(getClass().getResource(s008)));
}
if(sjy == blaf9){
s009="image/009.png";
blaf9.setIcon(new ImageIcon(getClass().getResource(s009)));
}
if(sjy == blafzkh){
szkh="image/zkh.png";
blafzkh.setIcon(new ImageIcon(getClass().getResource(szkh)));
}
if(sjy == blafykh){
sykh="image/ykh.png";
blafykh.setIcon(new ImageIcon(getClass().getResource(sykh)));
}
if(sjy == blafjia){
sjia="image/jia.png";
blafjia.setIcon(new ImageIcon(getClass().getResource(sjia)));
}
if(sjy == blafdian){
sdian="image/dian.png";
blafdian.setIcon(new ImageIcon(getClass().getResource(sdian)));
}
if(sjy == blafjian){
sjian="image/jian.png";
blafjian.setIcon(new ImageIcon(getClass().getResource(sjian)));
}
if(sjy == blafcheng){
scheng="image/cheng.png";
blafcheng.setIcon(new ImageIcon(getClass().getResource(scheng)));
}
if(sjy == blafchu){
schu="image/chu.png";
blafchu.setIcon(new ImageIcon(getClass().getResource(schu)));
}
if(sjy == blafpf){
spf="image/pf.png";
blafpf.setIcon(new ImageIcon(getClass().getResource(spf)));
}
if(sjy == blafkf){
skf="image/kf.png";
blafkf.setIcon(new ImageIcon(getClass().getResource(skf)));
}
if(sjy == blafqc){
sqc="image/qc.png";
blafqc.setIcon(new ImageIcon(getClass().getResource(sqc)));
}
if(sjy == blafht){
sht="image/ht.png";
blafht.setIcon(new ImageIcon(getClass().getResource(sht)));
}
if(sjy == blafdy){
sdy="image/dy.png";
blafdy.setIcon(new ImageIcon(getClass().getResource(sdy)));
}
if(sjy==blafbz){
sbz="image/help.png";
blafbz.setIcon(new ImageIcon(getClass().getResource(sbz)));
}
}
public void mouseReleased(MouseEvent e){
Object sjy = e.getSource();
if(sjy == blafbz){
JOptionPane.showMessageDialog(null,"本计算器暂不支持负号输入\n请合理使用(0-1)来代替\nS字符代表根号 格式为:\n开方次数S被开方数\n可精确到小数点后8位\n使用计算器时如遇bug请敬请见谅\n并希望能与本人联系","帮助",JOptionPane.PLAIN_MESSAGE);
}
if(sjy == blaf0)
shuru("0");
if(sjy == blaf1)
shuru("1");
if(sjy == blaf2)
shuru("2");
if(sjy == blaf3)
shuru("3");
if(sjy == blaf4)
shuru("4");
if(sjy == blaf5)
shuru("5");
if(sjy == blaf6)
shuru("6");
if(sjy == blaf7)
shuru("7");
if(sjy == blaf8)
shuru("8");
if(sjy == blaf9)
shuru("9");
if(sjy == blafzkh)
shuru("(");
if(sjy == blafykh)
shuru(")");
if(sjy == blafjia)
shuru("+");
if(sjy == blafdian)
shuru(".");
if(sjy == blafjian)
shuru("-");
if(sjy == blafcheng)
shuru("*");
if(sjy == blafchu)
shuru("/");
if(sjy == blafpf)
shuru("^");
if(sjy == blafkf)
shuru("S");
if(sjy == blafqc){
dian = true;
dengyu = true;
zkh = true;
txt.setText("0");
if(qc){
txta.append("---------------------------------------\n");
qc = false;
}
}
if(sjy == blafht)
txt.setText(houtui());
if(sjy == blafdy&&khpp;()&&wz;()){
qc = true;
txt.setText(shuchu());
}
else
if(sjy == blafdy)
JOptionPane.showMessageDialog(null,"括号未能匹配\n或输入格式错误","警告",JOptionPane.WARNING_MESSAGE);
}
public void mousePressed(MouseEvent e){
}
public void mouseClicked(MouseEvent e){
}
}
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
}
public void keyReleased(KeyEvent e){
}
public void keyTyped(KeyEvent e){
}
class StackTally{
private int n = 100;
private char tally[] = new char[n];
private int top;
public StackTally(){
tally[0]='#';
top=0;
}
public void TallyPush(char c){
tally[++top]=c;
}
public char TallyPop(){
return tally[top--];
}
public char TallyPopK(){
return tally[top];
}
}
class StackNum{
private int n = 100;
private double num[] = new double[n];
private int top;
public StackNum(){
top=-1;
}
public void NumPush(double f){
top++;
num[top]=f;
}
public double NumPop(){
return num[top--];
}
}
void shuru(String c){
String s;
if(dengyu||c.equals("+")||c.equals("-")||c.equals("*")||c.equals("/")||c.equals("^")||c.equals("S")){
s = txt.getText().trim();
dengyu = true;
}
else{
zkh = true;
dengyu = true;
s = "0";
}
if(s.endsWith("+")||s.endsWith("-")||s.endsWith("*")||s.endsWith("/")||s.endsWith("^")||s.endsWith("S")||s.endsWith("(")){
if(!(c.equals("+")||c.equals("-")||c.equals("*")||c.equals("/")||c.equals("^")||c.equals("S")||c.equals(".")||(s.endsWith("(")&&c.equals(")"))))
s=s.concat(c);
dian = true;
txt.setText(chu0(s));
}
else{
if(!c.equals("(")||zkh){
if(!(s.endsWith(")")&&!(c.equals("+")||c.equals("-")||c.equals("*")||c.equals("/")||c.equals("^")||c.equals("S")||c.equals(".")||s.endsWith("(")||c.equals(")")))&&(dian||!c.equals("."))&&!(s.endsWith("+")||s.endsWith("-")||s.endsWith("*")||s.endsWith("/")||s.endsWith("^")||s.endsWith("S")||s.endsWith("(")))
s=s.concat(c);
zkh = false;
}
if(c.equals("."))
dian = false;
txt.setText(chu0(s));
}
}
String shuchu(){
String s = txt.getText().concat("#");
txta.append(s.replace("#","="));
pn.NumPush(NumFen(s.toCharArray()));
String s1 =""+YunSuan(s.toCharArray());
dengyu = false;
p = 0;
i_ = false;
dian = true;
if(s1.endsWith(".0"))
s1=s1.replace(".0","");
txta.append(s1+'\n');
return s1;
}
double NumFen(char c[]){
double n = 0;
double m = 1;
while(c[p]>='0'&&c[p]<='9'){
n=n*10+(c[p]-'0');
p++;
}
if(c[p]=='.'){
p++;
while(c[p]>='0'&&c[p]<='9'){
m*=0.1;
n=n+(m*(c[p]-'0'));
p++;
}
}
return n;
}
double YunSuan(char c[]){
while(!(c[p]=='#'&&pt;.TallyPopK()=='#')){
if(BiJiao(pt.TallyPopK(),c[p])==1){
pt.TallyPush(c[p]);
p++;
if(c[p]>='0'&&c[p]<='9')
pn.NumPush(NumFen(c));
}
else
pn.NumPush(JiSua());
}
return pn.NumPop();
}
int BiJiao(char m,char n){
int z1,z2;
i_ = true;
z1=Zhuan(m);
i_ = false;
z2=Zhuan(n);
if(z1