java实验报告9.docx
《java实验报告9.docx》由会员分享,可在线阅读,更多相关《java实验报告9.docx(22页珍藏版)》请在冰豆网上搜索。
java实验报告9
班级:
1006学号:
20101698姓名:
王桢指导老师:
邓泓
课程名称:
java程序设计实验名称:
图形用户界面实验序号:
9实验地点:
301
正文:
一、实验目的:
1)了解图形用户界面基本组件窗口,按钮,文本框,选择框,滚动条等的使用方法
2)了解如何使用布局管理器对组件对象进行管理,以及如何使用java的事件处理进制
二、实验环境:
Eclipse3.7,jdk1.6,win7
三、实验内容(包括算法描述及实验步骤):
1)创建图形用户界面
【1】在Applet中添加标签,按钮并使用网格布局
代码如下:
//LX9_1.java
importjava.applet.Applet;
importjava.awt.Button;
importjava.awt.GridLayout;
importjava.awt.Label;
publicclassLX9_1extendsApplet{
Labell1;
Buttonb1,b2,b3,b4,b5,b6;
publicvoidinit(){
setLayout(newGridLayout(3,3));
l1=newLabel("labelone");
b1=newButton("buttonone");
b2=newButton("buttontwo");
b3=newButton("buttonthree");
b4=newButton("buttonfour");
add(l1);
add(b1);
add(b2);
add(b3);
add(b4);
add(newLabel());
add(newButton("buttonfive"));
add(newButton("button6"));
add(newLabel("Label2"));
}
}
【2】在面板中添加组件
源代码如下:
importjava.applet.Applet;
importjava.awt.BorderLayout;
importjava.awt.Button;
importjava.awt.Choice;
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.GridLayout;
importjava.awt.Label;
importjava.awt.Panel;
publicclassLX9_2extendsApplet{
publicvoidinit(){
setFont(newFont("Arial",Font.PLAIN,20));
Labell=newLabel("thisisappletlabel",Label.CENTER);
add(l);
Panelpanel1=newPanel();
add(panel1);
panel1.setBackground(Color.blue);
panel1.setForeground(Color.red);
panel1.setLayout(newBorderLayout());
panel1.add("North",newButton("north"));
panel1.add("South",newButton("south"));
panel1.add("East",newButton("east"));
panel1.add("West",newButton("west"));
panel1.add("Center",newLabel("center"));
Panelpanel2=newPanel();
panel2.setLayout(newGridLayout(3,1));
Choicec=newChoice();
c.addItem("beijing");
c.addItem("shanghai");
c.addItem("tianjin");
Labell1=newLabel("thisispanel2lanel");
Buttonb1=newButton("panel2button");
panel2.setBackground(Color.green);
panel2.add(l1);
panel2.add(b1);
panel2.add(c);
add(panel2);
}
}
2)了解事件处理机制
【1】单击按钮的事件处理程序
源代码如下:
importjava.applet.Applet;
importjava.awt.Button;
importjava.awt.Label;
importjava.awt.Toolkit;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
publicclassLX9_3extendsAppletimplementsActionListener{
publicvoidinit(){
setLayout(null);
Labell=newLabel("listen",Label.CENTER);
add(l);
l.setBounds(20,10,150,30);
Buttonb=newButton("button");
add(b);
b.setBounds(60,50,60,40);
b.addActionListener(this);
}
publicvoidactionPerformed(ActionEvente){
Toolkit.getDefaultToolkit().beep();
}
}
【2】选择复选框和单选按钮的事件处理程序
代码如下:
importjava.applet.Applet;
importjava.awt.Checkbox;
importjava.awt.CheckboxGroup;
importjava.awt.Label;
importjava.awt.TextArea;
importjava.awt.TextField;
importjava.awt.event.ItemEvent;
importjava.awt.event.ItemListener;
publicclassLX9_4extendsAppletimplementsItemListener{
TextAreaarea=newTextArea(6,30);
StringItem[]={"2进制","8进制","16进制","10进制"};
Checkboxcb[]=newCheckbox[5];
Checkboxradio[]=newCheckbox[5];
Labell=newLabel("pleasechoicenumber");
TextFieldtf=newTextField(6);
publicvoidinit(){
add(l);
add(tf);
add(area);
add(newLabel("choice"));
for(inti=0;i<4;i++){
cb[i]=newCheckbox(Item[i]);
add(cb[i]);
cb[i].addItemListener(this);
}
CheckboxGroupcbGroup=newCheckboxGroup();
add(newLabel("choice"));
for(inti=0;i<4;i++){
radio[i]=newCheckbox(Item[i],cbGroup,false);
add(radio[i]);
radio[i].addItemListener(this);
}
}
publicvoiditemStateChanged(ItemEvente){
intx=Integer.parseInt(tf.getText());
if(e.getItem()=="2进制")
area.append(e.getItem()+Integer.toBinaryString(x)+"\n");
if(e.getItem()=="8进制")
area.append(e.getItem()+Integer.toOctalString(x)+"\n");
if(e.getItem()=="16进制")
area.append(e.getItem()+Integer.toHexString(x)+"\n");
if(e.getItem()=="10进制")
area.append(e.getItem()+Integer.toString(x)+"\n");
}
}
3)建立独立运行的窗口界面并使用匿名类
【1】创建一个窗口界面
代码如下:
importjava.awt.Frame;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
publicclassLX9_5{
publicstaticvoidmain(String[]args){
newLX9_5();
}
LX9_5(){
Framef=newFrame("firstwindows");
f.setSize(350,200);
f.setVisible(true);
f.addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);
}
});
}
}
【2】在窗口中添加组件
importjava.awt.Button;
importjava.awt.FlowLayout;
importjava.awt.Font;
importjava.awt.Frame;
importjava.awt.Label;
importjava.awt.TextArea;
importjava.awt.TextField;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
publicclassLX9_6extendsFrameimplementsActionListener{
Buttonbtn1,btn2;
TextFieldf,tf1,tf2;
TextAreaArea;
LX9_6(){
super("addzujiandewindows");
addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System.exit(0);}
});
setSize(350,250);
setLocation(200,200);
setFont(newFont("Arial",Font.PLAIN,12));
setLayout(newFlowLayout());
Area=newTextArea(6,40);
tf1=newTextField(10);
tf2=newTextField(10);
btn1=newButton("show");
btn2=newButton("exit");
f=newTextField(20);
add(Area);
add(newLabel("username"));
add(tf1);
add(newLabel("phone"));
add(tf2);
add(f);
add(btn1);
add(btn2);
tf1.addActionListener(this);
tf2.addActionListener(this);
btn1.addActionListener(this);
btn2.addActionListener(this);
show();
}
@Override
publicvoidactionPerformed(ActionEvente){
//TODOAuto-generatedmethodstub
if(e.getSource()==btn1){
f.setText("nianxial"+e.getActionCommand()+"button");
//if(e.getSource()==tf1)
Area.append("username"+tf1.getText()+"\n");
//if(e.getSource()==tf2)
Area.append("phone"+tf2.getText()+"\n");}
if(e.getSource()==btn2){
//for(inti=0;i<1000000;i++)
dispose();
}
}
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
newLX9_6();
}
}
【3】为窗口添加菜单
importjava.awt.Button;
importjava.awt.Menu;
importjava.awt.MenuBar;
importjava.awt.MenuItem;
importjava.awt.Panel;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JApplet;
importjavax.swing.JFrame;
publicclassLX9_7extendsJFrameimplementsActionListener{
Panelp=newPanel();
Buttonb=newButton("exit");
MenuBarmb=newMenuBar();
Menum1=newMenu("file");
MenuItemopen=newMenuItem("open");
MenuItemclose=newMenuItem("close");
MenuItemexit=newMenuItem("exit");
Menum12=newMenu("bianji");
MenuItemcopy=newMenuItem("copy");
MenuItemcut=newMenuItem("cut");
MenuItempaste=newMenuItem("paste");
Menum2=newMenu("help");
MenuItemcontent=newMenuItem("content");
MenuItemindex=newMenuItem("index");
MenuItemabout=newMenuItem("about");
LX9_7(){
super("addmenuwindows");
setSize(350,200);
add("South",p);
p.add(b);
b.addActionListener(this);
m1.add(open);
m1.add(close);
m1.addSeparator();
m1.add(exit);
open.addActionListener(this);
exit.addActionListener(this);
mb.add(m1);
m12.add(copy);
m12.add(cut);
m12.add(paste);
m1.add(m12);
m2.add(content);
m2.add(index);
m2.add(about);
mb.add(m2);
setMenuBar(mb);
show();
}
@Override
publicvoidactionPerformed(ActionEvente){
//TODOAuto-generatedmethodstub
if(e.getActionCommand()=="exit")
System.exit(0);
elseif(e.getActionCommand()=="open")
newLX9_7();
}
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
newLX9_7();
}
4)使用swing组件
【1】在JApplet中添加swing组件
importjava.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.Container;
importjava.awt.FlowLayout;
importjavax.swing.ImageIcon;
importjavax.swing.JApplet;
importjavax.swing.JButton;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
importjavax.swing.SwingConstants;
publicclassLX9_8extendsJApplet{
Containerpane;
JPanelpanel1,panel2;
JButtonbutton1,button2,button3;
JLabellabel;
publicvoidinit(){
pane=getContentPane();
panel1=newJPanel(newFlowLayout());
panel2=newJPanel(newFlowLayout());
ImageIconicon=newImageIcon("0.gif");
button1=newJButton(icon);
button2=newJButton(newImageIcon("2.gif"));
button3=newJButton(newImageIcon("1.gif"));
label=newJLabel("tuxiangbiaoqian",newImageIcon("a.gif"),SwingConstants.CENTER);
pane.setBackground(newColor(255,255,200));
panel1.setBackground(newColor(255,255,104));
panel2.setBackground(newColor(255,255,214));
button1.setToolTipText("xiangshangfanyeanniu");
button2.setToolTipText("tiaozhuananniu");
button3.setToolTipText("xiangxiafayeanniu");
pane.add("North",panel1);
pane.add(panel2,BorderLayout.SOUTH);
panel1.add(button1);
panel1.add(button2);
panel1.add(button3);
panel2.add(label);
}
【2】在JFrame窗口中添加组件
代码如下:
importjava.awt.BorderLayout;
importjava.awt.Dimension;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.WindowAdapter;
importjava.awt.event.WindowEvent;
importjavax.swing.Icon;
importjavax.swing.ImageIcon;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
importjavax.swing.JScrollPane;
importjavax.swing.JTextArea;
importjavax.swing.JToolBar;
publicclassLX9_9extendsJFrameimplementsActionListener{