ADD file via upload

main
pcwo73qhm 1 year ago
parent 1a453e0924
commit 2d9d9abb63

@ -0,0 +1,165 @@
import javax.swing.*;
import java.awt.*;
public class CalculatorWindow extends JFrame {
Computer computer;
JButton numberButtons[];
JButton operatorButton[];
//加减乘除按钮
JButton dot,fu,back,equation,clear,sqrt,reciprocal;
JTextField resultShow;
//显示计算结果
JTextField showTempResult;
//临时结果
JLabel showOperator;
//运算符
JTextField showDataItem;
//当前参与运算的数据项
JTextArea computerProcess;
//计算步骤
JButton saveBtn,copyBtn,clearTextBtn;
public CalculatorWindow() throws HeadlessException {
computer=new Computer();
initView();//设置界面
initActionListener();//注册监视器
}
public void initView(){
this.setTitle("计算器");
JPanel left,right;
resultShow=new JTextField(10);
resultShow.setHorizontalAlignment(JTextField.LEFT);
resultShow.setForeground(Color.blue);
resultShow.setFont(new Font("TimesRoman",Font.BOLD,14));
resultShow.setEditable(false);
resultShow.setBackground(Color.lightGray);
showTempResult=new JTextField();
showTempResult.setHorizontalAlignment(JTextField.RIGHT);
showTempResult.setFont(new Font("Arial",Font.BOLD,14));
showTempResult.setEditable(false);
showOperator=new JLabel();
showOperator.setHorizontalAlignment(JTextField.CENTER);
showOperator.setFont(new Font("Arial",Font.BOLD,18));
showDataItem=new JTextField();
showDataItem.setHorizontalAlignment(JTextField.LEFT);
showDataItem.setBackground(Color.white);
showDataItem.setFont(new Font("Arial",Font.BOLD,14));
showDataItem.setEditable(false);
computerProcess=new JTextArea();
computerProcess.setEditable(false);
computerProcess.setFont(new Font("宋体",Font.PLAIN,14));
numberButtons=new JButton[10];
for (int i = 0; i <=9; i++) {
numberButtons[i]=new JButton(" "+i);
numberButtons[i].setFont(new Font("Arial",Font.BOLD,20));
}
operatorButton=new JButton[4];
String s[]={"+","-","*","/"};
for (int i = 0; i < 4; i++) {
operatorButton[i]=new JButton(s[i]);
operatorButton[i].setFont(new Font("Arial",Font.BOLD,20));
}
dot=new JButton(".");
fu=new JButton("+/-");
equation=new JButton("=");
back=new JButton("退格");
clear=new JButton("C");
sqrt=new JButton("sqrt");
reciprocal=new JButton("1/x");
saveBtn=new JButton("保存");
copyBtn=new JButton("复制");
clearTextBtn=new JButton("清除");
left=new JPanel();
right=new JPanel();
//左边布局
left.setLayout(new BorderLayout());
//BorderLayout 容器分为五个部分(东南西北中),每部分只能包含一个组件
JPanel centerInLeft=new JPanel();
Box box=Box.createHorizontalBox();
//创建水平排列组件的Box
box.add(showTempResult);
box.add(showOperator);
box.add(showDataItem);
left.add(box,BorderLayout.NORTH);
left.add(centerInLeft,BorderLayout.CENTER);
centerInLeft.setLayout(new GridLayout(5,5));
//GridLayout 网格布局5行5列
centerInLeft.add(numberButtons[1]);
centerInLeft.add(numberButtons[2]);
centerInLeft.add(numberButtons[3]);
centerInLeft.add(operatorButton[0]);
centerInLeft.add(back);
centerInLeft.add(numberButtons[4]);
centerInLeft.add(numberButtons[5]);
centerInLeft.add(numberButtons[6]);
centerInLeft.add(operatorButton[1]);
centerInLeft.add(clear);
centerInLeft.add(numberButtons[7]);
centerInLeft.add(numberButtons[8]);
centerInLeft.add(numberButtons[9]);
centerInLeft.add(operatorButton[2]);
centerInLeft.add(reciprocal);
centerInLeft.add(numberButtons[0]);
centerInLeft.add(fu);
centerInLeft.add(dot);
centerInLeft.add(operatorButton[3]);
centerInLeft.add(sqrt);
for (int i = 0; i < 4; i++) {
centerInLeft.add(new JLabel());
}
centerInLeft.add(equation);
//右边布局
right.setLayout(new BorderLayout());
right.add(resultShow,BorderLayout.NORTH);
right.add(new JScrollPane(computerProcess),BorderLayout.CENTER);
JPanel southInRight=new JPanel();
southInRight.add(saveBtn);
southInRight.add(copyBtn);
southInRight.add(clearTextBtn);
right.add(southInRight,BorderLayout.SOUTH);
JSplitPane split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,left,right);
//split 左右布局的分割面板
add(split,BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置 点击关闭退出程序
setVisible(true);
setBounds(200,200,788,258);//设置位置大小
validate();
}
public void initActionListener(){
HandleDight hd=new HandleDight(this);
for (int i = 0; i <= 9; i++) {
numberButtons[i].addActionListener(hd);
}
dot.addActionListener(hd);
HandleOperator ho=new HandleOperator(this);
for (int i = 0; i < 4; i++) {
operatorButton[i].addActionListener(ho);
}
fu.addActionListener(new HandleFu(this));
sqrt.addActionListener(new HandleSqrt(this));
reciprocal.addActionListener(new HandleReciprocal(this));
back.addActionListener(new HandleBack(this));
equation.addActionListener(new HandleEquation(this));
clear.addActionListener(new HandleClear(this));
HandleFile hf=new HandleFile(this);
saveBtn.addActionListener(hf);
copyBtn.addActionListener(hf);
clearTextBtn.addActionListener(hf);
}
}
Loading…
Cancel
Save