[ Java实例 ] 1 - 简易版计算器
文件结构:
运行结果:
代码:(复制可用)
(如果要用请在留言区留言说一下,谢谢!!)
package App;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ButtonGroup;
import javax.swing.SwingConstants;
import java.awt.GridLayout;
import java.awt.Dimension;
public class MainFrame extends JFrame {
private static final long serialVersionUID = 3272128285111488657L;
private JPanel contentPane;
private final ButtonGroup buttonDigit = new ButtonGroup();
private final ButtonGroup buttonControl = new ButtonGroup();
private final ButtonGroup buttonGroup = new ButtonGroup();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MainFrame() {
this.setTitle("杨北林的简易计算器");
this.setSize(450,480);
this.setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 355, 411);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JLabel lbresult;
contentPane.setLayout(new GridLayout(0, 1, 0, 0));
JPanel panel_text = new JPanel();
contentPane.add(panel_text);
panel_text.setLayout(new GridLayout(0, 1, 0, 0));
JLabel lbshow = new JLabel("");
panel_text.add(lbshow);
lbshow.setHorizontalTextPosition(SwingConstants.CENTER);
lbshow.setFont(new Font("新宋体", Font.PLAIN, 32));
lbresult = new JLabel("0");
panel_text.add(lbresult);
lbresult.setVerticalAlignment(SwingConstants.TOP);
lbresult.setMaximumSize(new Dimension(32, 0));
lbresult.setHorizontalAlignment(SwingConstants.LEFT);
lbresult.setHorizontalTextPosition(SwingConstants.CENTER);
lbresult.setFont(new Font("Adobe Garamond Pro Bold", Font.PLAIN, 42));
InputDigit input = new InputDigit(lbresult);
JPanel panel_button = new JPanel();
contentPane.add(panel_button);
panel_button.setLayout(new GridLayout(0, 4, 0, 0));
JButton btnrem = new JButton("%");
buttonGroup.add(btnrem);
btnrem.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//Double a = Double.parseDouble(lbresult.getText().substring(1));
//a = a / 100.0;
//System.out.print(a);
Float a = Float.parseFloat(lbresult.getText().substring(1));
lbshow.setText(Float.toString(a));
if(a<100) {
lbresult.setText("="+a/100.0);
}else
lbresult.setText("="+Double.toString(a/100.0));
}
});
btnrem.setHorizontalTextPosition(SwingConstants.CENTER);
panel_button.add(btnrem);
JButton btnclearall = new JButton("CE");
buttonControl.add(btnclearall);
btnclearall.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
lbshow.setText("");
lbresult.setText("=");
}
});
btnclearall.setHorizontalTextPosition(SwingConstants.CENTER);
panel_button.add(btnclearall);
JButton btnclear = new JButton("C");
buttonControl.add(btnclear);
btnclear.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
lbresult.setText("=");
}
});
btnclear.setHorizontalTextPosition(SwingConstants.CENTER);
panel_button.add(btnclear);
JButton btndel = new JButton("del");
buttonControl.add(btndel);
btndel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String a = String.valueOf(lbresult.getText());
//Integer a = Integer.parseInt(lbresult.getText());
//System.out.print(a.substring(1,a.length()-1));
//System.out.print(a.length());
//System.out.print(lbshow.getText());
if(lbshow.getText()!=null) {
lbshow.setText(null);
}else if(a.length()>=2){
// System.out.print(a.length());
lbresult.setText("="+a.substring(1,a.length()-1));
}
}
});
btndel.setHorizontalTextPosition(SwingConstants.CENTER);
panel_button.add(btndel);
JButton btnradical = new JButton("\u221Ax");
btnradical.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Float a = Float.parseFloat(lbresult.getText().substring(1));
lbresult.setText("="+Math.sqrt(a));
lbshow.setText("√("+a+")");
}
});
JButton btnx = new JButton("1/x");
buttonGroup.add(btnx);
btnx.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Float a = Float.parseFloat(lbresult.getText().substring(1));
//Double a = Double.parseDouble(lbresult.getText().substring(1));
float a1 = 1/(float)a;
lbresult.setText("="+a1);
lbshow.setText("1/("+a+")");
}
});
btnx.setHorizontalTextPosition(SwingConstants.CENTER);
panel_button.add(btnx);
JButton btnsquare = new JButton("X\u00B2");
buttonGroup.add(btnsquare);
btnsquare.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Float a = Float.parseFloat(lbresult.getText().substring(1));
lbresult.setText("="+a*a);
lbshow.setText("sqr("+a+")");
}
});
btnsquare.setHorizontalTextPosition(SwingConstants.CENTER);
panel_button.add(btnsquare);
buttonGroup.add(btnradical);
btnradical.setHorizontalTextPosition(SwingConstants.CENTER);
panel_button.add(btnradical);
JButton btn7 = new JButton("7");
buttonDigit.add(btn7);
btn7.setHorizontalTextPosition(SwingConstants.CENTER);
btn7.addMouseListener(input);
JButton btndiv = new JButton("/");
buttonGroup.add(btndiv);
btndiv.setHorizontalTextPosition(SwingConstants.CENTER);
btndiv.addMouseListener(input);
panel_button.add(btndiv);
panel_button.add(btn7);
JButton btn8 = new JButton("8");
buttonDigit.add(btn8);
btn8.setHorizontalTextPosition(SwingConstants.CENTER);
btn8.addMouseListener(input);
panel_button.add(btn8);
JButton btn9 = new JButton("9");
btn9.addMouseListener(input);
buttonDigit.add(btn9);
btn9.setHorizontalTextPosition(SwingConstants.CENTER);
panel_button.add(btn9);
JButton btnmul = new JButton("*");
buttonGroup.add(btnmul);
btnmul.setHorizontalTextPosition(SwingConstants.CENTER);
btnmul.addMouseListener(input);
panel_button.add(btnmul);
JButton btn4 = new JButton("4");
buttonDigit.add(btn4);
btn4.setHorizontalTextPosition(SwingConstants.CENTER);
btn4.addMouseListener(input);
panel_button.add(btn4);
JButton btn5 = new JButton("5");
buttonDigit.add(btn5);
btn5.setHorizontalTextPosition(SwingConstants.CENTER);
btn5.addMouseListener(input);
panel_button.add(btn5);
JButton btn6 = new JButton("6");
buttonDigit.add(btn6);
btn6.setHorizontalTextPosition(SwingConstants.CENTER);
btn6.addMouseListener(input);
panel_button.add(btn6);
JButton btnsum = new JButton("+");
buttonGroup.add(btnsum);
btnsum.setHorizontalTextPosition(SwingConstants.CENTER);
btnsum.addMouseListener(input);
panel_button.add(btnsum);
JButton btn1 = new JButton("1");
buttonDigit.add(btn1);
btn1.setHorizontalTextPosition(SwingConstants.CENTER);
btn1.addMouseListener(input);
panel_button.add(btn1);
JButton btn2 = new JButton("2");
buttonDigit.add(btn2);
btn2.setHorizontalTextPosition(SwingConstants.CENTER);
btn2.addMouseListener(input);
panel_button.add(btn2);
JButton btn3 = new JButton("3");
buttonDigit.add(btn3);
btn3.setHorizontalTextPosition(SwingConstants.CENTER);
btn3.addMouseListener(input);
panel_button.add(btn3);
JButton btnsub = new JButton("-");
buttonGroup.add(btnsub);
btnsub.setHorizontalTextPosition(SwingConstants.CENTER);
btnsub.addMouseListener(input);
panel_button.add(btnsub);
JButton btncontrary = new JButton("->+");
buttonGroup.add(btncontrary);
btncontrary.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Float a = Float.parseFloat(lbresult.getText().substring(1));
//System.out.print(-a);
if(a<0)
lbresult.setText("="+Float.toString(-a));
else
lbresult.setText("="+Float.toString(a));
}
});
btncontrary.setHorizontalTextPosition(SwingConstants.CENTER);
panel_button.add(btncontrary);
JButton btn0 = new JButton("0");
buttonDigit.add(btn0);
btn0.setHorizontalTextPosition(SwingConstants.CENTER);
btn0.addMouseListener(input);
panel_button.add(btn0);
JButton btnpoint = new JButton(".");
buttonGroup.add(btnpoint);
btnpoint.setHorizontalTextPosition(SwingConstants.CENTER);
btnpoint.addMouseListener(input);
panel_button.add(btnpoint);
JButton btnresult = new JButton("=");
buttonGroup.add(btnresult);
btnresult.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String a = String.valueOf(lbresult.getText());
//System.out.println(a);
String x="",y="";
int l=0;
float result=0;
char operator='\0';
if(a.charAt(1)=='-') {
x="-";
}else {
x = x + a.charAt(1);
}
for(int i=2;i<a.length();i++) {
char ch = a.charAt(i);
if(((ch>='0'&&ch<='9')||ch=='.')&&l==0) {
x = x + ch;
}else if(operator=='\0'){
l=1;
operator=ch;
}
if(((ch>='0'&&ch<='9')||ch=='.')&&l==1) {
y = y + ch;
}
}
/*System.out.print("运算符"+operator+'\n');
System.out.print("x="+x+"y="+y);
//Integer.parseInt(x);
/*System.out.print("x="+x+"\n");
System.out.print("运算符"+operator+'\n');
System.out.print("y="+y);*/
switch(operator) {
case '+':result=Float.parseFloat(x)+Float.parseFloat(y);break;
case '-':result=Float.parseFloat(x)-Float.parseFloat(y);break;
case '*':result=Float.parseFloat(x)*Float.parseFloat(y);break;
case '/':result = Float.parseFloat(x)/Float.parseFloat(y);
}
//System.out.print("result="+result+"\n");
lbshow.setText(x+operator+y);
lbresult.setText("="+Float.toString(result));
}
});
btnresult.setHorizontalTextPosition(SwingConstants.CENTER);
panel_button.add(btnresult);
}
class InputDigit extends MouseAdapter{
JLabel lb;
public InputDigit(JLabel lb) {
lb.setText("=");
this.lb=lb;
}
public void mouseClicked(MouseEvent e) {
JButton bt = (JButton) e.getSource();
lb.setText(lb.getText()+bt.getText());
}
}
}