GUI图形界面.docx

上传人:b****7 文档编号:10143394 上传时间:2023-02-08 格式:DOCX 页数:31 大小:24.85KB
下载 相关 举报
GUI图形界面.docx_第1页
第1页 / 共31页
GUI图形界面.docx_第2页
第2页 / 共31页
GUI图形界面.docx_第3页
第3页 / 共31页
GUI图形界面.docx_第4页
第4页 / 共31页
GUI图形界面.docx_第5页
第5页 / 共31页
点击查看更多>>
下载资源
资源描述

GUI图形界面.docx

《GUI图形界面.docx》由会员分享,可在线阅读,更多相关《GUI图形界面.docx(31页珍藏版)》请在冰豆网上搜索。

GUI图形界面.docx

GUI图形界面

图形界面(GUI)-----(AWT、Swing)

目标:

了解AWT的作用

了解Swing与AWT的关系

一、AWT(AbstractWindowingToolKit),抽象窗口工具包,是JDK1.0设置图形化界面

AWT在java.awt,这个包中的所有操作类可用来建立与平台无关的GUI的类,这些类又叫做组件(Components)

二、在AWT包中所提供的工具类,主要有三种

1.组件:

Component

2.容器:

Container

3.布局管理器:

LayoutManager

事件类组件颜色类布局管理器字体类绘图图片类菜单类

1.Component:

在AwT包中,所的组件(按钮)都是从Component和MenuComponent扩展而来的。

Component包括:

ButtonCheckBoxChoiceLabelListScollbarTextCompent….

Component是所有组件的最大父类

2.Container

所有的组件都应该放在容器中,容器也是Componet的子类

Container:

Window【FrameDialog】PanelScrollPane

3.LayoutManager

BorderLayoutFlowLayoutGridLayoutCardLayoutGirdBagLayout…..

三、Swing简介

AWT大量的引用了Windows函数,所以常被称为重量级的组件,在java2中提供了轻量级的图形界面组件——Swing,Swing使用java语言开发的,是以AWT平台为基础构建直起来的新组件。

在java中所有的Swing都保存在javax.swing包中,x代表是java包的扩展包

所有的组件是从JComponent扩展而来的

Java.awt.Componet

Java.awt.Container

Javax.swing.JComponent

Awt因为引用了大量的windows函数,所以移值比较困难

1.JComponent的常用子类

JButtonJChecboxJList…..

Jxx就是Swing的操作

总结:

AWT和Swing的关系,其实Swing还是引用大量的AWT包中的内容,在现在的开以不肯定不用AWT,连Swing也不怎么用(B/S)

基本的容器:

JFrame

JFrame的作用

JFrame的常用方法

Dimension

Point

java.lang.Object

java.awt.Component

java.awt.Container

java.awt.Window

java.awt.Frame

javax.swing.JFrame

1、第一个窗体:

1.新建一个窗体

例1:

publicclassJFrameDemo01{

publicstaticvoidmain(String[]args){

JFramef=newJFrame();//不可见的类

f.setSize(230,80);//设置组件的大小

f.setBackground(Color.WHITE);//设置背景色

f.setLocation(300,200);//设置组件的显示位置

f.setVisible(true);//让组件可见

}

}

例2:

Deimesion类和Point类的应用

publicclassJFrameDemo02{

publicstaticvoidmain(String[]args){

JFramef=newJFrame("第一个窗口");

Dimensiond=newDimension();

d.setSize(300,200);

f.setSize(d);

Pointp=newPoint(300,200);

f.setLocation(p);

f.setVisible(true);

}

}

2.JFrame的构造方法

3.JFrame运行之后:

窗体关闭之后,运行进程并没有结束,想要关闭,需要事件的支持

基本JComponent组件

1.标签组件:

JLabel(存放文字信息)

例1:

例2:

更改Jlabel的文字格式

Jlabel方法:

PublicvoidsetFont(Fontf);

Font:

java.awt包中;

Font(String name,int style,int size)

          根据指定名称、样式和磅值大小,创建一个新Font。

例3:

得到所有的字体

importjava.awt.GraphicsEnvironment;

publicclassALLFontDemo{

publicstaticvoidmain(String[]args){

GraphicsEnvironmenten=GraphicsEnvironment.getLocalGraphicsEnvironment();

StringfontName[]=en.getAvailableFontFamilyNames();

for(inti=0;i

System.out.println(fontName[i]);

}

}

}

例4:

JLabel标记设置一个图片(Icon类及ImageIcon类)

2.JButton组件:

按钮类

例1:

JFramewin1=newJFrame();//窗体

Iconimage=newImageIcon("D:

\\logo.jpg");

JButtonbun1=newJButton("李洪志",image);//按钮,参数是按钮里显示字符

win1.add(bun1);

win1.setVisible(true);//可见

win1.setSize(400,300);//大小

win1.setLocation(50,20);//初始位置

3.JTextComponent

(1)单行文本JTextField

例1:

(2)密码框JPassWordFiled

例1:

(3)多行文本JTextArea

例1:

publicclassShowFlowLayoutDemoextendsJFrame{

publicShowFlowLayoutDemo(){

setLayout(newFlowLayout(FlowLayout.LEFT,10,10));

JTextFieldtext=newJTextField();

add(newJLabel("name"));

add(text);

text.setColumns(10);

add(newJLabel("password"));

add(newJPasswordField());

add(newJTextArea(10,10));

this.setTitle("title");

this.setSize(200,200);

this.setLocationRelativeTo(null);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

}

/**

*@paramargs

*/

publicstaticvoidmain(String[]args){

ShowFlowLayoutDemoframe=newShowFlowLayoutDemo();

}

 

布局管理器

1、FlowLayout:

流式布局管理器,会使用组件向流水一样依次排列。

JFrame的setLayout()方法

例1:

publicclassFlowLayoutDemo{

publicstaticvoidmain(String[]args){

JFramef=newJFrame("流布局");

f.setLayout(newFlowLayout(FlowLayout.CENTER,10,10));//创建流布局并使每一行组件居中,指定组件间距。

JButtonbun1=newJButton("太");

JButtonbun2=newJButton("原");

JButtonbun3=newJButton("欢");

JButtonbun4=newJButton("迎");

JButtonbun5=newJButton("你");

f.add(bun1);

f.add(bun2);

f.add(bun3);

f.add(bun4);

f.add(bun5);

f.setSize(200,80);

f.setVisible(true);

}

}

2、BordreLayout:

会将窗体的版面分成东、西、南、北、中五个区域。

JFrame:

add(..)两个参数重载的方法

例1:

publicclassBorderLayoutDemo{

publicstaticvoidmain(Stringargs[]){

JFramef=newJFrame("Border布局");

f.setLayout(newBorderLayout());

f.add(newJButton("South"),BorderLayout.SOUTH);

f.add(newJButton("North"),BorderLayout.NORTH);

f.add(newJButton("East"),BorderLayout.EAST);

f.add(newJButton("West"),BorderLayout.WEST);

f.add(newJButton("Center"),BorderLayout.CENTER);

f.setSize(400,400);

f.setVisible(true);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

3、GridLayout:

以表格的形式进行管理

例1:

packageyanshi;

importjava.awt.*;

importjavax.swing.*;

publicclassGridLayoutDemoextendsJFrame{

publicstaticvoidmain(String[]args){

GridLayoutDemog=newGridLayoutDemo();

}

publicGridLayoutDemo(){

intflag=1;

JPanelj=newJPanel();

j.setLayout(newGridLayout(3,3));

this.add(j,BorderLayout.CENTER);

JButton[]botton=newJButton[9];

for(inti=0;i<9;i++){

if(i%2==0){

botton[i]=newJButton();

botton[i].setBackground(Color.RED);

}else{

botton[i]=newJButton();

botton[i].setBackground(Color.GREEN);

}

j.add(botton[i]);

}

this.setSize(300,300);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setLocationRelativeTo(null);

//this.setResizable(false);

this.setLayout(newBorderLayout());

this.add(j,BorderLayout.CENTER);

this.setVisible(true);

while(true){

try{

Thread.sleep(200);

}catch(InterruptedExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

if(flag==1){

flag=0;

for(intj1=0;j1<9;j1++)

{

if(j1%2==0){

botton[j1].setBackground(Color.GREEN);

}else{

botton[j1].setBackground(Color.RED);

}

}

}else{

flag=1;

for(intj1=0;j1<9;j1++)

{

if(j1%2==0){

botton[j1].setBackground(Color.RED);

}else{

botton[j1].setBackground(Color.GREEN);

}

}

}

}

}

}

 

4、CardLayout:

就是将一组组件彼此重叠的进行布局,就像一张张卡片一样,每次只显示一个界面

例1:

publicCardLayoutDemo(){

super("showCardLayout");

CardLayoutcl=newCardLayout();

setLayout(cl);

Containercon=this.getContentPane();

JButtonjb=newJButton("");

jb.setBackground(Color.BLACK);

JButtonjb1=newJButton("");

jb1.setBackground(Color.BLUE);

JButtonjb2=newJButton("");

jb2.setBackground(Color.CYAN);

JButtonjb3=newJButton("");

jb3.setBackground(Color.GRAY);

JButtonjb4=newJButton("");

jb4.setBackground(Color.GREEN);

JButtonjb5=newJButton("");

jb5.setBackground(Color.ORANGE);

JButtonjb6=newJButton("");

jb6.setBackground(Color.YELLOW);

this.add(jb,"1");

this.add(jb1,"2");

this.add(jb2,"3");

this.add(jb3,"4");

this.add(jb4,"5");

this.add(jb5,"6");

this.add(jb6,"7");

this.setSize(300,300);

this.setLocationRelativeTo(null);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setVisible(true);

//cl.show(con,"3");

for(inti=1;i<=100;i++){

try{

Thread.sleep(200);

}catch(InterruptedExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

cl.next(con);

}

}

5、绝对定位

首先取消窗体布局(null),通过每一个组件的setBounds()方法去设置坐标

例1:

publicstaticvoidmain(String[]args){

//TODOAuto-generatedmethodstub

JFrameframe=newJFrame();

JButtonbutton1=newJButton("button1");

JButtonbutton2=newJButton("button2");

frame.setSize(500,500);

frame.setLocation(200,100);

frame.add(button1);

frame.add(button2);

frame.setLayout(null);

button1.setBounds(200,100,100,50);

button2.setBounds(350,100,100,50);

frame.setVisible(true);

}

其他容器

1、JPanel:

可以将一组组件放入此容器中管理,完成一些复杂的界面设计。

例1:

publicclassJPanelDemo{

publicstaticvoidmain(String[]args){

JPanelMakerframe=newJPanelMaker();

//frame.setSize(500,500);

frame.pack();

frame.setVisible(true);

}

}

classJPanelMakerextendsJFrame{

publicJPanelMaker(){

JPanelp1=newJPanel(newGridLayout(3,3));

for(inti=1;i<=9;i++){

p1.add(newJButton(""+i));

}

JPanelp2=newJPanel(newGridLayout(3,3));

for(inti=1;i<=9;i++){

p2.add(newJButton(""+(i+10)));

}

JPanelp3=newJPanel(newGridLayout(3,3));

for(inti=1;i<=9;i++){

p3.add(newJButton(""+(i+20)));

}

this.setLayout(newBorderLayout());

this.add(p1,BorderLayout.NORTH);

this.add(p2,BorderLayout.CENTER);

this.add(p3,BorderLayout.WEST);

JScrollBarjs1=newJScrollBar();

JScrollBarjs2=newJScrollBar();

js2.setOrientation(JList.VERTICAL);

this.add(js1,BorderLayout.EAST);

this.add(js2,BorderLayout.SOUTH);

}

}

2、JSplitPane:

分割面板,可将一个窗体分为两个子窗体,可以水平分也可以垂直分。

3、JTabbedPane。

publicclassJTabbedPaneDemo11{

publicstaticvoidmain(String[]args){

JFrameframe=newJFrame("Welcome");//实例化窗体对象

Containercont=frame.getContentPane();

JTabbedPanetab=null;

tab=newJTabbedPane(JTabbedPane.TOP);//设置标签在顶部显示

JPanelpan1=newJPanel();

JPanelpan2=newJPanel();

JButtonbut=newJButton("按钮");

JLabellab=newJLabel("标签");

pan1.add(but);

pan2.add(lab);

tab.addTab("选项一",pan1);

tab.addTab("选项二",pan2);

//tab.addTab("图片选项",newImageIcon("image\\logo1.jpg"),pan1,"图象");

cont.add(tab);

frame.setSize(600,700);//根据组件自动调整大小

frame.setLocation(300,200);

frame.setVisible(true);

}

}

例1:

4、JScollPane:

例1:

JFrameframe=newJFrame("Welcome");//实例化窗体对象

//Containercont=frame.getContentPane();

//实例化Icon对象

JPanelpan=newJPanel();

JLabellab=newJLabel("你好");

JScrollPanescr1=null;

//设置垂直和水平的滚动条一直显示

scr1=newJScrollPane(pan,

JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,

JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

pan.add(lab);

//cont.add(scr1);

frame.add(scr1);

frame.setSize(230,120);//根据组件自动调

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 表格模板 > 合同协议

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1