欢迎来到淘文阁 - 分享文档赚钱的网站! | 帮助中心 好文档才是您的得力助手!
淘文阁 - 分享文档赚钱的网站
全部分类
  • 研究报告>
  • 管理文献>
  • 标准材料>
  • 技术资料>
  • 教育专区>
  • 应用文书>
  • 生活休闲>
  • 考试试题>
  • pptx模板>
  • 工商注册>
  • 期刊短文>
  • 图片设计>
  • ImageVerifierCode 换一换

    面向对象程序设计实验报告java实验报告图形用户界面.doc

    • 资源ID:35241299       资源大小:44KB        全文页数:14页
    • 资源格式: DOC        下载积分:15金币
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录   QQ登录  
    二维码
    微信扫一扫登录
    下载资源需要15金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    面向对象程序设计实验报告java实验报告图形用户界面.doc

    图形用户界面设计实验1Nested Panels The program Nested Panels.java is from Listing 3.8 of the text. Save the program to your directory and do the following: 1. Compile and run the program. Experiment with resizing the frame and observe the effect on the components. 运行程序后出现如下界面:改变窗口的大小,观察到:(见下图)(1)两个子面板one和two的尺寸都保持不变。(2)蓝色的主面板随着窗口的变大而扩展。(3)蓝色面板变长,one和two子面板都不变,当蓝色面板变宽时,两个子面板随着它移动,并保持居中状态。(4)缩小窗口,根据流式布局的形式,two子面板因为位置容不下,自动被放在下一行的位置。2. Modify the program by adding a third subpanel that is twice as wide, but the same height, as the other two subpanels. Choose your own label and color for the subpanel (the color should not be red, green, or blue). Add the panel to the primary panel after the other two panels. 修改的代码如下:JPanel subPanel3 = new JPanel() ; subPanel3.setPreferredSize (new Dimension(300, 100);ubPanel3.setBackground (Color.red);JLabel label3 = new JLabel ("Three");subPanel3.add (label3);primary.add (subPanel3);3. Compile and run the modified program. Again, experiment with resizing the frame and observe the effect on the components. 改变窗口的大小,3个子面板变化情况跟第一步实验的类似。4. Now add a statement to the program to set the preferred size of the primary panel to 320 by 260. (What would be the purpose of this?). Compile and run the program to see if anything changed. 代码修改:primary.setPreferredSize (new Dimension(320, 260);这一步是运行时让主面板的大小固定为宽度320,高度260。5. Now add another panel with background color blue and size 320 by 20. Add a "My Panels" label to this panel and then add this panel to the primary panel before adding the other panels. Compile and run the program. What was the effect of this panel? 代码如下:JPanel subPanel4 = new JPanel() ; subPanel4.setPreferredSize (new Dimension(320, 20);subPanel4.setBackground (Color.blue);JLabel label4 = new JLabel ("MyPanel");subPanel4.add (label4);primary.add (subPanel4);primary.add (subPanel1);primary.add (subPanel2);primary.add (subPanel3);因为蓝色主面板的宽320,由于流式布局,一个一个把面板加到主面板,MyPanel已经占据了320,所以one面板只能去到下一行。同理得Two,Three的布局形式。6、用可重用的思想编写该界面:package lab3;import java.awt.Color;import java.awt.Dimension;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;public class NestedPanels1 extends JFrame/Presents two colored panels nested within a third.JPanel subPanel1,subPanel2,subPanel3,subPanel4,primary;JLabel label1, label2, label3,label4;public NestedPanels1()label1 = new JLabel ("One");label2 = new JLabel ("Two");label3 = new JLabel ("Three");label4 = new JLabel ("MyPanel");subPanel1 = new JPanel();subPanel2 = new JPanel();subPanel3 = new JPanel() ; subPanel4 = new JPanel() ;primary = new JPanel();subPanel1.setPreferredSize(new Dimension(150, 100);subPanel2.setPreferredSize(new Dimension(150, 100);subPanel3.setPreferredSize(new Dimension(300, 100);subPanel4.setPreferredSize(new Dimension(320, 20);primary.setPreferredSize (new Dimension(320, 260);subPanel1.setBackground (Color.green);subPanel2.setBackground (Color.red);subPanel3.setBackground (Color.red);subPanel4.setBackground (Color.blue);primary.setBackground (Color.blue);subPanel1.add (label1);subPanel2.add (label2);subPanel3.add (label3);subPanel4.add (label4);primary.add (subPanel4);primary.add (subPanel1);primary.add (subPanel2);primary.add (subPanel3);getContentPane().add(primary);pack ();setVisible(true);setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);public static void main (String args) NestedPanels1 NP=new NestedPanels1(); NP.setTitle("Nested Panels"); /设置窗口的名称实验2Voting with Buttons Files Vote Counter.java and Vote Counter Panel.java contain slightly modified versions of Push Counter.java and Push Counter Panel.java in listings 4.10 and 4.11 of the text. As in the text the program counts the number of times the button is pushed; however, it assumes (“pretends”) each push is a vote for Joe so the button and variables have been renamed appropriately. 1. Compile the program, then run it to see how it works. 每次点击按钮一次,会显示Joe的得票数:2. Modify the program so that there are two candidates to vote forJoe and Sam. To do this you need to do the following: a. Add variables for Sama vote counter, a button, and a label. b. Add a new inner class named Sam Button Listener to listen for clicks on the button for Sam. Instantiate an instance of the class when adding the Action Listener to the button for Sam. c. Add the button and label for Sam to the panel. 代码如下:/Vote Counter Panel.java/Demonstrates a graphical user interface and event listeners to/tally votes for two candidates, Joe and Sam.package lab4;import java.awt.*; import java.awt.event.*; import javax. swing. *;public class VoteCounterPanel extends JPanel private int votesForJoe,votesForSam;private JButton joe,Sam;private JLabel labelJoe,labelSam;public VoteCounterPanel() votesForJoe = 0;votesForSam = 0;joe = new JButton("Vote for Joe");Sam = new JButton("Vote for Sam");joe.addActionListener(new JoeButtonListener();Sam.addActionListener(new SamButtonListener();labelJoe = new JLabel("Votes for Joe: " + votesForJoe);labelSam = new JLabel("Votes for Sam: " + votesForSam);add(joe);add(labelJoe); add(Sam);add(labelSam);setPreferredSize(new Dimension(300, 80); setBackground(Color.cyan); private class JoeButtonListener implements ActionListener public void actionPerformed(ActionEvent event) votesForJoe+;labelJoe.setText("Votes for Joe: " + votesForJoe); private class SamButtonListener implements ActionListener public void actionPerformed(ActionEvent event) votesForSam+;labelSam.setText("Votes for Sam: " + votesForSam); 3、Compile and run the program. 点击按钮后:4、以重用的思想实现该界面的代码如下:package lab4;import java.awt.Color;import java.awt.Dimension;import java.awt.event.*; import javax.swing.*;public class VoteCounter1 extends JFrame implements ActionListenerprivate JPanel VoteCounterPanel;private int votesForJoe,votesForSam;private JButton joe,Sam;private JLabel labelJoe,labelSam;public VoteCounter1() votesForJoe = 0;votesForSam = 0;VoteCounterPanel=new JPanel();joe = new JButton("Vote for Joe");Sam = new JButton("Vote for Sam");joe.addActionListener(this);Sam.addActionListener(this);labelJoe = new JLabel("Votes for Joe: " + votesForJoe);labelSam = new JLabel("Votes for Sam: " + votesForSam);VoteCounterPanel.add(joe);VoteCounterPanel.add(labelJoe); VoteCounterPanel.add(Sam);VoteCounterPanel.add(labelSam);VoteCounterPanel.setPreferredSize(new Dimension(300, 80); VoteCounterPanel.setBackground(Color.cyan); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);getContentPane().add(VoteCounterPanel);pack();setVisible(true); public void actionPerformed(ActionEvent event) /确定事件源if(event.getSource()=joe)votesForJoe+;labelJoe.setText("Votes for Joe: " + votesForJoe); if(event.getSource()=Sam)votesForSam+;labelSam.setText("Votes for Sam: " + votesForSam); public static void main(String args) VoteCounter1 VC1= new VoteCounter1();VC1.setTitle("Vote Counter"); /设置窗口的名称实验3Calculating Body Mass Index Body Mass Index (BMI) is measure of weight that takes height into account. Generally, a BMI above 25 is considered high, that is, likely to indicate that an individual is overweight. BMI is calculated as follows for both men and women: (703 * height in inches) / (weight in pounds)2Files BMI.java and BMIPanel.java contain skeletons for a program that uses a GUI to let the user compute their BMI. This is similar to the Fahrenheit program in listings 4.12 and 4.13 of the text. Fill in the code as indicated by the comments and compile and run this program; you should see the BMI calculator displayed. 填写的代码如下:package lab4;/BMIPanel.java /Computes body mass index in a GUI.import java.awt.*; import java.awt.event.*; import javax.swing.*;public class BMIPanel extends JPanel private int WIDTH = 280; private int HEIGHT = 120; private JLabel heightLabel, weightLabel, BMILabel,resultLabel; private JTextField height, weight; private JButton calculate;/ Sets up the GUI.public BMIPanel() /create labels for the height and weight textfields heightLabel = new JLabel ("Your height in inches: "); weightLabel = new JLabel ("Your weight in pounds: "); /create a "this is your BMI" label BMILabel=new JLabel ("this is your BMI"); /create a result label to hold the BMI value resultLabel=new JLabel();/create a JText Field to hold the person's height in inches /create a JText Field to hold the person's weight in pounds height=new JTextField(5);weight=new JTextField(5);/create a button to press to calculate BMI calculate=new JButton("Calculate");/create a BMIListener and make it listen for the button to be pressed BMIListener BL=new BMIListener();calculate.addActionListener(BL);/add the height label and height textfield to the paneladd(heightLabel);add(height);/add the weight label and weight textfield to the panel add(weightLabel); add(weight);/add the button to the panel add(calculate);/add the BMI label to the panel add(calculate);/add the label that holds the result to the panel add( resultLabel);/set the size of the panel to the WIDTH and HEIGHT constants setPreferredSize(new Dimension(WIDTH,HEIGHT);/set the color of the panel to whatever you choose this.setBackground(Color.cyan);/ Represents an action listener for the calculate button.private class BMIListener implements ActionListener / Compute the BMI when the button is pressedpublic void actionPerformed(ActionEvent event) String heightText, weightText; int heightVal, weightVal; double bmi;/get the text from the height and weight textfieldsheightText=height.getText();weightText=weight.getText();/Use Integer.parse Int to convert the text to integer values heightVal=Integer.parseInt(heightText);weightVal=Integer.parseInt(weightText);/Calculate the bmi = 703 * weight in pounds / (height in inches)2 bmi =( 703 * weightVal) / (heightVal*heightVal);/Put result in result label. Use Double.to String to convert double to string.resultLabel.setText("Your BMI is : "+Double.toString(bmi);运行时:输入数字后:用可重用的思想编写该界面:package lab4;/BMIPanel.java /Computes body mass index in a GUI.import java.awt.*; import java.awt.event.*; import javax.swing.*;public class BMI1 extends JFrame implements ActionListener private int WIDTH = 280; private int HEIGHT = 120; private JPanel BMIPanel;private JLabel heightLabel, weightLabel, BMILabel,resultLabel; private JTextField height, weight; private JButton calculate;/- / Sets up the GUI. public BMI1() BMIPanel =new JPanel();heightLabel = new JLabel ("Your height in inches: "); weightLabel = new JLabel ("Your weight in pounds: "); /create a "this is your BMI" label BMILabel=new JLabel ("this is your BMI"); /create a result label to hold the BMI value resultLabel=new JLabel();/create a JText Field to hold the person's height in inches /create a JText Field to hold the person's weight in pounds height=new JTextField(5);weight=new JTextField(5);/create a button to press to calculate BMI calculate=new JButton("Calculate"); calculate.addActionListener(this);/add the height label and height textfield to the panel BMIPanel.add(heightLabel); BMIPanel.add(height);/add the weight label and weight textfield to the panel BMIPanel.add(weightLabel); BMIPanel.add(weight);/add the button to the panel BMIPanel.add(calculate);/add the BMI label to the panel BMIPanel.add(calculate);/add the label that holds the result to the panel BMIPanel.add( resultLabel);/set the size of the panel to the WIDTH and HEIGHT constants BMIPanel.setPreferredSize(new Dimension(WIDTH,HEIGHT);/set the color of the panel to whatever you choose BMIPanel.setBackground(Color.cyan); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().add(BMIPanel);pack();setVisible(true); / Compute the BMI when the button is pressedpublic void actionPerformed(ActionEvent event) String heightText, weightText; int heightVal, weightVal; double bmi;/get the text from the height and weight textfieldsheightText=height.getText();weightText=weight.getText();/Use Integer.parse Int to convert the text to integer values heightVal=Integer.parseInt(heightText);weightVal=Integer.parseInt(weightText);/Calculate the bmi = 703 * weight in pounds / (height in inches)2 bmi =( 703 * weightVal) / (heightVal*heightVal);/Put result in result label. Use Double.to String to convert double to string.resultLabel.setText("Your BMI is : "+Double.toString(bmi);public static void main (String args) BMI1 B1 = new BMI1();B1.setTitle("BMI");

    注意事项

    本文(面向对象程序设计实验报告java实验报告图形用户界面.doc)为本站会员(叶***)主动上传,淘文阁 - 分享文档赚钱的网站仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知淘文阁 - 分享文档赚钱的网站(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于淘文阁 - 版权申诉 - 用户使用规则 - 积分规则 - 联系我们

    本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

    工信部备案号:黑ICP备15003705号 © 2020-2023 www.taowenge.com 淘文阁 

    收起
    展开