當前位置:編程學習大全網 - 編程語言 - Java Applet編程:設計壹個簡單的計算器

Java Applet編程:設計壹個簡單的計算器

下面的代碼有壹些妳這裏的功能,妳看懂了再去增加,很容易的

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

public class LX10 extends JFrame implements ActionListener {

p P;

p1 P1;

p2 P2;

LX10(){

P=new p();

P1=new p1();

P2=new p2();

setSize(300,120);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLayout(new BorderLayout());

getContentPane().add(P,BorderLayout.NORTH);

getContentPane().add(P1,BorderLayout.EAST);

getContentPane().add(P2,BorderLayout.CENTER);

P1.B.addActionListener(this);

}

public static void main(String[] args)

{

LX10 a=new LX10();

a.setVisible(true);

}

class p extends JPanel

{ JTextField t1;

JTextField t2;

Label l1;

Label l2;

p()

{

t1=new JTextField(5);

t2=new JTextField(5);

l1=new Label("加數:");

l2=new Label("被加數:");

setLayout(new FlowLayout());

add(l1);

add(t1);

add(l2);

add(t2);

}

}

class p1 extends JPanel

{

JButton B=new JButton("運算");

p1()

{

setLayout(new FlowLayout());

add(B);

}

}

class p2 extends JPanel

{

JTextField r;

Label l3;

p2()

{

r=new JTextField(6);

l3=new Label("結果");

setLayout(new FlowLayout());

add(l3);

add(r);

}

}

public void actionPerformed(ActionEvent e) {

if(P.t1.getText()!=""||P.t2.getText()!="")

{

int d1=Integer.parseInt(P.t1.getText());

int d2=Integer.parseInt(P.t2.getText());

P2.r.setText(Integer.toString(d1+d2));

}

}

}

  • 上一篇:曉東編程
  • 下一篇:電磁流量計零點校準數值含義
  • copyright 2024編程學習大全網