面向对象程序设计实验报告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");