JAVA-- Swring组件入门级基础

                                                                Swring组件入门及布局




Swing组件分析
组件分类:


顶层容器:独立存在,不需要放置到任何组件上
包括:JFrame、JWindow、JDialog


顶层容器:用于初始化界面,为其他组件的展示提供一个容器载体,以满足界面显示需求


JFrame:用于创建一个带有标题栏的窗体


JApplet:用于创建一个applet小应用窗体


JDialog:用于创建对话框窗体


JWindow:创建一个不带标题和边框的窗体
注意:这里一般不用JWindow,因为它的效果可以用JFrame经过操作来实现。
package layout.demo1;


import javax.swing.JFrame;


public class window extends JWindow{
public void into2(){
this.setSize(400,600);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setVisible(true);
}


}
-----------------------------------------------------------
package layout.demo1;


import javax.swing.JFrame;


public class layout001 extends JFrame{
public void into(){

this.setSize(400,600);
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setUndecorated(true);
this.setVisible(true);

}



}
------------------------------------------------------------
以上两个代码的效果是一样的。






中间层容器:放置到另外一个容器组件上的容器组件
JPanel:容器面板
JScrollPane:滑动面板
基本功能组件
注意:这里中间层容器上可以再放中间层容器。而组件上不可以再放组件。




补充:
设置大小:setSize   setPreferredSize
setSize:只能给顶层容器设置大小
setPreferredSize:给功能组件和中间层容器设置大小
布局分析
顶层容器默认布局:边框布局BorderLayout
中间层容器默认布局:流式布局FlowLayout


布局概念
布局是指组件摆放到容器中的排列方式
布局一般都是设置到容器对象上
每个容器需要往上添加组件的时候,
都需要先设置好布局
常用布局分类


FlowLayout:流式布局


BorderLayout:边框布局


GridLayout:网格布局


null:空布局(不使用任何布局)




package layout.demo2;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.util.MissingFormatArgumentException;


import javax.swing.JFrame;
import javax.swing.JPanel;


public class borderlayout {
public static void main(String[] args) {
JFrame jf=new JFrame();
jf.setSize(400, 450);
jf.setDefaultCloseOperation(3);
jf.setLocationRelativeTo(null);
jf.setVisible(true);

JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
JPanel panel3=new JPanel();
JPanel panel4=new JPanel();
JPanel panel5=new JPanel();

BorderLayout bl=new BorderLayout();
jf.setLayout(bl);

panel1.setBackground(Color.green);
panel2.setBackground(Color.red);
panel3.setBackground(Color.orange);
panel4.setBackground(Color.yellow);
panel5.setBackground(Color.blue);

Dimension dn=new Dimension(40,40);

panel1.setPreferredSize(dn);
panel2.setPreferredSize(dn);
panel3.setPreferredSize(dn);
panel4.setPreferredSize(dn);
panel5.setPreferredSize(dn);

jf.add(panel1,"West");
jf.add(panel2,"South");
jf.add(panel3,"Center");
jf.add(panel4,"East");
jf.add(panel5,"North");
}
}

猜你喜欢

转载自blog.csdn.net/xu_jin_ren_shuai/article/details/79159787