Java基础与面向对象实用小程序11

上传人:cn****1 文档编号:465849280 上传时间:2023-03-06 格式:DOCX 页数:17 大小:19.09KB
返回 下载 相关 举报
Java基础与面向对象实用小程序11_第1页
第1页 / 共17页
Java基础与面向对象实用小程序11_第2页
第2页 / 共17页
Java基础与面向对象实用小程序11_第3页
第3页 / 共17页
Java基础与面向对象实用小程序11_第4页
第4页 / 共17页
Java基础与面向对象实用小程序11_第5页
第5页 / 共17页
点击查看更多>>
资源描述

《Java基础与面向对象实用小程序11》由会员分享,可在线阅读,更多相关《Java基础与面向对象实用小程序11(17页珍藏版)》请在金锄头文库上搜索。

1、例11-1事件处理机制的应用:按钮功能的实现本例运行后,点击“提交”按钮将提取用户输入的信息显示在不可编辑文本 框中,点击“清空”按钮所有文本框内容将被清除,点击“退出”按钮程序运行 结束。/MyFrame.javaimport javax.swing.*;import java.awt.event.*;public class MyFrame extends JFrame implements ActionListener(JLabel labName,labAge,labShow;JTextField txtName,txtAge,txtShow;JButton btnSubmit,btn

2、Reset,btnExit;JPanel pan = new JPanel();MyFrame(String s)(super(s);setSize(300,150);this.setLocationRelativeTo(null);setContentPane(pan);labName = new JLabel(姓 名:);labAge = new JLabel(年 龄:);labShow = new JLabel(您输入的是:);txtName = new JTextField(10);txtAge = new JTextField(5);txtShow = new JTextField(

3、17);txtShow.setEditable(false);btnSubmit = new JButton( 提交);btnReset = new JButton( 清空);btnExit = new JButton( 退出);btnSubmit.addActionListener(this);btnReset.addActionListener(this);btnExit.addActionListener(this);pan.add(labName);pan.add(txtName);pan.add(labAge);pan.add(txtAge);pan.add(labShow);pan

4、.add(txtShow);pan.add(btnSubmit);pan.add(btnReset);pan.add(btnExit);this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);setVisible( true );public void actionPerformed(ActionEvent e)(if(e.getSource()=btnSubmit)String str = txtName.getText()+txtAge.getText();txtShow.setText(str);if(e.getSource()=bt

5、nReset)txtName.setText();txtAge.setText();txtShow.setText();if(e.getSource()=btnExit)System.exit(1);/EventDemo.javapublic class EventDemo (public static void main(String args) (MyFrame frm = new MyFrame( 事件处理演示);例11-2点击不同按钮,改变窗体的背景色/MyFrame.javaimport javax.swing.*;import java.awt.event.*;import jav

6、a.awt.*;public class MyFrame extends JFrame implements ActionListener (JButton btnYellow,btnGreen,btnWhite;JPanel pan = new JPanel();MyFrame(String s)(super(s);setSize(200,200);this.setLocationRelativeTo(null);setContentPane(pan);btnYellow = new JButton(黄色”);btnYellow.addActionListener(this);btnGree

7、n = new JButton( 绿色”);btnGreen.addActionListener(this);btnWhite = new JButton( 白色”);btnWhite.addActionListener(this);给按钮增加事件监听增加事件监听增加事件监听pan.add(btnYellow);pan.add(btnGreen);pan.add(btnWhite);this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);setVisible( true );实现 ActionListener 接口中的 actionPerf

8、ormed 方法public void actionPerformed(ActionEvent e)(if(e.getSource()=btnYellow)判断事件源pan.setBackground(Color.YELLOW);设置背景色if(e.getSource()=btnGreen)pan.setBackground(Color.GREEN);if(e.getSource()=btnWhite)pan.setBackground(Color.WHITE);/ BtnEventDemo.javapublic class BtnEventDemo (public static void m

9、ain(String args) (MyFrame frm = new MyFrame( 按钮事件演示);java.awt.event.*;javax.swing.*;java.awt.*;例11-3使用菜单项实现对窗体背景色进行设置 / MenuFrame.java import import importpublicclass MenuFrame extends JFrame implements ActionListener (JPanel pan = new JPanel();JMenuBar menubar = new JMenuBar();JMenu colorMenu;JMenu

10、Item yellowItem,greenItem,whiteItem,exitItem;MenuFrame(String s)(super(s);setSize(200,200);this.setLocationRelativeTo(null);setContentPane(pan);colorMenu = new JMenu( 背景色);背景色菜单及菜单项创建yellowItem = new JMenuItem( 黄色”);greenItem = new JMenuItem( 绿色”);whiteItem = new JMenuItem( 白色”);exitItem = new JMenu

11、Item( 退出);setJMenuBar(menubar); /菜单条、菜单、菜单项的组合menubar.add(colorMenu);colorMenu.add(yellowItem);colorMenu.add(greenItem);colorMenu.add(whiteItem);colorMenu.add(exitItem);yellowItem.addActionListener(this); / 给菜单项添加事件监听greenItem.addActionListener(this);whiteItem.addActionListener(this);exitItem.addAct

12、ionListener(this);this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);setVisible( true );实现 ActionListener 接口中的 actionPerformed 方法public void actionPerformed(ActionEvent e) (if(e.getSource()=yellowItem)/判断事件源pan.setBackground(Color.YELLOW);/设置背景色if(e.getSource()=greenItem)pan.setBackground(Color.

13、GREEN);if(e.getSource()=whiteItem)pan.setBackground(Color.WHITE);if(e.getSource()=exitItem)System.exit(0);/ MenuItemEventDemo.javapublic class MenuItemEventDemo (public static void main(String args) (MenuFrame frm = new MenuFrame( 菜单项事件演示);例11-4选择下拉列表中的国家,在窗体中显示该国家的国旗/ MyFrame.javaimport javax.swing

14、.*;import java.awt.event.*;public class MyFrame extends JFrame implements ItemListener (JPanel pan;JComboBox cmb;JLabel labTips,labImage;/用于显示文字、图片的标签Icon icon;/引用图片的Icon对象使用String数组存放国旗图片名称String images口 =厂1厂1 C厂 丁邛China.png,Canada.png,Germany.png,France.png,Japan.png;public MyFrame(String s) (supe

15、r(s);this.setSize(300,200);this.setLocationRelativeTo(null);pan = new JPanel();this.setContentPane(pan);cmb = new JComboBox();创建组合框String country: = 中国,加拿大,德国,法国,日本;for(int i = 0;i country.length; i+)/循环将国家名称添加全组合框cmb.addItem(countryi);cmb.addItemListener(this);/给组合框增加事件监听/文字标签引用国旗图片带图片的标签labTips = ne

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 学术论文 > 其它学术论文

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号