您的位置:首页 > 编程语言 > Java开发

java图形界面设计

2016-10-08 21:03 302 查看
1.创建一个简单框架:这是一个简单的Frame实例,该实例带有指定的标题,大小和背景颜色

import java.awt.*;
public class MyFrame extends Frame{
public static void main(String args[]) {

MyFrame fr=new MyFrame("Hello Out There !");
fr.setSize(400,200);
fr.setBackground(Color.blue);
fr.setVisible(true);
}
public MyFrame(String str) {
super(str);
}

}


执行之后的情况:



2.创建面板:该实例是创建一个黄色的面板,并将该面板加入到一个Frame对象中

import java.awt.*;
public class FrameWithPanel extends Frame{

public FrameWithPanel(String str){
super(str);
}

public static void main(String args[]){

FrameWithPanel fr=new FrameWithPanel("Frame with Panel");
Panel pan=new Panel();

fr.setSize(300,200);
fr.setBackground(Color.blue);
fr.setLayout(null);
pan.setSize(100,100);
pan.setBackground(Color.yellow);

fr.add(pan);
fr.setVisible(true);

}

}


执行后的情况:



3.布局

import java.awt.*;

public class ExGui{
private Frame f;
private Button b1;
private Button b2;

public static void main(String args[]) {

ExGui that =new ExGui();
that.go();

}

public void go() {
f=new Frame("GUI example");
f.setLayout(new FlowLayout());
b1=new Button("Press me");
b2=new Button("Don't press me");

f.add(b1);
f.add(b2);
f.pack();
f.setVisible(true);
}

}


执行后的情况:



 

4.BorderLayout布局管理器:

import java.awt.*;

public class ExGui2{
private Frame f;
private Button be,bw,bn,bs,bc;

public static void main(String args[]){

ExGui2 that=new ExGui2();
that.go();

}

void go(){

f=new Frame("Border Layout");
be=new Button("East");
bs=new Button("South");
bw=new Button("West");
bn=new Button("North");
bc=new Button("Center");

f.add(be,"East");
f.add(bs,"South");
f.add(bw,"West");
f.add(bc,"Center");

f.setSize(350,200);
f.setVisible(true);

}

}



5.GridLayout布局管理器:、

import java.awt.*;

public class GridEx{

private Frame f;
private Button b1,b2,b3,b4,b5,b6;

public static void main(String args[]){

GridEx that=new GridEx();
that.go();

}

void go(){
f=new Frame("Grid example");
f.setLayout(new GridLayout(3,2));

b1=new Button("b1");
b2=new Button("b2");
b3=new Button("b3");
b4=new Button("b4");
b5=new Button("b5");
b6=new Button("b6");

f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.add(b6);

f.pack();
f.setVisible(true);

}

}

执行后的情况:



6.CardLayout布局管理器:

import java.awt.*;
import java.awt.event.*;

public class CardTest extends MouseAdapter{

Panel p1,p2,p3,p4,p5;
Label l1,l2,l3,l4,l5;

CardLayout myCard;
Frame f;

public static void main(String args[]) {
CardTest ct=new CardTest();
ct.init();

}

public void init() {

f=new Frame("Card Test");
maCard=new CardLayout();
f.setLayout(myCard);

p1=new Panel();
p2=new Panel();
p3=new Panel();
p4=new Panel();
p5=new Panel();

l1=new Label("This is the first Panel");
p1.add(l1);
p1.setBackground(Color.yellow);

l2=new Label("This is the second Panel");
p2.add(l2);
p2.setBackground(Color.green);

l3=new Label("This is the third Panel");
p3.add(l3);
p3.setBackground(Color.magenta);

l4=new Label("This is the fourth Panel");
p4.add(l4);
p4.setBackground(Color.white);

l5=new Label("This is the fifth Panel");
p5.add(l5);
p5.setBackground(Color.cyan);

p1.addMouseListener(this);
p2.addMouseListener(this);
p3.addMouseListener(this);
p4.addMouseListener(this);
p5.addMouseListener(this);

f.add(p1,"First");
f.add(p2,"Second");
f.add(p3,"Thirt");
f.add(p4,"Fourth");
f.add(p5,"Fifth");

myCard.show(f,"First");
f.setSize(300,200);
f.show();

}

public void mouseClicked(MouseEvent e){
myCard.next(f);
}

}

执行后的情况:



7.构造复杂布局

import java.awt.*;

public class ExGui3{
private Frame f;
private Panel p;
private Button bw,bc;
private Button bfile,bhelp;

public static void main(String args[]) {

ExGui3 gui=new ExGui3();
gui.go();
}

public void go() {
f=new Frame("GUI example 3");
bw=new Button("West");
bc=new Button("Work space region");
f.add(bw,"West");
f.add(bc,"Center");
p=new Panel();
f.add(p,"North");
bfile=new Button("File");
bhelp=new Button("Help");
p.add(bfile);
p.add(bhelp);
f.pack();
f.setVisible(true);
}

}

执行后的情况:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: