一、图解
说明:
1、Java的图形用户界面的最基本组成部分是Component,一般的Component对象不能独立地显示出来,必须放在某一Container对象中才可以显示出来。
2、Container是Component子类,Container对象可以容纳别的Component对象。
3、Container也可以当成Component添加至其他Container中。
4、Windows其对象表示自由停泊的顶级窗口。Panel可以容纳其他的Component,但不能独立存在,必须添加到其他的Container中,如Windows或Applet
二、布局管理器
1、BorderLayout实例
import javax.swing.*; import java.awt.*; public class Test35 { static JFrame jFrame; public static void main(String args[]){ jFrame = new JFrame("登录"); /* Container container = jFrame.getContentPane(); container.setVisible(true); 构造Frame对象时,会默认有个Pane,Jframe对象的setLayout其实是将默认的Pane划分为5部分,add方法 其实是将组建添加至默认的Pane中,所以代码下方使用jframe.setBackground后无法看到颜色,其实是被默认的 Pane挡住了 */ JPanel panel =(JPanel) jFrame.getContentPane(); panel.setOpaque(true);//设置默认面板为透明 panel.setSize(20,20); panel.setVisible(true); panel.setBackground(Color.green); jFrame.setLayout(new BorderLayout(5,10)); // jFrame.add(new JButton("东"),BorderLayout.EAST); JPanel jPanel = new JPanel(); jPanel.setLayout(new BorderLayout()); jPanel.add(new JButton("dong"),BorderLayout.EAST); jPanel.add(new JButton("xi"),BorderLayout.WEST); jPanel.add(new JButton("nan"),BorderLayout.SOUTH); jPanel.add(new JButton("bei"),BorderLayout.NORTH); panel.add(jPanel,BorderLayout.EAST); jFrame.add(new JButton("西"),BorderLayout.WEST); jFrame.add(new JButton("南"),BorderLayout.SOUTH); jFrame.add(new JButton("北"),BorderLayout.NORTH); jFrame.add(new JButton("中"),BorderLayout.CENTER); Toolkit toolkit = Toolkit.getDefaultToolkit();//获取默认工具包 Dimension dimension = toolkit.getScreenSize();//获取默认屏幕尺寸 jFrame.setVisible(true);//设置容器是否可见 jFrame.setLocation(dimension.width/4,dimension.height/4);//设定容器位置 jFrame.setSize(dimension.width/2,dimension.height/2);//设定容器大小 jFrame.setBackground(Color.blue);//设定容器背景 jFrame.setResizable(true);//设定容器是否可以改变大小 //应用程序关闭后,其实在进程中并没有完全关闭,需要此设置后才可完全关闭 jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
2、FlowLayout
import javax.swing.*; import java.awt.*; public class Test36 { static JFrame jFrame; static JPanel jPanel; static JButton jButton,jButton1,jButton2; public static void main(String args[]){ Toolkit toolkit = Toolkit.getDefaultToolkit();//获取默认工具包 Dimension dimension = toolkit.getScreenSize() ;//获取默认屏幕尺寸 jPanel = new JPanel(); jFrame = new JFrame("测试"); jButton = new JButton("按钮1"); jButton1 = new JButton("按钮2"); jButton2 = new JButton("按钮3"); jPanel.add(jButton); jPanel.add(jButton1); jPanel.add(jButton2); jFrame.add(jPanel); // jPanel.setVisible(false);jPanel默认的布局是FlowLayout,把panl加入到Frame中不用再设定panl是否可见了 jFrame.setVisible(true);//设置容器是否可见 jFrame.setLocation(dimension.width/4,dimension.height/4);//设定容器位置 jFrame.setSize(dimension.width/2,dimension.height/2);//设定容器大小 jFrame.setDefaultCloseOperation(jFrame.EXIT_ON_CLOSE); } }
3、CardLayout
import javax.swing.*; import java.awt.*; /* CardLayout是卡片布局管理器,只按顺序显示第一个组件,CardLyout封装了一些方法,可以显示指定的卡片 */ public class Test37 { static JFrame jFrame; static JButton jButton,jButton1,jButton2; public static void main(String args[]){ Toolkit toolkit = Toolkit.getDefaultToolkit(); Dimension dimension = toolkit.getScreenSize(); jFrame = new JFrame("测试"); jButton = new JButton("按钮1"); jButton1 = new JButton("按钮2"); jButton2 = new JButton("按钮3"); CardLayout cardLayout = new CardLayout(); jFrame.setLayout(cardLayout); jFrame.add(jButton); jFrame.add(jButton1); jFrame.add(jButton2); cardLayout.next(jFrame.getContentPane());//显示下一个卡片 jFrame.setSize(dimension.width/2,dimension.height/2); jFrame.setLocation(dimension.width/4,dimension.height/4); jFrame.setVisible(true); jFrame.setDefaultCloseOperation(jFrame.EXIT_ON_CLOSE); } }
四、GridLayout
import javax.swing.*; import java.awt.*; public class Test38 extends JFrame{ static JFrame jFrame; static JButton jButton,jButton1,jButton2; public static void main(String args[]){ Toolkit toolkit = Toolkit.getDefaultToolkit(); Dimension dimension = toolkit.getScreenSize(); jFrame = new JFrame("登录"); jFrame.setLayout(new GridLayout()); /* GridLayout 类是一个布局处理器,它以矩形网格形式对容器的组件进行布置。容器被分成大小相等的矩形,一个矩形中放置一个组件 可以在参数中设定几行几列 */ jButton = new JButton("按钮1"); jButton1 = new JButton("按钮2"); jButton2 = new JButton("按钮3"); jFrame.add(jButton); jFrame.add(jButton1); jFrame.add(jButton2); jFrame.setLocation(dimension.width/4,dimension.height/4); jFrame.setSize(dimension.width/2,dimension.height/2); jFrame.setVisible(true); jFrame.setResizable(true); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }