1

代码如下,我希望文本字段中的任何数据都T1T2减去并在T3我们按下按钮后进入......

import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class abc extends Applet implements ActionListener {
    TextField T1, T2, T3, T4, T5;
    Button B1;
    String ST1, ST2, ST3;
    double a;

    public void paint(Graphics g) {

    setLayout(null);
    setBackground(Color.pink);
    g.drawRect(0, 0, 600, 500);
    g.drawLine(0, 50, 600, 50);
    g.drawLine(0, 80, 600, 80);
    g.drawLine(150, 50, 150, 80);
    g.drawLine(300, 50, 300, 80);
    g.drawLine(0, 110, 600, 110);
    g.drawLine(120, 80, 120, 150);
    g.drawLine(240, 80, 240, 150);
    g.drawLine(360, 80, 360, 150);
    g.drawLine(480, 80, 480, 150);
    g.drawLine(0, 150, 600, 150);
    g.drawLine(230, 150, 230, 360);
    g.drawLine(300, 150, 300, 170);
    g.drawLine(370, 150, 370, 170);
    g.drawLine(460, 150, 460, 390);
    g.drawLine(230, 170, 460, 170);
    g.drawLine(0, 390, 600, 390);
    g.drawLine(0, 360, 460, 360);
    g.drawLine(345, 200, 345, 390);
    g.drawLine(0, 200, 345, 200);
    g.drawLine(0, 420, 600, 420);
    g.drawLine(0, 220, 230, 220);
    g.drawLine(0, 240, 230, 240);
    g.drawLine(0, 260, 230, 260);
    g.drawLine(0, 280, 230, 280);
    g.drawLine(0, 300, 230, 300);
    g.drawLine(0, 320, 230, 320);
    g.drawLine(0, 340, 230, 340);
    g.drawLine(140, 220, 140, 360);

    Label L1 = new Label("A COMPANY PVT.LTD.");
    L1.setBounds(150, 5, 440, 30);
    Font myFont = new Font("TimesRoman", Font.BOLD, 18);
    L1.setFont(myFont);
    Label L2 = new Label("Ph. :01222");
    L2.setBounds(200, 30, 250, 20);
    Label L3 = new Label("Date //___");
    L3.setBounds(470, 30, 120, 20);

    Label L4 = new Label("Vehicle No.");
    Label L5 = new Label("Colliery");
    Label L6 = new Label("Unloaded at:");
    L4.setBounds(5, 55, 120, 10);
    L5.setBounds(155, 55, 130, 15);
    L6.setBounds(315, 55, 120, 10);

    this.add(L1);
    this.add(L2);
    this.add(L3);
    this.add(L4);
    this.add(L5);
    this.add(L6);

    Label L7 = new Label("Desp.Wt.in MT");
    L7.setBounds(10, 84, 100, 25);
    this.add(L7);
    Label L8 = new Label("Recd.Wt.in MT");
    L8.setBounds(135, 84, 100, 25);
    this.add(L8);
    Label L9 = new Label("Shortage in MT");
    L9.setBounds(255, 84, 100, 25);
    this.add(L9);
    Label L10 = new Label("Freight Rate PMT");
    L10.setBounds(375, 84, 100, 25);
    this.add(L10);
    Label L11 = new Label("Advance Rs.");
    L11.setBounds(500, 84, 95, 25);
    this.add(L11);

    Font myFont1 = new Font("TimesRoman", Font.BOLD, 10);
    Label L12 = new Label("Weight X");
    L12.setBounds(240, 152, 55, 15);
    this.add(L12);
    L12.setFont(myFont1);
    Label L13 = new Label("Freight =");
    L13.setBounds(310, 152, 55, 15);
    this.add(L13);
    L13.setFont(myFont1);
    Label L14 = new Label("Amount");
    L14.setBounds(390, 152, 55, 15);
    this.add(L14);
    L14.setFont(myFont1);

    Font myFont2 = new Font("ArialBlack", Font.BOLD, 10);
    Label S1 = new Label("Particulars");
    S1.setBounds(15, 221, 30, 18);
    S1.setFont(myFont2);
    this.add(S1);
    Label S2 = new Label("Shortage");
    S2.setBounds(5, 241, 30, 18);
    S2.setFont(myFont2);
    this.add(S2);
    Label S3 = new Label("Advance");
    S3.setBounds(5, 261, 30, 18);
    S3.setFont(myFont2);
    this.add(S3);
    Label S4 = new Label("Commission");
    S4.setBounds(5, 281, 30, 18);
    S4.setFont(myFont2);
    this.add(S4);
    Label S5 = new Label("Munsiana");
    S5.setBounds(5, 301, 30, 18);
    S5.setFont(myFont2);
    this.add(S5);
    Label S6 = new Label("Other Deduction");
    S6.setBounds(5, 321, 30, 18);
    S6.setFont(myFont2);
    this.add(S6);
    Label S7 = new Label("Total");
    S7.setBounds(5, 341, 30, 18);
    S7.setFont(myFont2);
    this.add(S7);
    Label R1 = new Label("Amount");
    R1.setBounds(150, 221, 40, 18);
    R1.setFont(myFont2);
    this.add(R1);
    TextField R2 = new TextField();
    R2.setBounds(145, 241, 80, 18);
    R2.setFont(myFont2);
    this.add(R2);
    TextField R3 = new TextField();
    R3.setBounds(145, 261, 80, 18);
    R3.setFont(myFont2);
    this.add(R3);
    TextField R4 = new TextField();
    R4.setBounds(145, 281, 80, 18);
    R4.setFont(myFont2);
    this.add(R4);
    TextField R5 = new TextField();
    R5.setBounds(145, 301, 80, 18);
    R5.setFont(myFont2);
    this.add(R5);
    TextField R6 = new TextField();
    R6.setBounds(145, 321, 80, 18);
    R6.setFont(myFont2);
    this.add(R6);
    TextField R7 = new TextField();
    R7.setBounds(145, 341, 80, 18);
    R7.setFont(myFont2);
    this.add(R7);

    Label L15 = new Label("Total Freight Payable Rs.");
    L15.setBounds(190, 366, 150, 19);
    this.add(L15);
    Label L16 = new Label("Rupees....................................................................................");
    L16.setBounds(30, 395, 200, 19);
    this.add(L16);
    Label L17 = new Label("Paid Date.................................................");
    L17.setBounds(10, 425, 200, 19);
    this.add(L17);
    L17 = new Label("Recieved By Name....................................");
    L17.setBounds(10, 450, 200, 19);
    this.add(L17);
    Label L18 = new Label("Mobile No........................................");
    L18.setBounds(10, 475, 200, 19);
    this.add(L18);
    Label L19 = new Label("Signature");
    L19.setBounds(250, 475, 150, 19);
    this.add(L19);
    Label L20 = new Label("For:A Co.(P)Ltd");
    L20.setBounds(440, 475, 155, 19);
    this.add(L20);

    T1 = new TextField();
    T1.setBounds(5, 118, 110, 28);
    this.add(T1);
    T2 = new TextField();
    T2.setBounds(125, 118, 110, 28);
    this.add(T2);
    T3 = new TextField();
    T3.setBounds(245, 118, 110, 28);
    this.add(T3);
    T4 = new TextField();
    T4.setBounds(365, 118, 110, 28);
    this.add(T4);
    T5 = new TextField();
    T5.setBounds(485, 118, 110, 28);
    this.add(T5);

    B1 = new Button("Submit");
    B1.setBounds(200, 520, 100, 23);

    B1.addActionListener(this);
    this.add(B1);

    T1.setText("40");
    T2.setText("20");
    ST1 = T1.getText();
    ST2 = T2.getText();
    a = Double.parseDouble(ST1) - Double.parseDouble(ST2);
    ST3 = "" + a;
    }

    public void actionPerformed(ActionEvent ev) {
    Object obj = ev.getSource();
    if (obj == B1) {

        System.out.println("wjnqjqw");
    } else {
        T3.setText(ST3);
        System.out.println("assassas");

    }
    }
}// <applet code="abc.java" width=600 height=550></applet>
4

1 回答 1

1

1.首先添加 B1,然后对其应用监听器

2.将减法代码 保留在actionPerformed()方法中。

T1.setText("40");
T2.setText("20"); 
this.add(B1);
B1.addActionListener(this);

public void actionPerformed(ActionEvent ev){

ST1=T1.getText(); 
ST2=T2.getText(); 
a=Double.parseDouble(ST1)-Double.parseDouble(ST2); 
ST3=""+a; 
}

3.使用方法比较对象.equals(),所以使用(obj.equals(B1))

于 2012-07-21T12:59:56.787 回答