《面向对象程序设计实验报告java实验报告图形用户界面.docx》由会员分享,可在线阅读,更多相关《面向对象程序设计实验报告java实验报告图形用户界面.docx(12页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、面向对象程序设计实验报告java实验报告图形用户界面 图形用户界面设计 实验1 Nested Panels The program Nested Panels.java is from Listing 3.8 of the text. Save the program to your directory and do the following: pile 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
3、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);
4、 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 26
5、0. (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
6、 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);
7、 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 java
8、x.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
9、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.setPref
10、erredSize(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)
11、; 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
12、); pack (); setVisible(true); setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); public static void main (String args) NestedPanels1 NP=new NestedPanels1(); NP.setTitle(Nested Panels); /设置窗口的名称 实验2 Voting with Buttons Files Vote Counter.java and Vote Counter Panel.java contain slightly modified versio
13、ns 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
14、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
15、 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
16、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
17、; 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(l
18、abelJoe); 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 SamButtonListene
19、r 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 ActionListener private JPanel VoteCounterPanel; private int votesForJoe,votesForSam; private JButton joe,Sam; private JLabel labelJoe,labelSam; public VoteCounter1() votesForJoe = 0;