GUI的窗体事件

【一】GUI的窗体事件

导包:
java.awt.event.*;
java.awt.*;
说明:
因为WindowListener接口的子类WindowAdapter已经实现并覆盖了父类的所有方法。
那么只要继承来自WindowAdapter覆盖需要的方法即可。

【二】代码示例

import java.awt.*;
import java.awt.event.*;
class GuiDemo
{
	public static void main(String[] args) 
	{
		Frame f=new Frame("测试窗口");
		f.setBounds(200,100,600,500);
		f.setVisible(true);
		
		//addWindowListener()增加窗口监听事件
		//已打开窗口时调用
		f.addWindowListener(new WindowAdapter(){
			 public void windowOpened(WindowEvent e){
				System.out.println("窗口打开啦");
			 }
		});
		//窗体前置就实现windowActivated
		f.addWindowListener(new WindowAdapter(){
			public void windowActivated(WindowEvent e){
				System.out.println("活了。。。");
			}
		});
		//窗口关闭实现windowClosing
		f.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.out.println("关闭窗口");
				System.exit(0);
			}
		});
	}
}

 【三】截图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
整理了Java GUI编程基础知识! public class AllFrame { /** * 这里的代码涉及到 GUI 编程基本面板、基本组件、事件(ActionListener、ChangeListener、MouseListener) */ public AllFrame() { // new 一个主窗体 也可以这样写 new MainFrame(); MainFrame mf = new MainFrame(); } public static void main(String[] args) { new AllFrame(); } // 主窗体 继承 窗体基本格式 Jframe 以下弹出窗口均继承 Jframe class MainFrame extends Jframe implements ActionListener { JMenuBar menubar = new JMenuBar();;// 菜单条 JMenu jmenu[] = new JMenu[5];// panelMenu,usedMenu1,usedMenu2,layoutMenu,helpMenu; // 菜单项 JButton closebtn;// 关闭窗体 // 具体的选项 JMenuItem panelitem[] = new JMenuItem[4]; JMenuItem useditem1[] = new JMenuItem[6]; JMenuItem useditem2[] = new JMenuItem[6]; JMenuItem layoutitem[] = new JMenuItem[3]; JMenuItem helpitem = new JMenuItem("关于"); // 菜单名称 String menu[] = { "面板使用", "常用组件一", "常用组件二", "布局设置", "帮助" }; String pane[] = { "普通面板", "滚动面板", "分割面板", "选项面板" }; String uesd1[] = { "标签", "按钮", "文本框", "文本域", "单选按钮" ,"微调"}; String uesd2[] = { "滑块","复选框", "下拉菜单", "列表", "表格", "树状" }; String layout[] = { "流式布局", "边界布局", "网格布局" }; // Jpanelitem,Jscrollpaneitem,Splitpaneitem,JTabbedpanelitem, // Jlabelitem,Jbuttonitem,JTextfielditem,JTextareaitem,JRadiobuttonitem, // JCheckboxitem,JCombomoxitem,Jlistitem,Jtableitem,Jtreeitem, // Flowlayoutitem,Borderlayoutitem,Girdlayoutitem, // Helpitem}; public MainFrame() { this.setTitle("JAVA图型界面学习(GUI)"); // 加载菜单条到窗体 this.setJMenuBar(menubar); // 重新设置大小 this.setSize(380, 290); // 简单的居方法 在窗体基本格式 Jframe 有详细的居方法 this.setLocationRelativeTo(null); // 设置关闭窗口 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 调用方法 menu(); // 设置容器 Container con = this.getContentPane(); // 定义一个 标签 “JAVA图型界面学习” JLabel jl = new JLabel("JAVA图型界面学习"); // 组件 设置字体大小, 其他组件用法相同 jl.setFont(new Font("隶书", Font.BOLD, 24)); // 具体定位 组件名.setBounds(x, y, width, height) jl.setBounds(30, 30, 280, 50); // 把标签加到 con 容器 con.add(jl); /
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值