欢迎来到冰点文库! | 帮助中心 分享价值,成长自我!
冰点文库
全部分类
  • 临时分类>
  • IT计算机>
  • 经管营销>
  • 医药卫生>
  • 自然科学>
  • 农林牧渔>
  • 人文社科>
  • 工程科技>
  • PPT模板>
  • 求职职场>
  • 解决方案>
  • 总结汇报>
  • ImageVerifierCode 换一换
    首页 冰点文库 > 资源分类 > DOCX文档下载
    分享到微信 分享到微博 分享到QQ空间

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

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

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

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

    1、面向对象程序设计实验报告java实验报告图形用户界面 图形用户界面设计实验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. 运行程序后出现如下界面:改变窗口的大

    2、小,观察到:(见下图)(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 co

    3、lor 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);subPa

    4、nel3.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

    5、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 pa

    6、nel 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 (

    7、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

    8、.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

    9、 (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)

    10、; 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.

    11、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); set

    12、DefaultCloseOperation (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 C

    13、ounter 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 ho

    14、w 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

    15、 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./*packag

    16、e 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 fo

    17、r 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); setPrefer

    18、redSize(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 acti

    19、onPerformed(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 imp

    20、lements ActionListener private 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 S

    21、am); 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); VoteCounterPa

    22、nel.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+; lab

    23、elJoe.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

    24、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)2 Files BMI.java and BMIPanel.java contain s

    25、keletons 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

    26、;/* /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,

    27、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

    28、 BMI); /create a result label to hold the BMI value resultLabel=new JLabel(); /create a JText Field to hold the persons height in inches /create a JText Field to hold the persons weight in pounds height=new JTextField(5);weight=new JTextField(5); /create a button to press to calculate BMI calculate=

    29、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 panel add(heightLabel);add(height); /add the weight label and weight textfield to the panel


    注意事项

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

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




    关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

    copyright@ 2008-2023 冰点文库 网站版权所有

    经营许可证编号:鄂ICP备19020893号-2


    收起
    展开