您的位置:首页 > 产品设计 > UI/UE

黑马程序员_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());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: