public class CalViews extends JFrame {
private JPanel contentPane;
private JTextField textField_2;
private JTextField textField;
private String t1=null;
private String t3;
private double n1=0;
private JMenuBar menuBar;
private JMenu menu;
private JButton button_1;
private JButton button_3;
private JButton button_2;
private JButton button_4;
private JButton button_5;
private JButton button_6;
private JButton button_7;
private JButton button_8;
private JButton button_9;
private JButton button_10;
private JButton button_11;
private JButton button_12;
private JButton btnC;
private JButton button_17;
private JButton btnSin;
private JButton button_18;
private String strbg="";
private JButton btnPow;
private JButton button_19;
private JButton btnLg;
private JButton btnNewButton;
private JButton button_20;
private JButton button_21;
private JButton button_22;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(NimbusLookAndFeel());
} catch (UnsupportedLookAndFeelException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CalViews frame = new CalViews();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private static LookAndFeel NimbusLookAndFeel() {
// TODO Auto-generated method stub
return null;
}
/**
* Create the frame.
*/
public CalViews() {
setForeground(new Color(204, 255, 255));
setBackground(new Color(204, 255, 255));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 478, 333);
menuBar = new JMenuBar();
menuBar.setBackground(new Color(204, 255, 255));
menuBar.setForeground(Color.BLUE);
setJMenuBar(menuBar);
menu = new JMenu("帮助");
menu.setBackground(new Color(204, 255, 255));
menuBar.add(menu);
button_1 = new JButton("关于");
button_1.setBackground(new Color(204, 255, 255));
button_1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(CalViews.this, "此软件由浮冰制作,版权所有哦!(*^__^*) 嘻嘻……", "计算器", JOptionPane.INFORMATION_MESSAGE, null);
}
});
menu.add(button_1);
contentPane = new JPanel();
contentPane.setBackground(new Color(204, 255, 255));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
ButtonGroup group=new ButtonGroup();
textField = new JTextField();
textField.setBackground(new Color(255, 153, 255));
textField.setFont(new Font("Yu Gothic Light", Font.BOLD | Font.ITALIC, 18));
textField.setEditable(false);
textField.setColumns(10);
JButton button = new JButton("计算");
button.setBackground(new Color(204, 255, 255));
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
ScriptEngine se = new ScriptEngineManager().getEngineByName("JavaScript");
//t1=textField.getText();
t1=strbg;
try {
Object object=se.eval(t1);
t1=object.toString();
} catch (ScriptException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
n1=Double.parseDouble(t1);
n1=Math.pow(10,14)*n1;
n1=Math.round(n1);
n1=n1/Math.pow(10,14);
if(n1 % 1.0==0){
Long l=(long) n1;
t3=String.valueOf(l);
textField_2.setText(t3);
}else{
t3=String.valueOf(n1);
textField_2.setText(t3);
}
}
});
//textField_1.addKeyListener(new KeyAdapter(){
//
//@Override
//public void keyPressed(KeyEvent e) {
//// TODO Auto-generated method stub
//textField_1.setText(textField_1.getText().replaceAll("[^-/qwer]", ""));
//};
//});
textField_2 = new JTextField();
textField_2.setBackground(new Color(255, 153, 255));
textField_2.setFont(new Font("Yu Gothic Light", Font.BOLD | Font.ITALIC, 18));
textField_2.setEditable(false);
textField_2.setHorizontalAlignment(SwingConstants.LEFT);
textField_2.setColumns(10);
button_2 = new JButton("1");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
button_2.setBackground(new Color(204, 255, 255));
button_2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
textField.setText(textField.getText()+1);
strbg=strbg+"1";
}
});
button_3 = new JButton("2");
button_3.setBackground(new Color(204, 255, 255));
button_3.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
textField.setText(textField.getText()+2);
strbg=strbg+"2";
}
});
button_4 = new JButton("3");
button_4.setBackground(new Color(204, 255, 255));
button_4.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
textField.setText(textField.getText()+3);
strbg=strbg+"3";
}
});
button_5 = new JButton("4");
button_5.setBackground(new Color(204, 255, 255));
button_5.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
textField.setText(textField.getText()+4);
strbg=strbg+"4";
}
});
button_6 = new JButton("5");
button_6.setBackground(new Color(204, 255, 255));
button_6.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
textField.setText(textField.getText()+5);
strbg=strbg+"5";
}
});
button_7 = new JButton("6");
button_7.setBackground(new Color(204, 255, 255));
button_7.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
textField.setText(textField.getText()+6);
strbg=strbg+"6";
}
});
button_8 = new JButton("7");
button_8.setBackground(new Color(204, 255, 255));
button_8.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
textField.setText(textField.getText()+7);
strbg=strbg+"7";
}
});
button_9 = new JButton("8");
button_9.setBackground(new Color(204, 255, 255));
button_9.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
textField.setText(textField.getText()+8);
strbg=strbg+"8";
}
});
button_10 = new JButton("9");
button_10.setBackground(new Color(204, 255, 255));
button_10.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
textField.setText(textField.getText()+9);
strbg=strbg+"9";
}
});
button_11 = new JButton("0");
button_11.setBackground(new Color(204, 255, 255));
button_11.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
textField.setText(textField.getText()+0);
strbg=strbg+"0";
}
});
button_12 = new JButton(".");
button_12.setBackground(new Color(204, 255, 255));
button_12.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
textField.setText(textField.getText() + ".");
strbg=strbg+".";
}
});