黑马程序员_GUI编程n种方法实现两个数相加
2013-02-18 18:24
190 查看
------- android培训、java培训、期待与您交流! ----------
题目:三个TextField一个Label一个Button实现两个数相加求和
一、用一个类实现:
第一种(不继承Frame):
import java.awt.*; import java.awt.event.*; class TestCalc1 implements ActionListener { TextField tf1 = new TextField(10); TextField tf2 = new TextField(10); TextField tf3 = new TextField(15); Label lblPlus = new Label("+"); Button btnEqual = new Button("="); Frame f = new Frame(); TestCalc1() { f.setLayout(new FlowLayout()); f.add(tf1); f.add(lblPlus); f.add(tf2); f.add(btnEqual); btnEqual.addActionListener(this); f.add(tf3); f.setVisible(true); f.pack(); } public static void main(String[] args) { new TestCalc1(); } public void actionPerformed(ActionEvent e) { int a = Integer.parseInt(tf1.getText()); int b = Integer.parseInt(tf2.getText()); tf3.setText((a+b) + ""); } }
第二种(继承Frame):
import java.awt.*; import java.awt.event.*; class TestCalc2 extends Frame implements ActionListener { TextField tf1 = new TextField(10); TextField tf2 = new TextField(10); TextField tf3 = new TextField(15); Label lblPlus = new Label("+"); Button btnEqual = new Button("="); TestCalc2() { setLayout(new FlowLayout()); add(tf1); add(lblPlus); add(tf2); add(btnEqual); btnEqual.addActionListener(this); add(tf3); setVisible(true); pack(); } public static void main(String[] args) { new TestCalc1(); } public void actionPerformed(ActionEvent e) { int a = Integer.parseInt(tf1.getText()); int b = Integer.parseInt(tf2.getText()); tf3.setText((a+b) + ""); } }
二、用两个类实现:
import java.awt.*; import java.awt.event.*; public class CalcDesign { public static void main(String[] args) { new Calc(); } } class Calc extends Frame implements ActionListener { TextField tf1 = new TextField(); TextField tf2 = new TextField(); TextField tf3 = new TextField(); Label l1 = new Label("+"); Button b1 = new Button("="); public Calc() { setLayout(new FlowLayout()); add(tf1); add(l1); add(tf2); add(b1); add(tf3); b1.addActionListener(this); pack(); setVisible(true); } public void actionPerformed(ActionEvent e) { int a = Integer.parseInt(tf1.getText()); int b = Integer.parseInt(tf2.getText()); int c = a + b; //多种整型转化成字符串型的方法 //tf3.setText(String.valueOf(c)); //tf3.setText(((Integer)a).toString()); //tf3.setText(c + ""); tf3.setText(Integer.toString(c)); } }
三、用三个类实现:
第一种(引用方式):
import java.awt.*; import java.awt.event.*; class TestCalc { public static void main(String[] args) { new Calc().lunchFrame(); } } class Calc extends Frame { TextField tf1 = new TextField(10); TextField tf2 = new TextField(10); TextField tf3 = new TextField(15); Label lblPlus = new Label("+"); Button btnEqual = new Button("="); public void lunchFrame() { setLayout(new FlowLayout()); add(tf1); add(lblPlus); add(tf2); add(btnEqual); btnEqual.addActionListener(new Monitor()); add(tf3); pack(); setVisible(true); } } class Monitor implements ActionListener { Calc c = null; Monitor(Calc c) { //传递引用 this.c = c; } public void actionPerformed(ActionEvent e) { int a = Integer.parseInt(c.tf1.getText()); int b = Integer.parseInt(c.tf2.getText()); c.tf3.setText("" + (a+b)); } }
第二种(使用内部类):
import java.awt.*; import java.awt.event.*; class TestCalc { public static void main(String[] args) { new Calc().lunchFrame(); } } class Calc extends Frame { TextField tf1 = new TextField(10); TextField tf2 = new TextField(10); TextField tf3 = new TextField(15); Label lblPlus = new Label("+"); Button btnEqual = new Button("="); public void lunchFrame() { setLayout(new FlowLayout()); add(tf1); add(lblPlus); add(tf2); add(btnEqual); btnEqual.addActionListener(new Monitor()); add(tf3); pack(); setVisible(true); } //内部类 class Monitor implements ActionListener { public void actionPerformed(ActionEvent e) { int a = Integer.parseInt(tf1.getText()); int b = Integer.parseInt(tf2.getText()); tf3.setText("" + (a+b)); } } }
第三种(使用局部内部类):
import java.awt.*; import java.awt.event.*; class TestCalc { public static void main(String[] args) { new Calc().lunchFrame(); } } class Calc extends Frame { TextField tf1 = new TextField(10); TextField tf2 = new TextField(10); TextField tf3 = new TextField(15); Label lblPlus = new Label("+"); Button btnEqual = new Button("="); public void lunchFrame() { setLayout(new FlowLayout()); add(tf1); add(lblPlus); add(tf2); add(btnEqual); add(tf3); pack(); setVisible(true); //局部内部类 class Monitor implements ActionListener { public void actionPerformed(ActionEvent e) { int a = Integer.parseInt(tf1.getText()); int b = Integer.parseInt(tf2.getText()); tf3.setText("" + (a+b)); } } btnEqual.addActionListener(new Monitor()); } }
相关文章推荐
- 用GUI实现求两个数的加法--方法一
- 用GUI实现求两个数的加法--方法二
- Java GUI编程之贪吃蛇游戏简单实现方法【附demo源码下载】
- 一道爱出的题目,就是前面两个数相加 用递归方法实现
- 黑马程序员—编程实现:猫和狗都会叫,但猫是喵喵的叫,狗是汪汪的叫?定义一个动物类,在动物类(animal)中有一个叫的抽象方法。 写两个子类,一个猫一个狗, * 继承自动物类,并实现相应的抽象方法。
- Android编程实现3D滑动旋转效果的方法
- Spring MVC 编程两种方法的实现
- C#编程实现动态改变配置文件信息的方法
- Android编程实现启动另外的APP及传递参数的方法
- java语言GUI编程之TextField中setEchoChar方法的使用
- 黑马程序员<Java笔记<GUI 网络编程反射>>
- Android编程实现录音及保存播放功能的方法【附demo源码下载】
- 黑马程序员****OC语言基础****有参和无参方法的调用和实现
- .Net下实现可扩展的编程方法简述
- 多线程编程--5种方法实现线程同步
- Photoshop选区技术编程实现方法研究
- PHP编程快速实现数组去重的方法详解
- 程序实现自启动的各种方法及编程实现
- 黑马程序员------GUI编程复习
- 黑马程序员__三种方法实现两个变量值的交换