面向对象程序设计实验报告java实验报告图形用户界面.doc
《面向对象程序设计实验报告java实验报告图形用户界面.doc》由会员分享,可在线阅读,更多相关《面向对象程序设计实验报告java实验报告图形用户界面.doc(14页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、图形用户界面设计实验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、尺寸都保持不变。(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 c
3、olor 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.a
4、dd (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 thi
5、s?). 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 be
6、fore 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 (subPan
7、el4);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.
8、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);l
9、abel4 = 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(30
10、0, 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.
11、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
12、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
13、 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 ther
14、e 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 Act
15、ion 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.
16、 *;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 JoeButtonLi
17、stener();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 JoeButtonLis
18、tener 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: + votesFor
19、Sam); 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,votesForS
20、am;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:
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 面向 对象 程序设计 实验 报告 java 图形 用户界面
限制150内