Java实验报告3Word格式.docx
《Java实验报告3Word格式.docx》由会员分享,可在线阅读,更多相关《Java实验报告3Word格式.docx(17页珍藏版)》请在冰豆网上搜索。
(1).掌握窗体的创建以及几种常用方法。
(2).掌握按钮的创建以及动作监听。
(3).掌握面板、文本组建、选择框的应用。
(4).熟练掌握布局的应用。
(5).熟悉掌握应用类Canvas创建画布对象。
(6).掌握菜单、菜单项的创建以及使用菜单的技巧。
(7).掌握树结构的应用。
2.实验内容:
(1).创建窗体。
(2).创建按钮。
(3).创建面板、文本框、选择框。
(4).创建画布对象。
(5).创建菜单、菜单项。
3.实验步骤与方法:
(1).新建文本文档用记事本打开并编写代码。
(2).打开dos命令栏编译,运行。
(3).出现错误,回到原代码进行改错,并保存。
(4).重新编译,运行。
4.实验数据与程序清单:
实验室:
机号:
实验日期:
年月日
延安大学计算机学院实验报告附页
实验名称
实验一:
运行下列程序,并写出其输出结果
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
publicclassEx5_1extendsJFrameimplementsActionListener
{JTextFieldtext;
JButtonbuttonEnter,buttonQuit;
JLabelstr;
Ex5_1()
{
setBounds(100,100,300,200);
setVisible(true);
setLayout(newFlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
str=newJLabel("
在文本框输入数字字符回车或单击按钮"
);
add(str);
text=newJTextField("
0"
10);
add(text);
buttonEnter=newJButton("
确定"
buttonQuit=newJButton("
清除"
add(buttonEnter);
add(buttonQuit);
validate();
buttonEnter.addActionListener(this);
buttonQuit.addActionListener(this);
text.addActionListener(this);
}
publicvoidactionPerformed(ActionEvente)
{if(e.getSource()==buttonEnter||e.getSource()==text)
{doublenumber=0;
try{number=Double.valueOf(text.getText()).doubleValue();
text.setText("
"
+Math.sqrt(number));
catch(NumberFormatExceptionevent)
{text.setText("
请输入数字字符"
elseif(e.getSource()==buttonQuit)
publicstaticvoidmain(Stringargs[])
{newEx5_1();
实验二:
运行下列程序,分析其输出结果
publicclassEx5_2extendsJFrameimplementsActionListener
{JTextFieldtext1,text2;
JPasswordFieldpasstext;
Ex5_2()
setSize(300,200);
text1=newJTextField("
输入密码:
text1.setEditable(false);
passtext=newJPasswordField(10);
passtext.setEchoChar('
*'
text2=newJTextField("
我是一个文本框"
20);
add(text1);
add(passtext);
add(text2);
passtext.addActionListener(this);
text2.setText("
设置的密码为:
+passtext.getText());
newEx5_2();
实验三:
运行下列程序,画出其输出结果
classMyFrameextendsJFrameimplementsItemListener,ActionListener
{JCheckBoxbox;
JTextAreatext;
JButtonbutton;
MyFrame(Strings)
{super(s);
box=newJCheckBox("
设置窗口是否可调整大小"
text=newJTextArea(12,12);
button=newJButton("
关闭窗口"
button.addItemListener(this);
box.addItemListener(this);
add(text,BorderLayout.CENTER);
add(box,BorderLayout.SOUTH);
add(button,BorderLayout.NORTH);
setResizable(false);
publicvoiditemStateChanged(ItemEvente)
if(box.isSelected()==true)setResizable(true);
elsesetResizable(false);
{dispose();
classEx5_3
{publicstaticvoidmain(Stringargs[])
{newMyFrame("
窗口"
实验四:
classMypanelextendsJPanelimplementsActionListener
JButtonbutton1,button2,button3;
ColorbackColor;
Mypanel()
{button1=newJButton("
button2=newJButton("
取消"
button3=newJButton("
保存"
add(button1);
add(button2);
add(button3);
setBackground(Color.pink);
backColor=getBackground();
button1.addActionListener(this);
button2.addActionListener(this);
{if(e.getSource()==button1)
setBackground(Color.cyan);
elseif(e.getSource()==button2)
setBackground(backColor);
publicclassEx5_4extendsJFrame
{Mypanelpanel1,panel2,panel3;
Ex5_4()
panel1=newMypanel();
panel2=newMypanel();
panel3=newMypanel();
我不在那些面板里"
add(panel1);
add(panel2);
add(panel3);
add(button);
Ex5_4win=newEx5_4();
实验五:
classMycanvasextendsCanvas
Strings;
Mycanvas(Strings)
{this.s=s;
setSize(90,80);
publicvoidpaint(Graphicsg)
{if(s.equals("
circle"
))
g.drawOval(20,25,30,30);
elseif(s.equals("
rect"
g.drawRect(30,35,20,20);
publicclassEx5_5extendsJFrame
Mycanvascanvas1,canvas2;
Ex5_5()
canvas1=newMycanvas("
canvas2=newMycanvas("
add(canvas1);
JPanelp=newJPanel();
p.setBackground(Color.pink);
p.add(canvas2);
add(p);
newEx5_5();
实验六:
publicclassEx5_6extendsJFrame
Ex5_6()
setSize(500,300);
setLayout(newGridLayout(12,12));
JButtonbutton[][]=newJButton[12][12];
for(inti=0;
i<
12;
i++)
{for(intj=0;
j<
j++)
{button[i][j]=newJButton();
if((i+j)%2==0)
button[i][j].setBackground(Color.black);
else
button[i][j].setBackground(Color.white);
add(button[i][j]);
newEx5_6();
实验七:
classHerwindowextendsJFrameimplementsActionListener
{JMenuBarmenubar;
JMenumenu;
JMenuItemitem;
Herwindow(Strings)
{super(s);
setSize(160,170);
menubar=newJMenuBar();
menu=newJMenu("
文件"
item=newJMenuItem("
退出"
item.addActionListener(this);
menu.add(item);
menubar.add(menu);
setJMenuBar(menubar);
{if(e.getSource()==item)
{System.exit(0);
publicclassEx5_7
{Herwindowwindow=newHerwindow("
法制之窗"
实验八:
importjavax.swing.tree.*;
publicclassEx5_8extendsJFrame
{Ex5_8()
{setSize(300,200);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Containercon=getContentPane();
DefaultMutableTreeNoderoot=newDefaultMutableTreeNode("
c:
\\"
DefaultMutableTreeNodet1=newDefaultMutableTreeNode("
dos"
DefaultMutableTreeNodet2=newDefaultMutableTreeNode("
java"
DefaultMutableTreeNodet1_1=newDefaultMutableTreeNode("
wps"
DefaultMutableTreeNodet1_2=newDefaultMutableTreeNode("
epg"
DefaultMutableTreeNodet2_1=newDefaultMutableTreeNode("
applet"
DefaultMutableTreeNodet2_2=newDefaultMutableTreeNode("
jre"
root.add(t1);
root.add(t2);
t1.add(t1_1);
t1.add(t1_2);
t2.add(t2_1);
t2.add(t2_2);
JTreetree=newJTree(root);
DefaultTreeCellRendererrender=newDefaultTreeCellRenderer();
render.setLeafIcon(newImageIcon("
leaf.gif"
));
render.setBackground(Color.yellow);
render.setClosedIcon(newImageIcon("
close.gif"
render.setOpenIcon(newImageIcon("
open.gif"
render.setTextSelectionColor(Color.red);
render.setTextNonSelectionColor(Color.green);
render.setFont(newFont("
TimeRoman"
Font.BOLD,16));
tree.setCellRenderer(render);
JScrollPanescrollPane=newJScrollPane(tree);
con.add(scrollPane);
validate();
{newEx5_8();
5.出现的问题及解决方法:
(1).在程序编译过程中会出现java使用或覆盖已经过时的APN,而无法进行编译。
(2).解决方法:
根据提示在javacEx5_2.java后面加上-Xlint:
deprecation重新编译,然后会发现出现了一个警告,但是并不影响程序的运行。
(3).编译过程会出现非法字符的开始,就说明代码中有大小写,或者拼写错误。
6.体会:
(1).练习了窗体的创建以及几种常用方法。
(2).练习了按钮的创建以及动作监听。
(3).掌握了面板、文本组建、选择框的应用。
(4).熟练了掌握布局的应用。
(5).掌握了应用类Canvas创建画布对象。
(6).掌握了菜单、菜单项的创建以及使用菜单的技巧。
(7).掌握了树结构的应用。
(8).窗体、面板、文本框的创建,代码比较复杂,在编写的过程中一定要仔细,认真。
(9).Java实验一定要多动手编写,记住程序的基本框架,在编写过程中才能提高速度,避免出错。