面向对象编程思维学习——计算器

package calc_new;


import java.awt.BorderLayout;
import java.awt.EventQueue;


import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JComboBox;
import java.awt.Button;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.TextField;
import java.awt.TextArea;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseWheelListener;
import java.awt.event.MouseWheelEvent;
import java.awt.Font;
import java.awt.Color;
import java.awt.SystemColor;
import javax.swing.UIManager;
import java.awt.Dialog.ModalExclusionType;


public class Calc_1 extends JFrame {


String symbol = null;
private JPanel contentPane;
float var_typing = 0;
float var_typed = 0;
int jtextLines = 5;
boolean cal_finish = false;


/**
* Launch the application.
*/


public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Calc_1 frame = new Calc_1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}


/**
* Create the frame.
*/
public Calc_1() {
setResizable(false);
setTitle("by yangjingqi");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(500, 100, 250, 500);
contentPane = new JPanel();
contentPane.setBackground(UIManager.getColor("Button.background"));



contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

TextField textField = new TextField();
textField.setForeground(Color.BLACK);
textField.setFont(new Font("Arial", Font.PLAIN, 25));
textField.setBounds(10, 10, 218, 54);
contentPane.add(textField);

JTextArea textArea = new JTextArea();
textArea.setBounds(10, 350, 218, 103);
textArea.setLineWrap(true);
contentPane.add(textArea);

contentPane.addMouseWheelListener(new MouseWheelListener() {
public void mouseWheelMoved(MouseWheelEvent arg0) {
textArea.setText("var_typed: "+var_typed + "\n");
textArea.append("var_typing: "+var_typing+"\n");
textArea.append("Power by yangjingqi\n");


}
});

Button button = new Button("CE");
button.setBackground(UIManager.getColor("Button.background"));
button.setForeground(UIManager.getColor("CheckBox.foreground"));
button.setFont(new Font("Arial", Font.PLAIN, 15));
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
button.setBounds(10, 70, 50, 50);
contentPane.add(button);

Button button_1 = new Button("C");
button_1.setBackground(UIManager.getColor("Button.background"));
button_1.setForeground(UIManager.getColor("CheckBox.foreground"));
button_1.setFont(new Font("Arial", Font.PLAIN, 15));
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("0");
var_typing *= 0;
var_typed *= 0;
cal_finish = true;
}
});
button_1.setBounds(66, 70, 50, 50);
contentPane.add(button_1);

Button button_2 = new Button("Del");
button_2.setBackground(UIManager.getColor("Button.background"));
button_2.setForeground(UIManager.getColor("CheckBox.foreground"));
button_2.setFont(new Font("Arial", Font.PLAIN, 15));
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(""+(int)Integer.parseInt(textField.getText())/10);
if(cal_finish == false)
{
var_typing = (int)(var_typing/=10) ;
}
else
{
var_typed = (int)(var_typed/=10);
}
}
});
button_2.setBounds(122, 70, 50, 50);
contentPane.add(button_2);

Button button_3 = new Button("/");
button_3.setBackground(UIManager.getColor("Button.background"));
button_3.setForeground(UIManager.getColor("CheckBox.foreground"));
button_3.setFont(new Font("Arial", Font.PLAIN, 15));
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
symbol = "/";
if(cal_finish == false)
var_typed = var_typing;
var_typing = 0;
textField.setText("");
}
});
button_3.setBounds(178, 70, 50, 50);
contentPane.add(button_3);

Button button_4 = new Button("7");
button_4.setBackground(UIManager.getColor("Button.background"));
button_4.setForeground(UIManager.getColor("CheckBox.foreground"));
button_4.setFont(new Font("Arial", Font.PLAIN, 15));
button_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(cal_finish == false)
textField.setText(textField.getText()+"7");
else
{
cal_finish = false;
var_typing = 0;
textField.setText("");
textField.setText(textField.getText()+"7");
}
var_typing *= 10;
var_typing += 7;
}
});
button_4.setBounds(10, 126, 50, 50);
contentPane.add(button_4);

Button button_5 = new Button("8");
button_5.setBackground(UIManager.getColor("Button.background"));
button_5.setForeground(UIManager.getColor("CheckBox.foreground"));
button_5.setFont(new Font("Arial", Font.PLAIN, 15));
button_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(cal_finish == false)
textField.setText(textField.getText()+"8");
else
{
cal_finish = false;
var_typing = 0;
textField.setText("");
textField.setText(textField.getText()+"8");
}
var_typing *= 10;
var_typing += 8;
}
});
button_5.setBounds(66, 126, 50, 50);
contentPane.add(button_5);

Button button_6 = new Button("9");
button_6.setBackground(UIManager.getColor("Button.background"));
button_6.setForeground(UIManager.getColor("CheckBox.foreground"));
button_6.setFont(new Font("Arial", Font.PLAIN, 15));
button_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(cal_finish == false)
textField.setText(textField.getText()+"9");
else
{
cal_finish = false;
var_typing = 0;
textField.setText("");
textField.setText(textField.getText()+"9");
}
var_typing *= 10;
var_typing += 9;
}
});
button_6.setBounds(122, 126, 50, 50);
contentPane.add(button_6);

Button button_7 = new Button("\u00D7");
button_7.setBackground(UIManager.getColor("Button.background"));
button_7.setForeground(UIManager.getColor("CheckBox.foreground"));
button_7.setFont(new Font("Arial", Font.PLAIN, 15));
button_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
symbol = "x";
if(cal_finish == false)
var_typed = var_typing;
var_typing = 0;
textField.setText("");
}
});
button_7.setBounds(178, 126, 50, 50);
contentPane.add(button_7);

Button button_8 = new Button("4");
button_8.setBackground(UIManager.getColor("Button.background"));
button_8.setForeground(UIManager.getColor("CheckBox.foreground"));
button_8.setFont(new Font("Arial", Font.PLAIN, 15));
button_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(cal_finish == false)
textField.setText(textField.getText()+"4");
else
{
cal_finish = false;
var_typing = 0;
textField.setText("");
textField.setText(textField.getText()+"4");
}
var_typing *= 10;
var_typing += 4;
}
});
button_8.setBounds(10, 182, 50, 50);
contentPane.add(button_8);

Button button_9 = new Button("5");
button_9.setBackground(UIManager.getColor("Button.background"));
button_9.setForeground(UIManager.getColor("CheckBox.foreground"));
button_9.setFont(new Font("Arial", Font.PLAIN, 15));
button_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(cal_finish == false)
textField.setText(textField.getText()+"5");
else
{
cal_finish = false;
var_typing = 0;
textField.setText("");
textField.setText(textField.getText()+"5");
}
var_typing *= 10;
var_typing += 5;
}
});
button_9.setBounds(66, 182, 50, 50);
contentPane.add(button_9);

Button button_10 = new Button("6");
button_10.setBackground(UIManager.getColor("Button.background"));
button_10.setForeground(UIManager.getColor("CheckBox.foreground"));
button_10.setFont(new Font("Arial", Font.PLAIN, 15));
button_10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(cal_finish == false)
textField.setText(textField.getText()+"6");
else
{
cal_finish = false;
var_typing = 0;
textField.setText("");
textField.setText(textField.getText()+"6");
}
var_typing *= 10;
var_typing += 6;
}
});
button_10.setBounds(122, 182, 50, 50);
contentPane.add(button_10);

Button button_11 = new Button("-");
button_11.setBackground(UIManager.getColor("Button.background"));
button_11.setForeground(UIManager.getColor("CheckBox.foreground"));
button_11.setFont(new Font("Arial", Font.PLAIN, 15));
button_11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
symbol = "-";
if(cal_finish == false)
var_typed = var_typing;
var_typing = 0;
textField.setText("");


}
});
button_11.setBounds(178, 182, 50, 50);
contentPane.add(button_11);

Button button_12 = new Button("1");
button_12.setBackground(UIManager.getColor("Button.background"));
button_12.setForeground(UIManager.getColor("CheckBox.foreground"));
button_12.setFont(new Font("Arial", Font.PLAIN, 15));
button_12.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(cal_finish == false)
textField.setText(textField.getText()+"1");
else
{
cal_finish = false;
var_typing = 0;
textField.setText("");
textField.setText(textField.getText()+"1");
}
var_typing *= 10;
var_typing += 1;
}
});
button_12.setBounds(10, 238, 50, 50);
contentPane.add(button_12);

Button button_13 = new Button("2");
button_13.setBackground(UIManager.getColor("Button.background"));
button_13.setForeground(UIManager.getColor("CheckBox.foreground"));
button_13.setFont(new Font("Arial", Font.PLAIN, 15));
button_13.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(cal_finish == false)
textField.setText(textField.getText()+"2");
else
{
cal_finish = false;
var_typing = 0;
textField.setText("");
textField.setText(textField.getText()+"2");
}
var_typing *= 10;
var_typing += 2;
if(var_typing == 222222.0)
{
textField.setText("HI Baby!\n");
var_typing = 0;
cal_finish = true;
}
}
});
button_13.setBounds(66, 238, 50, 50);
contentPane.add(button_13);

Button button_14 = new Button("3");
button_14.setBackground(UIManager.getColor("Button.background"));
button_14.setForeground(UIManager.getColor("CheckBox.foreground"));
button_14.setFont(new Font("Arial", Font.PLAIN, 15));
button_14.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(cal_finish == false)
textField.setText(textField.getText()+"3");
else
{
cal_finish = false;
var_typing = 0;
textField.setText("");
textField.setText(textField.getText()+"3");
}
var_typing *= 10;
var_typing += 3;
}
});
button_14.setBounds(122, 238, 50, 50);
contentPane.add(button_14);

Button button_15 = new Button("+");
button_15.setBackground(UIManager.getColor("Button.background"));
button_15.setForeground(UIManager.getColor("CheckBox.foreground"));
button_15.setFont(new Font("Arial", Font.PLAIN, 15));
button_15.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
symbol = "+";
if(cal_finish == false)
var_typed = var_typing;
var_typing = 0;
textField.setText("");
}
});
button_15.setBounds(178, 238, 50, 50);
contentPane.add(button_15);

Button button_16 = new Button("\u00B1");
button_16.setBackground(UIManager.getColor("Button.background"));
button_16.setForeground(UIManager.getColor("CheckBox.foreground"));
button_16.setFont(new Font("Arial", Font.PLAIN, 15));
button_16.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(cal_finish == false)
{
var_typing *= -1;
if(var_typing < 0)
textField.setText("-"+ textField.getText());
else
textField.setText(""+var_typing);
}
else
{
var_typed *= -1;
if(var_typed < 0)
textField.setText("-"+ textField.getText());
else
textField.setText(""+var_typed);
}


}
});
button_16.setBounds(10, 294, 50, 50);
contentPane.add(button_16);

Button button_17 = new Button("0");
button_17.setBackground(UIManager.getColor("Button.background"));
button_17.setForeground(UIManager.getColor("CheckBox.foreground"));
button_17.setFont(new Font("Arial", Font.PLAIN, 15));
button_17.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(cal_finish == false)
textField.setText(textField.getText()+"0");
else
{
cal_finish = false;
var_typing = 0;
textField.setText("");
textField.setText(textField.getText()+"0");
}
var_typing *= 10;
var_typing += 0;
}
});
button_17.setBounds(66, 294, 50, 50);
contentPane.add(button_17);

Button button_18 = new Button(".");
button_18.setBackground(UIManager.getColor("Button.background"));
button_18.setForeground(UIManager.getColor("CheckBox.foreground"));
button_18.setFont(new Font("Arial", Font.PLAIN, 15));
button_18.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText()+".");
}
});
button_18.setBounds(122, 294, 50, 50);
contentPane.add(button_18);

Button button_19 = new Button("=");
button_19.setBackground(UIManager.getColor("Button.background"));
button_19.setForeground(UIManager.getColor("CheckBox.foreground"));
button_19.setFont(new Font("Arial", Font.PLAIN, 15));
button_19.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
switch(symbol)
{
case "+": var_typed += var_typing;break;
case "-": var_typed -= var_typing;break;
case "x": var_typed *= var_typing;break;
case "/": var_typed /= var_typing;break;
}
/*用与整数判定,若是则直接以整形方式显示*/
if(var_typed%1 == 0)
textField.setText(""+(int)var_typed);
else
textField.setText(""+var_typed);
cal_finish = true;

}
});
button_19.setBounds(178, 294, 50, 50);
contentPane.add(button_19);


}

}
阅读更多 登录后自动展开
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页