实验五 Java图形用户界面设计.docx
- 文档编号:5418476
- 上传时间:2022-12-16
- 格式:DOCX
- 页数:22
- 大小:147.44KB
实验五 Java图形用户界面设计.docx
《实验五 Java图形用户界面设计.docx》由会员分享,可在线阅读,更多相关《实验五 Java图形用户界面设计.docx(22页珍藏版)》请在冰豆网上搜索。
实验五Java图形用户界面设计
§5Java图形用户界面设计
§5.1实验目的、内容及性质
掌握Java的GUI设计技术,掌握AWT和Swing的应用技巧。
实验性质:
验证、必做
实验学时:
2学时
§5.2问题及思考
1、最常见的AWT以及Swing控件用法。
2、几个常见布局总结
3、区分容器控件和一般非容器控件
4、Java事件几种关键组成部分以及事件处理流程
§5.3实验指导
1、Swing示例
/*需要哪些组件,如何布局?
*/
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassMyFrmextendsJFrame{//从JFrame继承
/*声明界面需要使用的控件*/
JLabellbl_name=newJLabel("用户名");
JLabellbl_pwd=newJLabel("密码");
JTextFieldtxt_name=newJTextField();
JPasswordFieldtxt_pwd=newJPasswordField();
JButtonbtn_OK=newJButton("登陆");
JButtonbtn_Cancel=newJButton("取消");
/*在构造函数中将控件放置在JFrame上*/
publicMyFrm(){
/*获取当前Frame的内容面板*/
JPaneljp=(JPanel)this.getContentPane();
/*设置内容面板的布局Layout*/
jp.setLayout(newGridLayout(3,2));
jp.add(lbl_name);jp.add(txt_name);
jp.add(lbl_pwd);jp.add(txt_pwd);
jp.add(btn_OK);jp.add(btn_Cancel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
publicstaticvoidmain(Stringarg[]){
/*纯Java样式显示窗体*/
JFrame.setDefaultLookAndFeelDecorated(true);
/*实例化当前窗体类*/
MyFrmfrm=newMyFrm();
frm.setSize(200,200);
frm.setVisible(true);
}
}
2、常用布局
1)、流布局:
FlowLayout从左到右,自上而下方式在容器中排列,控件的大小不会随容器大小变化.
容器.setLayout(newFlowLayout(FlowLayout.LEFT));
2)、网格布局:
GridLayout按照指定行数与列数,将容器分成大小相等的单元格每个单元格放置一个控件.不能将控件放在指定单元格
容器.setLayout(newGridLayout(3,4,10,15));
3)、边界布局:
BorderLayout将容器分成东、西、南、北、中五个部分
容器.setLayout(newBorderLayout());
窗口的内容面板默认布局就是边界布局。
容器.add(控件,BorderLayout.NORTH);
4)、混合布局:
使用JPanel,将多个布局组合在一起使用
JPaneljp=(JPanel)this.getContentPane();
for(inti=0;i btn[i]=newJButton("btn"+i); JPaneljp1=newJPanel();//默认布局为FlowLayout jp1.setLayout(newGridLayout(2,2)); for(inti=0;i<4;i++) jp1.add(btn[i]); JPaneljp2=newJPanel();//默认布局为FlowLayout for(inti=0;i<4;i++) jp2.add(btn[i+4]); 5)、绝对布局null: 以坐标定位 容器.setLayout(null); 每个控件在放置在容器之前,必须设置其边界setBounds(x,y,width,height); btn.setBounds(10,100,30,60); 3、Swing示例Grid布局 importjava.awt.*; importjava.awt.event.*; importjavax.swing.*; publicclassGridLayoutDemoextendsJFrame{ privateJButtonbuttons[]; privateStringnames[]={"one","two","three","four","five","six"}; publicGridLayoutDemo(){ super("GridLayoutDemo"); JPanelcontainer=(JPanel)this.getContentPane(); container.setLayout(newGridLayout(3,2)); //createandaddbuttons buttons=newJButton[names.length]; for(intcount=0;count buttons[count]=newJButton(names[count]); container.add(buttons[count]); } this.setSize(300,150); this.setVisible(true); } publicstaticvoidmain(Stringargs[]){ GridLayoutDemoapplication=newGridLayoutDemo(); } }//endclassGridLayoutDemo 4、常用事件 基本组成: 1)、事件源EventSource: 能够触发事件控件如: JButton,JTextField,JFrame, JComboBox,.... 2)、事件Event: ActionEvent,KeyEvent,WindowEvent,TextEvent,... 3)、事件侦听者Listener(接口) ActionListener,WindowListener,... classAimplementsActionListener{ publicvoidactionPerformed(ActionEvente){ .... } } Alis=newA(); 4)、事件处理函数 publicvoidactionPerformed(ActionEvente){ .... } 事件流程: 事件源触发事件-->事件源侦听者接收事件-->自动调用相应事件处理函数. 编程模板: classMyFrmextendsJFrameimplementsActionListener{ JButtonbtn=newJButton("OK"); ... publicMyFrm(){ ... btn.addActionListner(this); ... } publicvoidactionPerformed(ActionEvente){ .... if(e.getSource()==btn){ } } } Java事件处理示例: importjavax.swing.*; importjava.awt.*; importjava.awt.event.*; publicclassMyFrmextendsJFrameimplementsActionListener{ /*界面中需要的组件作为属性声明*/ JTextFieldtxt=newJTextField(10); JLabellbl=newJLabel("姓名"); JButtonbtn=newJButton("查询"); /*组件在构造函数中放置在窗体JFrame上*/ publicMyFrm(){ /*获取JFrame的内容面板ContentPane,控件放在该面板上*/ JPaneljp=(JPanel)this.getContentPane(); /*设置面板布局Layout,如何放? */ jp.setLayout(newFlowLayout()); jp.add(lbl); jp.add(txt); jp.add(btn); /*为事件源加一个侦听者*/ btn.addActionListener(this); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } /*事件处理方法*/ publicvoidactionPerformed(ActionEvente){ JOptionPane.showMessageDialog(this,"btn点击"); } publicstaticvoidmain(Stringarg[]){ JFrame.setDefaultLookAndFeelDecorated(true);//设置纯Java样式 MyFrmfrm=newMyFrm(); frm.setSize(400,300); frm.setVisible(true); } } 注意: 在做下列题目前仔细阅读第一个示例,彻底弄懂Swing界面设计 §5.4实践编程 1、调试运行Swing示例 2、调试运行Java事件处理示例 3、依据题1、2,编写如下界面 当用户点击”ClickMe”按钮,显示消息对话框,消息为”ClickMe按钮被点击”。 packageEx5_3; importjavax.swing.*; importjava.awt.*; importjava.awt.event.*; publicclassMyFrmextendsJFrameimplementsActionListener{ JButtonbtn=newJButton("ClickMe"); publicMyFrm(){ JPaneljp=(JPanel)this.getContentPane(); jp.setLayout(newFlowLayout()); jp.add(btn); btn.addActionListener(this); this.setTitle("ActionEvent"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } publicvoidactionPerformed(ActionEvente){ JOptionPane.showMessageDialog(this,"ClickMe按钮被点击"); } } packageEx5_3; importjavax.swing.*; importjava.awt.*; importjava.awt.event.*; publicclassTest{ publicstaticvoidmain(String[]args){ MyFrmfrm=newMyFrm(); frm.setSize(400,300); frm.setVisible(true); } } 4、编写一个程序实现用户登录界面 当用户登录按下确定键,判断用户是否录入了用户名与密码,如果没有按或用户名不为admin密码不为1234都需要提示错误。 packageEx5_4; importjavax.swing.*; importjava.awt.*; importjava.awt.event.*; publicclassMyFrmextendsJFrameimplementsActionListener{ JLabellbl_name=newJLabel("用户名"); JLabellbl_pwd=newJLabel("密码"); JTextFieldtxt_name=newJTextField(); JPasswordFieldtxt_pwd=newJPasswordField(); JButtonbtn_OK=newJButton("登陆"); JButtonbtn_Cancel=newJButton("取消"); publicMyFrm(){ JPaneljp=(JPanel)this.getContentPane(); jp.setLayout(newGridLayout(3,2,10,5)); jp.add(lbl_name);jp.add(txt_name); jp.add(lbl_pwd);jp.add(txt_pwd); jp.add(btn_OK);jp.add(btn_Cancel); this.setTitle("登陆"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); btn_OK.addActionListener(this); } publicvoidactionPerformed(ActionEvente){ Stringuname=txt_name.getText(); Stringupwd=txt_pwd.getText(); if(uname.equals("")||upwd.equals("")) JOptionPane.showMessageDialog(this,"用户名或密码未输入! "); elseif(! uname.equals("admin")||! upwd.equals("1234")) JOptionPane.showMessageDialog(this,"用户名或密码输入错误! "); elseJOptionPane.showMessageDialog(this,"用户名或密码输入正确! "); } } packageEx5_4; importjavax.swing.*; importjava.awt.*; importjava.awt.event.*; publicclassTest{ publicstaticvoidmain(String[]args){ JFrame.setDefaultLookAndFeelDecorated(true); MyFrmfrm=newMyFrm(); frm.setSize(400,200); frm.setVisible(true); } } 5、利用合适的布局和Swing控件完成下题 按照界面使用相应控件与合适的布局完成下题,要求按生成随机数按纽产生三个随机整数0到100之间,按计算平均数按纽计算平均值,如图所示,初始界面 参考: intx=(Math.random()*41+60);产生随机数 txt1.setText(x+””);将随机数赋予文本框txt1 packageEx5_5; importjavax.swing.*; importjava.awt.*; importjava.awt.event.*; publicclassMyFrmextendsJFrameimplementsActionListener{ JLabellbl=newJLabel("随机数字一"); JLabellb2=newJLabel("随机数字二"); JLabellb3=newJLabel("随机数字三"); JLabellb4=newJLabel("三数平均值"); JTextFieldtxt1=newJTextField(); JTextFieldtxt2=newJTextField(); JTextFieldtxt3=newJTextField(); JTextFieldtxt4=newJTextField(); JButtonbtn_start=newJButton("生成随机数"); JButtonbtn_average=newJButton("计算平均数"); publicMyFrm(){ JPaneljp=(JPanel)this.getContentPane(); jp.setLayout(newGridLayout(5,2,10,5)); jp.add(lbl);jp.add(txt1); jp.add(lb2);jp.add(txt2); jp.add(lb3);jp.add(txt3); jp.add(lb4);jp.add(txt4); jp.add(btn_start);jp.add(btn_average); this.setTitle("三个随机数字"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); btn_start.addActionListener(this); btn_average.addActionListener(this); } publicvoidactionPerformed(ActionEvente){ if(e.getSource()==btn_start){ intx=(int)(Math.random()*41+60); txt1.setText(x+""); inty=(int)(Math.random()*41+60); txt2.setText(y+""); intz=(int)(Math.random()*41+60); txt3.setText(z+""); } if(e.getSource()==btn_average){ doublex=Double.valueOf(txt1.getText()); doubley=Double.valueOf(txt2.getText()); doublez=Double.valueOf(txt3.getText()); doublet=(x+y+z)/3; txt4.setText(t+""); } } } packageEx5_5; importjavax.swing.*; importjava.awt.*; importjava.awt.event.*; publicclassTest{ publicstaticvoidmain(String[]args){ MyFrmfrm=newMyFrm(); frm.setSize(400,200); frm.setVisible(true); } } 6.编写程序实现如下界面,实现事件如果按下座位i就在控制台中显示“座位i被选中”例如按下“座位0“,则输出座位0被选中” packageEx5_6; importjavax.swing.*; importjava.awt.*; importjava.awt.event.*; publicclassMyFrmextendsJFrameimplementsActionListener{ JButtonbtn0=newJButton("讲台"); JButtonbtn1=newJButton("座位1"); JButtonbtn2=newJButton("座位2"); JButtonbtn3=newJButton("座位3"); JButtonbtn4=newJButton("座位4"); JButtonbtn5=newJButton("座位5"); JButtonbtn6=newJButton("座位6"); publicMyFrm(){ JPaneljp=(JPanel)this.getContentPane(); JPaneljp1=newJPanel(); jp1.setLayout(newGridLayout()); jp1.add(btn0); jp.add(jp1,BorderLayout.NORTH); JPaneljp2=newJPanel(); jp2.setLayout(newGridLayout(2,3)); jp2.add(btn1);jp2.add(btn2);jp2.add(btn3); jp2.add(btn4);jp2.add(btn5);jp2.add(btn6); jp.add(jp2,BorderLayout.CENTER); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); btn1.addActionListener(this); btn2.addActionListener(this); btn3.addActionListener(this); btn4.addActionListener(this); btn5.addActionListener(this); btn6.addActionListener(this); } publicvoidactionPerformed(ActionEvente){ if(e.getSource()==btn1) System.out.println("座位1被选中"); if(e.getSource()==btn2) System.out.println("座位2被选中"); if(e.getSource()==btn3) System.
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验五 Java图形用户界面设计 实验 Java 图形 用户界面 设计
![提示](https://static.bdocx.com/images/bang_tan.gif)