用户图形界面的实现

来源:互联网 发布:天津网络报警平台 编辑:程序博客网 时间:2024/05/18 03:33

package work_15;import java.awt.Button;import java.awt.Color;import java.awt.Component;import java.awt.Font;import java.awt.Frame;import java.awt.GridLayout;import java.awt.Label;import java.awt.TextField;import java.awt.Window;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowEvent;import java.awt.event.WindowListener;public class lalala extends Frame implements WindowListener, ActionListener {private Button b1,b2,b3,b4,b5;private Label L1,L2,L3,L4,L5;private TextField t1,t2,t3;public void actionPerformed(ActionEvent xpp) {String n;n=xpp.getActionCommand();if(n.equals("连接")){String m=t1.getText().concat(t2.getText());t3.setText(m);}if(n.equals("加")){String a=t1.getText();String b=t2.getText();double c=Double.parseDouble(a)+Double.parseDouble(b);t3.setText(c+"");}if(n.equals("乘")){String a=t1.getText();String b=t2.getText();double c=Double.parseDouble(a)*Double.parseDouble(b);t3.setText(c+"");}if(n.equals("减")){String a=t1.getText();String b=t2.getText();double c=Double.parseDouble(a)-Double.parseDouble(b);t3.setText(c+"");}if(n.equals("除")){String a=t1.getText();String b=t2.getText();double c=Double.parseDouble(a)+Double.parseDouble(b);t3.setText(c+"");}else if(n.equals("清除")){t1.setText("");t2.setText("");t3.setText("");}}public void windowActivated(WindowEvent e) {}public void windowClosed(WindowEvent xpp) {}public void windowClosing(WindowEvent xpp) {Window f=(Window)xpp.getSource();f.setVisible(false);f.dispose();}public void windowDeactivated(WindowEvent e) {}public void windowDeiconified(WindowEvent e) {}public void windowIconified(WindowEvent e) {}public void windowOpened(WindowEvent e) {}public lalala(){super("基本运算");setLocation(300,500);setSize(600,200);setLayout(new GridLayout(3,5,5,5));//行列水平竖直间距Font f=new Font("宋体",Font.ITALIC,25);//字体型号setFont(f);setForeground(Color.yellow);setBackground(Color.darkGray);b1=new Button("连接");b2=new Button("清除");b3=new Button("加");b4=new Button("乘");b5=new Button("减");L1=new Label("+");L2=new Label("=");L3=new Label("第一个数");L4=new Label("第二个数");L5=new Label("结果");t1=new TextField();t2=new TextField();t3=new TextField();add(L3);add(new Label());add(L4);add(new Label());add(L5);add(t1);add(L1);add(t2);add(L2);add(t3);add(b1);add(b4);add(b3);add(b5);add(b2);b1.addActionListener(this);b2.addActionListener(this);b3.addActionListener(this);b4.addActionListener(this);b5.addActionListener(this);addWindowListener(this);//关闭窗口}public static void main(String[] args) {lalala w=new lalala();w.setVisible(true);}}