//当输入两个数时,点加数按钮可自动加
import java.awt.*;
import java.awt.event.*;
import java.util.EventListener;
import javax.swing.*;
public class Add extends JFrame implements ActionListener{
private JLabel la1;
private JLabel la2;
private JTextField tx1;
private JTextField tx2;
private JTextField tx3;
private JButton bt;
public void Init()
{
la1 = new JLabel("加数1:");
la1.setSize(60, 30);
la1.setLocation(30,30);
la2 = new JLabel("加数2:");
la2.setSize(60, 30);
la2.setLocation(30, 70);
tx1 = new JTextField();
tx1.setSize(100,30);
tx1.setLocation(110,30);
tx2 = new JTextField();
tx2.setSize(100,30);
tx2.setLocation(110,70);
tx3 = new JTextField();
tx3.setSize(100,30);
tx3.setLocation(110,110);
bt = new JButton("加");
bt.setSize(50,30);
bt.setLocation(30,110);
this.setLayout(null);
this.setSize(300, 300);
this.add(la1);
this.add(la2);
this.add(tx1);
this.add(tx2);
this.add(tx3);
this.add(bt);
bt.addActionListener( this);
//设置可见性
this.setVisible(true);
}
public Add()
{
this.Init();
}
public void actionPerformed(ActionEvent e)
{
String str1 = tx1.getText();
String str2 = tx2.getText();
int tmp = (Integer.parseInt(str1)+Integer.parseInt(str2));
tx3.setText(String.valueOf(tmp));
}
}
public class classtest {
public static void main(String[] args) {
Add add = new Add();
}
}
用窗口实现两个加数的和
最新推荐文章于 2024-08-14 11:29:56 发布