简易计算器
作者:互联网
组合
面向过程
public class TestCalc { public static void main(String[] args) { new Calculate(); } } class Calculate extends Frame{ public Calculate(){ TextField num1 = new TextField(10); TextField num2 = new TextField(10); TextField num3 = new TextField(20); Label label = new Label("+"); Button button = new Button("="); button.addActionListener(new CalculateListener(num1,num2,num3)); setLayout(new FlowLayout()); add(num1); add(label); add(num2); add(button); add((num3)); pack(); setVisible(true); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { super.windowClosing(e); System.exit(0); } }); } } class CalculateListener implements ActionListener{ private TextField num1,num2,num3; public CalculateListener(TextField num1,TextField num2,TextField num3){ this.num1=num1; this.num2=num2; this.num3=num3; } @Override public void actionPerformed(ActionEvent e) { int n1=Integer.parseInt(num1.getText()); int n2=Integer.parseInt(num2.getText()); num3.setText(""+(n1+n2)); num1.setText(""); num2.setText(""); } }
面向对象
public class TestCalc { public static void main(String[] args) { Calculate a=new Calculate(); a.loadFrame(); } } class Calculate extends Frame{ TextField num1,num2,num3; public void loadFrame(){ num1 = new TextField(10); num2 = new TextField(10); num3 = new TextField(20); Label label = new Label("+"); Button button = new Button("="); button.addActionListener(new CalculateListener(this)); setLayout(new FlowLayout()); add(num1); add(label); add(num2); add(button); add((num3)); pack(); setVisible(true); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { super.windowClosing(e); System.exit(0); } }); } } class CalculateListener implements ActionListener{ Calculate calculate; public CalculateListener(Calculate calculate){ this.calculate=calculate; } @Override public void actionPerformed(ActionEvent e) { int n1=Integer.parseInt(calculate.num1.getText()); int n2=Integer.parseInt(calculate.num2.getText()); calculate.num3.setText(""+(n1+n2)); calculate.num1.setText(""); calculate.num2.setText(""); } }
内部类
public class TestCalc { public static void main(String[] args) { Calculate a=new Calculate(); a.loadFrame(); } } class Calculate extends Frame{ TextField num1,num2,num3; class CalculateListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { int n1=Integer.parseInt(num1.getText()); int n2=Integer.parseInt(num2.getText()); num3.setText(""+(n1+n2)); num1.setText(""); num2.setText(""); } } public void loadFrame(){ num1 = new TextField(10); num2 = new TextField(10); num3 = new TextField(20); Label label = new Label("+"); Button button = new Button("="); button.addActionListener(new CalculateListener()); setLayout(new FlowLayout()); add(num1); add(label); add(num2); add(button); add((num3)); pack(); setVisible(true); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { super.windowClosing(e); System.exit(0); } }); } }
标签:num1,num2,num3,简易,计算器,new,TextField,public 来源: https://www.cnblogs.com/guojianglong/p/16549295.html