📄 cal.java
字号:
package frame;
import java.awt.*;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Cal extends JFrame {
String title;
int with;
int length;
Model model;
boolean clear;
JTextField txt=new JTextField(20);
JMenuBar mb=new JMenuBar();
Cal(String title,int with,int length,Model model){
super(title);
this.model=model;
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(with,length);
this.add(txt,BorderLayout.NORTH);
initButton();
initMenu();
this.pack();
}
public Model getModel() {
return model;
}
public String getTxt() {
return txt.getText();
}
public void clearTxt() {
txt.setText("");
}
public void setTxt(String txt) {
StringBuffer sbf=new StringBuffer(this.txt.getText());
if(this.clear==false)
{sbf.append(txt);
this.txt.setText(sbf.toString());
}
else{
this.txt.setText(txt);
this.clear=false;
}
}
private void initMenu(){
JMenu mb1=new JMenu("编辑");
JMenu mb2=new JMenu("查看");
JMenu mb3=new JMenu("帮助");
mb.add(mb1);
mb.add(mb2);
mb.add(mb3);
this.setJMenuBar(mb);
}
private void initButton(){
String a []={"0","1","2","3","4","5","6","7","8","9"};
JPanel cpa=new JPanel(new GridLayout(0,4));
ButtonListener b=new ButtonListener(this,model);
b.registerModel(model);
JButton MC=new JButton("MC");
MC.addActionListener(b);
cpa.add(MC);
JButton MR=new JButton("MR");
MR.addActionListener(b);
cpa.add(MR);
JButton MS=new JButton("MS");
cpa.add(MS);
MS.addActionListener(b);
JButton M=new JButton("M+");
M.addActionListener(b);
cpa.add(M);
for(int i=0;i<10;i++)
{
JButton d=new JButton(a[i]);
cpa.add(d);
d.addActionListener(b);
}
JButton E=new JButton("=");
JButton P=new JButton("+");
JButton MI=new JButton("-");
JButton CH=new JButton("*");
JButton S=new JButton("/");
JButton n=new JButton(".");
JButton zf=new JButton("+/-");
JButton sqrt=new JButton("sqrt");
JButton x=new JButton("1/x");
JButton bai=new JButton("%");
JButton Back=new JButton("Backspace");
JButton CE=new JButton("CE");
JButton C=new JButton("C");
E.addActionListener(b);
P.addActionListener(b);
MI.addActionListener(b);
CH.addActionListener(b);
S.addActionListener(b);
Back.addActionListener(b);
x.addActionListener(b);
bai.addActionListener(b);
CE.addActionListener(b);
C.addActionListener(b);
sqrt.addActionListener(b);
zf.addActionListener(b);
n.addActionListener(b);
cpa.add(E);
cpa.add(P);
cpa.add(MI);
cpa.add(CH);
cpa.add(S);
cpa.add(CE);
cpa.add(C);
cpa.add(Back);
cpa.add(bai);
cpa.add(x);
cpa.add(sqrt);
cpa.add(zf);
cpa.add(n);
cpa.add(S );
this.add(cpa);
}
public static void main(String[] args) {
/* javax.swing.SwingUtilities.invokeLater(
new Runnable() {
public void run() {
Cal app=new Cal("计算器",300,300);
app.setVisible(true);
}
});
}}*/
Model model=new Model();
Cal cal=new Cal("计算器",300,300,model);
cal.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -