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);//根据组件自动调