JAVA图形用户界面1.docx
《JAVA图形用户界面1.docx》由会员分享,可在线阅读,更多相关《JAVA图形用户界面1.docx(28页珍藏版)》请在冰豆网上搜索。
JAVA图形用户界面1
JAVA图形用户界面
(1)
最初的JAVA图形界面是AWT,为了平台公用性,AWT功能很简单,而且不同平台下的AWT存在不同的漏洞,因此被戏称为“一次编写,到处调试”。
随后,SUN与Netscape合作创建了Swing用户界面库。
Swing并不是完全替代AWT,而是基于AWT。
12.1框架(Frame、JFrame)
AWT中Frame/Swing中JFrame
(1)使用importjava.awt.*;访问图形类
(2)使用Frame(Stringtitle)构造方法产生框架
(3)使用setSize(intw,inth)设置框架大小
(4)使用show()方法使之可见,也可以使用setVisible(true)
(5)创建完成后,可以使用setTitle(Stringtitle)重设窗口的标题
例12.1
importjava.awt.*;
classProgram
{
publicstaticvoidmain(String[]args)
{
Framefrm=newFrame("我是窗口");
frm.setSize(500,300);
frm.setVisible(true);
}
}
或者:
importjava.awt.*;
classMyFrameextendsFrame
{
publicMyFrame()
{
}
publicMyFrame(Stringtitle)
{
super(title);
this.setSize(500,300);
this.addWindowListener(this);
frm.setVisible(true);
}
}
classProgram
{
publicstaticvoidmain(String[]args)
{
MyFramefrm=newMyFrame("我是窗口");
}
}
运行程序,将出现一个窗口,但什么内容也没有,也无法关闭,因为当前这个窗口还不能处理各种事件。
下面就为它加上简单的关闭窗口事件:
(1)引入java.awt.event包:
importjava.awt.event.*;
(2)实现WindowListener接口,该接口包括以下方法:
⏹publicvoidwindowClosing(WindowEvente);
⏹publicvoidwindowActivated(WindowEvente);
⏹publicvoidwindowClosed(WindowEvente);
⏹publicvoidwindowDeactivated(WindowEvente);
⏹publicvoidwindowDeiconified(WindowEvente);
⏹publicvoidwindowIconified(WindowEvente);
⏹publicvoidwindowOpened(WindowEvente);
(3)调用框架的addWindowListener方法添加事件监听(注册)
例12.2:
importjava.awt.*;
importjava.awt.event.*;
classMyFrameextendsFrameimplementsWindowListener
{
publicvoidwindowClosing(WindowEvente)
{
System.exit(0);
}
publicvoidwindowActivated(WindowEvente){}
publicvoidwindowClosed(WindowEvente){}
publicvoidwindowDeactivated(WindowEvente){}
publicvoidwindowDeiconified(WindowEvente){}
publicvoidwindowIconified(WindowEvente){}
publicvoidwindowOpened(WindowEvente){}
publicMyFrame()
{
}
publicMyFrame(Stringtitle)
{
super(title);
this.setSize(500,300);
this.addWindowListener(this);
frm.setVisible(true);
}
};
classProgram
{
publicstaticvoidmain(String[]args)
{
MyFramefrm=newMyFrame("我是窗口");
}
}
例12.3Swing中的JFrame
packagetestGUI;
importjavax.swing.JFrame;
publicclassTestJFrameextendsJFrame{
publicintwidth=300;
publicintheight=200;
publicTestJFrame()
{
this.setSize(this.width,this.height);//设置大小
this.setTitle("我的JFrame");//标题
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗体
this.setVisible(true);//显示
}
}
如何:
(1)得到当前屏幕分辨率
Toolkitkit=Toolkit.getDefaultToolkit();
DimensionscreenSize=kit.getScreenSize();
this.width=screenSize.width;
this.height=screenSize.height;
注:
Toolkit、Dimension都在java.awt包中
(2)窗体定位
窗体引用.setLocation(x坐标,y坐标);
或
窗体引用.setBounds(x坐标,y坐标,宽,高);
(3)设定图标
Imageimg=kit.getImage("d:
\\app.gif");
this.setIconImage(img);
注Image都在java.awt包中
窗体是一个容器,可以装载其他的控件或容器。
12.2按钮(Button、JButton)
(1)使用Button(Stringtitle)创建按钮,如Buttonb=newButton(“我是按钮”);
(2)使用框架类的add方法将按钮添加到框架中,如frm.add(b);
(3)为按钮添加事件:
●实现ActionListener接口
●编写actionPerformed(ActionEvente)方法,可以通过比较getActionCommand方法判断是哪个按钮触发的事件
●使用按钮的addActionListener(this)方法添加事件监听(注册)
(4)常用方法:
●setLabel():
设定按钮标题
●getLabel():
获得按钮标题
例12.4Button
importjava.awt.*;
importjava.awt.event.*;
classMyFrameextendsFrameimplementsWindowListener,ActionListener
{
publicvoidwindowClosing(WindowEvente)
{
System.exit(0);
}
publicvoidwindowActivated(WindowEvente){}
publicvoidwindowClosed(WindowEvente){}
publicvoidwindowDeactivated(WindowEvente){}
publicvoidwindowDeiconified(WindowEvente){}
publicvoidwindowIconified(WindowEvente){}
publicvoidwindowOpened(WindowEvente){}
publicvoidactionPerformed(ActionEvente)
{
Stringtitle=e.getActionCommand();//获得当前按钮标题
if(title.equals("关闭"))
{
System.exit(0);
}
}
publicMyFrame()
{
}
publicMyFrame(Stringtitle)
{
super(title);
this.setSize(500,300);
this.addWindowListener(this);
Buttonb=newButton("关闭");
b.addActionListener(this);
this.add(b);
this.show();
}
};
classProgram
{
publicstaticvoidmain(String[]args)
{
MyFramefrm=newMyFrame("我是窗口");
}
}
运行程序,会看到一个占满整个窗体的按钮,点击它可以关闭程序
程序中可以通过e.getActionCommand()来得到当前按钮的标题,进而判断按钮相应事件;也可以通过Objectsource=e.getSource();来得到当前按钮的引用,然后通过引用的比较来判断按钮相应事件。
例12.5JButton
packagetestGUI;
importjava.awt.Toolkit;
importjava.awt.Image;
importjava.awt.Dimension;
importjava.awt.event.*;
importjavax.swing.*;
publicclassTestJFrameextendsJFrameimplementsActionListener{
publicintwidth=300;
publicintheight=200;
privateJButtonbutton1;
publicvoidactionPerformed(ActionEvente)
{
Objectsource=e.getSource()
if(source==this.button1)
{
System.exit(0);
}
}
publicTestJFrame()
{
Toolkitkit=Toolkit.getDefaultToolkit();
Imageimg=kit.getImage("d:
\\app.gif");
this.setIconImage(img);
this.setSize(this.width,this.height);
this.setTitle("我的JFrame");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.button1=newJButton("按钮1");
this.add(this.button1);
button1.addActionListener(this);
this.setVisible(true);
}
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
TestJFrametf=newTestJFrame();
tf.setBounds(100,200,300,200);
}
}
例12.6使用局部类的JButton
packagetestGUI;
importjava.awt.Toolkit;
importjava.awt.Image;
importjava.awt.Dimension;
importjava.awt.event.*;
importjavax.swing.*;
publicclassTestJFrameextendsJFrame{
publicintwidth=300;
publicintheight=200;
privateJButtonbutton1;
publicTestJFrame()
{
Toolkitkit=Toolkit.getDefaultToolkit();
Imageimg=kit.getImage("d:
\\app.gif");
this.setIconImage(img);
this.setSize(this.width,this.height);
this.setTitle("我的JFrame");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.button1=newJButton("按钮1");
this.add(this.button1);
button1.addActionListener(
newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
System.exit(0);
}
}
);
this.setVisible(true);
}
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
TestJFrametf=newTestJFrame();
tf.setBounds(100,200,300,200);
}
}
12.3布局管理器
(1)流布局FlowLayout
●不限定组件大小
●从左往右,按照添加顺序显示,如果一行显示不下,换行显示
●使用框架的setLayout方法设置布局管理器
●FlowLayout的构造方法:
⏹FlowLayout()
⏹FlowLayout(intalign)//对齐方式
⏹FlowLayout(intalign,inthgap,intvgap)//对齐方式,水平间距,垂直间距
⏹注:
align的值为FlowLayout.LEFT、FlowLayout.CENTER、FlowLayout.RIGHT
例12.7
importjava.awt.*;
importjava.awt.event.*;
classMyFrameextendsFrameimplementsWindowListener,ActionListener
{
publicvoidwindowClosing(WindowEvente)
{
System.exit(0);
}
publicvoidwindowActivated(WindowEvente){}
publicvoidwindowClosed(WindowEvente){}
publicvoidwindowDeactivated(WindowEvente){}
publicvoidwindowDeiconified(WindowEvente){}
publicvoidwindowIconified(WindowEvente){}
publicvoidwindowOpened(WindowEvente){}
publicvoidactionPerformed(ActionEvente)
{
Stringtitle=e.getActionCommand();
if(title.equals("关闭"))
{
System.exit(0);
}
}
publicMyFrame()
{
this("title");
}
publicMyFrame(Stringtitle)
{
super(title);
this.setSize(500,300);
this.setLayout(newFlowLayout());
this.addWindowListener(this);
Buttonb=newButton("关闭");
b.addActionListener(this);
this.add(b);
Buttonb2=newButton("按钮2");
b2.addActionListener(this);
this.add(b2);
Buttonb3=newButton("按钮3");
b3.addActionListener(this);
this.add(b3);
this.show();
}
};
classProgram
{
publicstaticvoidmain(String[]args)
{
MyFramefrm=newMyFrame("我是窗口");
}
}
(2)边界布局BorderLayout
●BorderLayout将窗体分为5个区域:
BorderLayout.NORTH
BorderLayout.WEST
BorderLayout.CENERT
BorderLayout.EAST
BorderLayout.SOUTH
●使用框架的setLayout方法设置布局管理器
●使用框架的add(组件,区域)方法将组建添加到指定的区域
●每个区域只能添加一个组件,如果要添加多个组件,可以把这些组件先放在一个容器(Panel)中,然后将该容器添加到指定的区域。
例12.8
importjava.awt.*;
importjava.awt.event.*;
classMyFrameextendsFrameimplementsWindowListener,ActionListener
{
publicvoidwindowClosing(WindowEvente)
{
System.exit(0);
}
publicvoidwindowActivated(WindowEvente){}
publicvoidwindowClosed(WindowEvente){}
publicvoidwindowDeactivated(WindowEvente){}
publicvoidwindowDeiconified(WindowEvente){}
publicvoidwindowIconified(WindowEvente){}
publicvoidwindowOpened(WindowEvente){}
publicvoidactionPerformed(ActionEvente)
{
Stringtitle=e.getActionCommand();
if(title.equals("关闭"))
{
System.exit(0);
}
}
publicMyFrame()
{
this("title");
}
publicMyFrame(Stringtitle)
{
super(title);
this.setSize(500,300);
this.setLayout(newBorderLayout());
this.addWindowListener(this);
Buttonb=newButton("关闭");
b.addActionListener(this);
this.add(b,BorderLayout.SOUTH);
Buttonb2=newButton("按钮2");
b2.addActionListener(this);
this.add(b2,BorderLayout.WEST);
Panelp=newPanel();
p.setLayout(newFlowLayout());
Buttonb3=newButton("按钮3");
Buttonb4=newButton("按钮4");
b3.addActionListener(this);
b4.addActionListener(this);
p.add(b3);
p.add(b4);
this.add(p,BorderLayout.CENTER);
Buttonb5=newButton("按钮5");
b5.addActionListener(this);
this.add(b5,BorderLayout.EAST);
Buttonb6=newButton("按钮6");
b6.addActionListener(this);
this.add(b6,BorderLayout.NORTH);
this.show();
}
};
classProgram
{
publicstaticvoidmain(String[]args)
{
MyFramefrm=newMyFrame("我是窗口");
}
}
(3)网格布局GridLayout
例12.9
packagetestGUI;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassTestJFrameextendsJFrame{
publicintwidth=300;
publicintheight=200;
privateJButton[]buttons;
publicTestJFrame()
{
this.setS