java实验报告 - - 129074(4)

2019-06-05 11:14

MyMenuFrame mf=new MyMenuFrame(); mf.setSize(new Dimension(300,200)); //指定窗口初始大小 mf.setVisible(true); //使窗口可见,默认为不可见 } }

//定义窗口

class MyMenuFrame extends Frame implements ActionListener,ItemListener{ MenuBar m_MenuBar; Menu menuFile,menuEdit,m_Edit_Pasete; MenuItem mi_File_Open,mi_File_Close,mi_File_Exit,mi_Edit_Copy; MenuItem pi_New,pi_Del,pi_Pro,mi_paste_All,mi_Paste_Part; CheckboxMenuItem mi_Edit_cut; PopupMenu popM; TextArea ta; MyMenuFrame(){ super(\拥有菜单的窗口\); //指定窗口标题 ta=new TextArea(\,5,20); ta.addMouseListener((MouseListener) new HandleMouse(this)); add(\,ta); //创建弹出式菜单 popM=new PopupMenu(); pi_New=new MenuItem(\新建\); pi_New.addActionListener(this); popM.add(pi_New); pi_Del=new MenuItem(\删除\); pi_Del.addActionListener(this); popM.add(pi_Del); pi_Pro=new MenuItem(\属性\); pi_Pro.addActionListener(this); popM.add(pi_Pro); //将弹出式菜单加在文本域上 ta.add(popM); m_MenuBar=new MenuBar(); //创建菜单栏 menuFile=new Menu(\文件\); //创建菜单项、菜单自项并指定快捷键 mi_File_Open=new MenuItem(\打开\,new MenuShortcut('o')); mi_File_Close=new MenuItem(\关闭\); mi_File_Exit=new MenuItem(\退出\); mi_File_Exit.setShortcut(new MenuShortcut('x')); mi_File_Open.setActionCommand(\打开\); mi_File_Close.setActionCommand(\退出\); mi_File_Open.addActionListener(this); mi_File_Close.addActionListener(this); //自身作为监视器 mi_File_Exit.addActionListener(this); menuFile.add(mi_File_Open); menuFile.add(mi_File_Close); menuFile.addSeparator(); menuFile.add(mi_File_Exit); m_MenuBar.add(menuFile); //实现以下菜单,并添加监听 /*(“编辑”)((“复制”);(“剪切”); (“粘贴”)((“全部粘贴”); (“部分粘贴”)))*/ menuEdit=new Menu(\编辑\); mi_Edit_Copy=new MenuItem(\复制\); mi_Edit_cut=new CheckboxMenuItem(\剪切\); m_Edit_Pasete=new Menu(\粘贴\);

//文本域响应鼠标事件

文 } }

MenuItem mi_Paste_All = new MenuItem(\全部粘帖\); MenuItem mi_Paste_Part=new MenuItem(\部分粘贴\);

mi_Edit_Copy.addActionListener(this); mi_Edit_cut.addItemListener(this); m_Edit_Pasete.add(mi_Paste_Part); m_Edit_Pasete.add(mi_Paste_All);

mi_Paste_Part .addActionListener(this); mi_Paste_All.addActionListener(this); menuEdit.add(mi_Edit_Copy); menuEdit.add(mi_Edit_cut); menuEdit.addSeparator();

menuEdit.add(m_Edit_Pasete); m_MenuBar.add(menuEdit);

this.setMenuBar(m_MenuBar); //在窗口中添加菜单栏 }

public void actionPerformed(ActionEvent e){ if(e.getActionCommand()==\退出\){ dispose(); System.exit(0); } else ta.setText(e.getActionCommand()); }

public void itenStateChanged(ItemEvent e){ if(e.getSource()==mi_Edit_cut) if(((CheckboxMenuItem)e.getSource()).getState()) ta.setText(\+((CheckboxMenuItem)e.getSource()).getLabel());//将时间小时在 else//本框中显示 ta.setText(\+((CheckboxMenuItem)e.getSource()).getLabel()); }

@Override

public void itemStateChanged(ItemEvent e) { // TODO Auto-generated method stub }

class HandleMouse extends MouseAdapter{//处理鼠标事件类 MyMenuFrame m_Parent; HandleMouse(MyMenuFrame mf){ m_Parent=mf; }

public void mouseReleased(MouseEvent e){//鼠标按键松开事件弹出菜单 if(e.isPopupTrigger())//检查鼠标事件是否是由弹出菜单引发的 m_Parent.popM.show((Component)e.getSource(),e.getX(),e.getY()); }

编译并运行TestMenu.class查看结果。

⒊使用Dialog实验消息对话框和一般对话框

编辑TestDialog.java,设保存在D:\\myjava目录下。

import java.awt.*;

import java.awt.event.*;

public class search_7_TestDialog { public static void main(String args[ ]){

MyDialogFrame df= new MyDialogFrame(); } }

class MyDialogFrame extends Frame implements ActionListener,ComponentListener{ Dialog MegDlg,InOutDlg; Button btn1,btn2,btnY,btnN,btnR; TextField tf=new TextField(\,45); TextField gerMeg=new TextField(\,10); MyDialogFrame(){ super(\); btn1=new Button(\隐藏\); btn2=new Button(\询问\); btnY=new Button(\是\); btnN=new Button(\否\); btnR=new Button(\返回\); setLayout(new FlowLayout()); add(tf); add(btn1); add(btn2); btn1.addComponentListener(this); this.addWindowListener(new WinAdpt());//Frame响应窗口关闭事件 btn1.addActionListener(this); btn2.addActionListener(this); btnY.addActionListener(this); btnN.addActionListener(this); btnR.addActionListener(this); setSize(350,150); show(); } public void actionPerformed(ActionEvent e){ //实现Action Listener中的方法 if(e.getActionCommand()==\隐藏\){ MegDlg=new Dialog(this, \,true); Panel p1=new Panel(); p1.add(new Label(\)); MegDlg.add(\,p1); Panel p2=new Panel(); p2.add(btnY); p2.add(btnN); MegDlg.add(\,p2); MegDlg.setSize(200,100); MegDlg.show(); } else if(e.getActionCommand()==\响应\){ InOutDlg=new Dialog(this, \); InOutDlg.add(\,gerMeg); InOutDlg.add(\,btnR); InOutDlg.setSize(200,100); InOutDlg.addComponentListener(this); InOutDlg.show(); } else if(e.getActionCommand()==\是\){ MegDlg.dispose(); btn1.setVisible(false); } else if(e.getActionCommand() == \否\){ MegDlg.dispose(); } else if(e.getActionCommand()==\返回\){ tf.setText(gerMeg.getText()+\是对话框的输入\); InOutDlg.dispose(); } } //列举Component Listener中未重载的方法

public void componentShown(ComponentEvent e) {} public void componentResized(ComponentEvent e) {} public void componentMoved(ComponentEvent e) {} public void componentHidden(ComponentEvent e) {//实现ComponentListener中的方法 tf.setText(\按钮\+((Button)e.getComponent()).getLabel()+\获得了注意的焦点\); } public void fousGained(FocusEvent e){ //处理FocusListener中的方法 gerMeg.setText(\对话框\+((Dialog)e.getComponent()).getTitle()+\获得了注意的焦点\); } public void focusLost(FocusEvent e) {} }

class WinAdpt extends WindowAdapter{ public void windowClosing(WindowEvent e){ //处理关闭窗口事件 ((Frame)e.getWindow()).dispose(); System.exit(0); } }

编译并运行查看结果

4、根据上面参考程序, 实现一个登陆系统(在数据库中创建一个用户表(编号,用户名,密码))

1)创建登陆窗口,实现用户名和密码的数据库连接、判断;如果成功,转动第二步;不成功,重新输入用户名和密码。只能输入三次,如果超过三次,则退出系统。

2)如果成功,则进入一个新窗口,原来的登陆窗口关闭。

import java.awt.event.*; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel;

import javax.swing.JOptionPane; import javax.swing.JPanel;

import javax.swing.JPasswordField; import javax.swing.JTextField;

public class Logoin extends JFrame{ static int n = 0; private static JFrame jf = new JFrame(\登陆窗口\); private JPanel jp =new JPanel(); private JLabel l_name = new JLabel(\账号\); private JLabel l_password = new JLabel(\密码\); static JTextField t_name = new JTextField(20); static JPasswordField t_password = new JPasswordField(20); private JButton ok = new JButton(\登陆\); private JButton cancel = new JButton(\退出\); public Logoin(){ ok.addActionListener(new okListener()); cancel.addActionListener(new cancelListener()); jp.add(l_name); jp.add(t_name); jp.add(l_password); jp.add(t_password); jp.add(ok); jp.add(cancel); jf.add(jp);

jf.setVisible(true); jf.setSize(300,150); jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE); jf.setLocation(500,270); } class okListener implements ActionListener{ public void actionPerformed(ActionEvent arg0) { check(); } } class cancelListener implements ActionListener{ public void actionPerformed(ActionEvent arg0){ System.exit(0); } } public static void check(){ char[] ch=t_password.getPassword(); if(t_name.getText().equals(\)){ JOptionPane.showMessageDialog(null, \请输入用户名!\,\,JOptionPane.ERROR_MESSAGE); } else if(t_password.getPassword().equals(\)){ JOptionPane.showMessageDialog(null, \请输入密码\,\,JOptionPane.ERROR_MESSAGE); } else if(t_name.getText()==\&&ch.toString()==\){ JOptionPane.showMessageDialog(null, \登陆成功\,\,JOptionPane.ERROR_MESSAGE); n=0; jf.setVisible(false); JFrame jf2 = new JFrame(\登陆成功窗口\); jf2.setVisible(true); } else{

JOptionPane.showMessageDialog(null, \账号或密码不正确!\,\,JOptionPane.ERROR_MESSAGE); n+=1; if(n>=3) System.exit(1); } }

public static void main(String[] args){ new Logoin();

}

运行结果:

}五、注意事项

⒈认真填写实验报告

⒉遵守实验室各项制度,服从实验指导教师的安排 ⒊按规定的时间完成实验

六、说明

⒈建议学时数6学时 七、实验总结与体会

通过这个实验基本掌握了用MouseListener和MouseMotionListener接口处理鼠标事件Mouse Event的简单方法,掌


java实验报告 - - 129074(4).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:三网融合:广电网的宏观战略思考

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: