《实验三 Java图形用户界面设计.docx》由会员分享,可在线阅读,更多相关《实验三 Java图形用户界面设计.docx(16页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、实验三 Java图形用户界面设计 实验报告 课程名称:Java程序设计 实验项目:实验三Java图形用户界面设计 系别_理学院_ 专业/班级_信计1302_ 姓名_ 实验日期_2022.5.5_ 成绩_ 指导教师_ 一、实验题目:实验三Java图形用户界面 二、实验内容: (1)创建一个窗口 1)窗口有标题“Exercise 2)在窗口内从文本框输入用户名,密码,并选择”确定“按钮。如果用户名为“abc”,密码为“123”,则提示“用户名密码正确”,否则输入”用户名或密码错误“。 (2)创建一个窗口 1)窗口有“退出”菜单,选择后可退出程序 窗口内有“提示”菜单,单击后弹出信息框,显示自己的班
2、级、学号、姓名 2)在窗口内实现一个贷款计算器,程序,从键盘上输入年利率、贷款额、贷款年数,输出月还款额以及总的还款额。 要求: 1)创建贷款类来模拟贷款的属性和行为,如具有年利率、贷款年限、贷款额等属性,以及计算月利率、计算月还款额、总还款额等行为。 2)创建实际的应用类(包含main方法),生成图形界面,并调用贷款类的功能。 三、程序清单及结果:(需要标明实现程序对应的实验内容,并给出合理的程序注释及运行结果) 1. 1)程序清单及注释 importjavax.swing.*; importjava.awt.event.*; importjava.awt.*; public class l
3、and extends JFrame implements ActionListener JTextField username = new JTextField(15);/定义用户名 JPasswordField password = new JPasswordField(15);/定义密码 JButton ok = new JButton(确定);/定义按钮 JLabelmessLabel = new JLabel( );/定义显示框 GridBagLayoutgridbag = new GridBagLayout();/定义布局 public land() super(Exercise)
4、;/标题 setSize(250,200);/窗口大小 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/退出方式 JLabelusernameLabel = new JLabel(姓名:);/定义姓名: JLabelpasswordLabel = new JLabel(密码:);/定义密码: ok.addActionListener(this);/确定按钮的动作 GridBagConstraints constraints; setLayout(gridbag); addComponent(usernameLabel,0,0,1,1,10,100
5、,GridBagConstraints.NONE,GridBagC onstraints.CENTER);/依次加入各个部件 addComponent(username,1,0,5,1,40,100,GridBagConstraints.NONE,GridBagConstr aints.CENTER); addComponent(passwordLabel,0,1,1,1,10,100,GridBagConstraints.NONE,GridBagC onstraints.CENTER);/依次加入各个部件 addComponent(password,1,1,5,1,40,100,GridBa
6、gConstraints.NONE,GridBagConstr aints.CENTER); addComponent(messLabel,1,2,1,1,100,100,GridBagConstraints.NONE,GridBagCons traints.CENTER);/依次加入各个部件 addComponent(ok,1,3,3,1,10,100,GridBagConstraints.NONE,GridBagConstraints.C ENTER); setVisible(true);/显示粗来 public void actionPerformed(ActionEventevt)/按
7、钮的动作 String user = abc;/用户名 Object source = evt.getSource(); char pass = password.getPassword();/密码 if (source =ok) if(username.getText().equals(user) &isPasswordCorrect(pass)/如果用户名和密码正确 messLabel.setText(用户名密码正确); else messLabel.setText(用户名密码错误); repaint();/重绘 private static booleanisPasswordCorrec
8、t(char inputPassword) /验证密码char actualPassword = 1, 2, 3 ;/真实的密码 if (inputPassword.length != actualPassword.length)/判断长度是否相同return false; / 长度不同,密码错误 for (inti = 0; iinputPassword.length; i+) if (inputPasswordi != actualPasswordi)/挨个比较 return false;/有不一样的就退出 return true; private void addComponent(Co
9、mponent component , intgridx, intgridy, intgridwidth, intgridheight ,intweightx, int weighty, int fill, int anchor) GridBagConstraints constraints = new GridBagConstraints(); constraints.gridx = gridx; constraints.gridy = gridy; constraints.gridwidth = gridwidth; constraints.gridheight = gridheight;
10、 constraints.weightx = weightx; constraints.weighty = weighty; constraints.fill = fill; constraints.anchor = anchor; gridbag.setConstraints(component, constraints); add(component); public static void main(String args) landts = new land(); 2)程序运行结果 2. 1)程序清单及注释 importjava.awt.*; importjava.awt.event.
11、*; importjavax.swing.*; public class LoanCalculator extends JFrame implements ActionListener ,FocusListener /按钮 JButton quit = new JButton(退出); JButton tip = new JButton(提示); /文本框 JTextFieldannualratetext = new JTextField(4);/年利率 JTextFieldyeartext = new JTextField(4);/贷款年限 JTextFieldloantext = new
12、JTextField(4);/贷款额 JTextFieldmonthrepaytext = new JTextField(4);/月还款额 JTextFieldtotalrepaytext = new JTextField(4);/总还款额 GridBagLayoutgridbag = new GridBagLayout();/定义布局 class Loan/定义贷款类 floatannualrate; float year; float loan; floatmonthrepay; floattotalrepay; Loan(float a,float y, float l)/构造函数 an
13、nualrate=a; year=y; loan=l; monthrepay=0; totalrepay=l; for (inti=0;iyear;i+) totalrepay=(1+annualrate)*totalrepay;/计算 monthrepay=totalrepay/12/year; public float getMonthrepay()/月还款额接口 returnmonthrepay; public float getTotalrepay()/总还款额接口 returntotalrepay; publicLoanCalculator() super(贷款计算器);/标题 se
14、tSize(250,200);/窗口大小 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/退出方式 /确定按钮的动作 quit.addActionListener(this); tip.addActionListener(this); /用户输入 annualratetext.addFocusListener(this); yeartext.addFocusListener(this); loantext.addFocusListener(this); /页面布局设置 GridBagConstraints constraints; setLayo
15、ut(gridbag); /输出项不可编辑 monthrepaytext.setEditable(false); totalrepaytext.setEditable(false); /标签 JLabelannualrateLabel = new JLabel( 年利率);/年利率 JLabelyearLabel = new JLabel(贷款年限);/贷款年限 JLabelloanLabel = new JLabel( 贷款额);/贷款额 JLabelmonthrepayLabel = new JLabel(月还款额);/月还款额 JLabeltotalrepayLabel = new JL
16、abel(总还款额);/总还款额 /设计布局 addComponent(annualrateLabel,0,0,1,1,10,100,GridBagConstraints.NONE,GridBag Constraints.CENTER); addComponent(annualratetext,1,0,9,1,90,100,GridBagConstraints.HORIZONTAL,G ridBagConstraints.EAST); addComponent(yearLabel,0,1,1,1,10,100,GridBagConstraints.NONE,GridBagConstr aint
17、s.CENTER); addComponent(yeartext,1,1,5,1,90,100,GridBagConstraints.HORIZONTAL,GridBa gConstraints.EAST); addComponent(loanLabel,0,2,1,1,10,100,GridBagConstraints.NONE,GridBagConstr aints.CENTER); addComponent(loantext,1,2,5,1,90,100,GridBagConstraints.HORIZONTAL,GridBa gConstraints.CENTER); addCompo
18、nent(monthrepayLabel,0,3,1,1,10,100,GridBagConstraints.NONE,GridBa gConstraints.CENTER); addComponent(monthrepaytext,1,3,5,1,90,100,GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER); addComponent(totalrepayLabel,0,4,1,1,10,100,GridBagConstraints.NONE,GridBag Constraints.CENTER); addComponent
19、(totalrepaytext,1,4,5,1,40,100,GridBagConstraints.HORIZONTAL,Gr idBagConstraints.CENTER); /按钮位置 addComponent(quit,1,5,1,1,30,100,GridBagConstraints.NONE,GridBagConstraints. CENTER); addComponent(tip,0,5,1,1,30,100,GridBagConstraints.NONE,GridBagConstraints.C ENTER); setVisible(true);/显示粗来 private vo
20、id addComponent(Component component , intgridx, intgridy, intgridwidth, intgridheight ,intweightx, int weighty, int fill, int anchor) /窗口设计 GridBagConstraints constraints = new GridBagConstraints(); constraints.gridx = gridx; constraints.gridy = gridy; constraints.gridwidth = gridwidth; constraints.
21、gridheight = gridheight; constraints.weightx = weightx; constraints.weighty = weighty; constraints.fill = fill; constraints.anchor = anchor; gridbag.setConstraints(component, constraints); add(component); public void actionPerformed(ActionEventevt)/按钮的动作 Object source = evt.getSource(); if (source =
22、tip) JOptionPane.showMessageDialog(null,班级:信计1302n学号:1111111111n姓名:纳尼); if(source =quit) System.exit(0); repaint();/重绘 public void focusGained(FocusEvent event)/文本框的动作 try float at=Float.parseFloat(annualratetext.getText(); floatlt=Float.parseFloat(loantext.getText(); floatyt=Float.parseFloat(yearte
23、xt.getText(); Loan loant = new Loan(at,yt,lt); floatmt=loant.getMonthrepay(); floattt=loant.getTotalrepay(); monthrepaytext.setText(+mt); totalrepaytext.setText(+tt); catch(NumberFormatExceptionnfe) annualratetext.setText(0.1); loantext.setText(1); yeartext.setText(3); public void focusLost(FocusEvent event) focusGained(event); public static void main(String args) LoanCalculator ts2 = new LoanCalculator();/不解释 2)程序运行结果 四、程序调试报告及总结 (调试中遇到的问题及解决方法,并说明实验过程中对Java知识点的认识及收获等)