面向对象程序设计实验报告java实验报告图形用户界面.docx
《面向对象程序设计实验报告java实验报告图形用户界面.docx》由会员分享,可在线阅读,更多相关《面向对象程序设计实验报告java实验报告图形用户界面.docx(18页珍藏版)》请在冰豆网上搜索。
面向对象程序设计实验报告java实验报告图形用户界面
图形用户界面设计
实验1
NestedPanels
TheprogramNestedPanels.javaisfromListing3.8ofthetext.Savetheprogramtoyourdirectoryanddothefollowing:
1.Compileandruntheprogram.Experimentwithresizingtheframeandobservetheeffectonthecomponents.
运行程序后出现如下界面:
改变窗口的大小,观察到:
(见下图)
(1)两个子面板one和two的尺寸都保持不变。
(2)蓝色的主面板随着窗口的变大而扩展。
(3)蓝色面板变长,one和two子面板都不变,当蓝色面板变宽时,两个子面板随着它移动,并保持居中状态。
(4)缩小窗口,根据流式布局的形式,two子面板因为位置容不下,自动被放在下一行的位置。
2.Modifytheprogrambyaddingathirdsubpanelthatistwiceaswide,butthesameheight,astheothertwosubpanels.Chooseyourownlabelandcolorforthesubpanel(thecolorshouldnotbered,green,orblue).Addthepaneltotheprimarypanelaftertheothertwopanels.
修改的代码如下:
JPanelsubPanel3=newJPanel();
subPanel3.setPreferredSize(newDimension(300,100));
ubPanel3.setBackground(Color.red);
JLabellabel3=newJLabel("Three");
subPanel3.add(label3);
primary.add(subPanel3);
3.Compileandrunthemodifiedprogram.Again,experimentwithresizingtheframeandobservetheeffectonthecomponents.
改变窗口的大小,3个子面板变化情况跟第一步实验的类似。
4.Nowaddastatementtotheprogramtosetthepreferredsizeoftheprimarypanelto320by260.(Whatwouldbethe
purposeofthis?
).Compileandruntheprogramtoseeifanythingchanged.
代码修改:
primary.setPreferredSize(newDimension(320,260));
这一步是运行时让主面板的大小固定为宽度320,高度260。
5.Nowaddanotherpanelwithbackgroundcolorblueandsize320by20.Adda"MyPanels"labeltothispaneland
thenaddthispaneltotheprimarypanelbeforeaddingtheotherpanels.Compileandruntheprogram.Whatwasthe
effectofthispanel?
代码如下:
JPanelsubPanel4=newJPanel();
subPanel4.setPreferredSize(newDimension(320,20));
subPanel4.setBackground(Color.blue);
JLabellabel4=newJLabel("MyPanel");
subPanel4.add(label4);
……………………………
primary.add(subPanel4);
primary.add(subPanel1);
primary.add(subPanel2);
primary.add(subPanel3);
因为蓝色主面板的宽320,由于流式布局,一个一个把面板加到主面板,MyPanel已经占据了320,所以one面板只能去到下一行。
同理得Two,Three的布局形式。
6、用可重用的思想编写该界面:
packagelab3;
importjava.awt.Color;
importjava.awt.Dimension;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
publicclassNestedPanels1extendsJFrame
{
//--------------------------------------------------------
//Presentstwocoloredpanelsnestedwithinathird.
//--------------------------------------------------------
JPanelsubPanel1,subPanel2,subPanel3,subPanel4,primary;
JLabellabel1,label2,label3,label4;
publicNestedPanels1(){
label1=newJLabel("One");
label2=newJLabel("Two");
label3=newJLabel("Three");
label4=newJLabel("MyPanel");
subPanel1=newJPanel();
subPanel2=newJPanel();
subPanel3=newJPanel();
subPanel4=newJPanel();
primary=newJPanel();
subPanel1.setPreferredSize(newDimension(150,100));
subPanel2.setPreferredSize(newDimension(150,100));
subPanel3.setPreferredSize(newDimension(300,100));
subPanel4.setPreferredSize(newDimension(320,20));
primary.setPreferredSize(newDimension(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);
}
publicstaticvoidmain(String[]args)
{
NestedPanels1NP=newNestedPanels1();
NP.setTitle("NestedPanels");//设置窗口的名称
}
}
实验2
VotingwithButtons
FilesVoteCounter.javaandVoteCounterPanel.javacontainslightlymodifiedversionsofPushCounter.javaandPushCounterPanel.javainlistings4.10and4.11ofthetext.Asinthetexttheprogramcountsthenumberoftimesthebuttonispushed;however,itassumes(“pretends”)eachpushisavoteforJoesothebuttonandvariableshavebeenrenamedappropriately.
1.Compiletheprogram,thenrunittoseehowitworks.
每次点击按钮一次,会显示Joe的得票数:
2.Modifytheprogramsothattherearetwocandidatestovotefor—JoeandSam.Todothisyouneedtodothefollowing:
a.AddvariablesforSam—avotecounter,abutton,andalabel.
b.AddanewinnerclassnamedSamButtonListenertolistenforclicksonthebuttonforSam.InstantiateaninstanceoftheclasswhenaddingtheActionListenertothebuttonforSam.
c.AddthebuttonandlabelforSamtothepanel.
代码如下:
//************************************************************
//VoteCounterPanel.java
//Demonstratesagraphicaluserinterfaceandeventlistenersto
//tallyvotesfortwocandidate