实验七 图形用户界面与事件处理机制(二)

import java.awt.BorderLayout; import java.awt.Button; import java.awt.Color; import java.awt.Frame; import java.awt.Dialog; import java.awt.Label;

import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Panel;

import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent;

class MyDialog implements ActionListener { private Button quit = new Button(\关闭\); private Dialog Dg; private Label la = new Label(); MyDialog(Frame Fm) { Dg = new Dialog(Fm,\菜鸟制作。。\,true); Dg.setLayout(new BorderLayout()); Dg.setBounds(300,150,400,200); Dg.setBackground(Color.green); Dg.add(la); la.setPreferredSize(new Dimension(400,30)); Dg.add(\,quit); Panel panel = new Panel(); panel.add(quit); Dg.add(\, panel); la.setText(\版本型号:V 1.2.3\); quit.addActionListener(this); Dg.setVisible(true); } public void actionPerformed(ActionEvent e) { if(e.getSource()==quit) Dg.dispose(); } }

public class Alex implements ActionListener { private Button quit = new Button(\退出\); private Button login = new Button(\打开对话框\);

}

private Label la = new Label(\对话框控制操作:\); private Frame Fm = new Frame(); public Alex() { Fm.setTitle(\。制作:\); Fm.setLayout(new FlowLayout(FlowLayout.LEFT,20,20)); Fm.setBounds(500,300,400,200); Fm.add(la); la.setPreferredSize(new Dimension(400,30)); Fm.add(quit); quit.addActionListener(this); Fm.add(login); login.addActionListener(this); Fm.setVisible(true); Fm.addWindowListener ( new WindowAdapter() { public void windowClosing(WindowEvent windowEvent) { Fm.dispose(); } } ); }

public void actionPerformed(ActionEvent e) { if(e.getSource()==quit) System.exit(0); if(e.getSource()==login) new MyDialog(Fm); }

public static void main(String args[]) { Alex Ax = new Alex(); }

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4