In interface design, a container must place many components. For the sake of beauty, the components are arranged in the container's position. This is layout design. There are multiple layout classes defined in java.awt, and each layout class corresponds to a layout strategy. The following layout classes are commonly used:
•FlowLayout, which places components in sequence.
•BoarderLayout, places components on the border.
•CardLayout, stacks components like playing cards, and only one component can be displayed at a time.
•GridLayout divides the display area into equal grids by rows and columns, and puts components into these grids in turn.
•GridBagLayout divides the display area into many small rectangular units, and each component can occupy one or more small units.
Among them, GridBagLayout can perform fine position control and is also the most complex. This tutorial will not discuss this layout strategy for the time being and will explain it in detail in the special article.
Each container has a layout manager, which determines how to arrange the components placed in the container. A layout manager is a class that implements the LayoutManager interface.
1. FlowLayout layout (JApplet, JPanel, JScrollPane default layout)
FlowLayout layout arranges the components from left to right in the order they are added. When a row is full, go to it. The rows continue from left to right, with the components in each row centered. This is the simplest layout strategy. It is generally used when there are not many components. When there are many components, the components in the container will appear uneven, with each row being of different lengths.
FlowLayout is the default layout of small applications and panels. The construction methods of FlowLayout layout are:
1.FlowLayout(), generates a default FlowLayout layout. By default, the component is centered with a gap of 5 pixels.
2.FlowLayout(int alignment), sets the alignment of each component. The alignment value can be FlowLayout.LEFT, FlowLayout.CENTER, FlowLayout.RIGHT.
3.FlowLayout(int aligment, int horz, int vert), set the alignment, and set the horizontal spacing horz and vertical spacing vert of the component. Use the setLayout() method of the super class Container to set the layout for the container. For example, the code setLayout(new FlowLayout()) sets the FlowLayout layout for the container. The method to add a component to the container is add(component name).
2.BorderLayout layout (default layout of JWindow, JFrame, JDialog)
BorderLayout layout strategy is to simply divide the space in the container into East "East", West "West", and South " There are five areas: "South", "North" and "Center". When adding a component, you should specify the area in which the component should be placed. Place a component in one position. If multiple components are to be added to a certain location, the components to be added to that location should first be placed in another container, and then the container should be added to this location.
The construction methods of BorderLayout layout are:
(1) BorderLayout(), which generates a default BorderLayout layout. By default, there is no gap.
(2) BorderLayout(int horz,int vert), sets the horizontal spacing and vertical spacing between components.
The setting method of BorderLayout layout strategy is setLayout(new BorderLayout()). The method of adding a component to a container is add(component name, position). If no position is specified when adding a component, the default position is "middle".
BorderLayout layout is the default layout of JWindow, JFrame, and JDialog.
[Example 11-5] The application has five labels, which are placed in the east, west, south, north and center areas of the window.
import javax.swing.*;import java.awt.*; public class J505{ public static void main(String[]args){ JLabel label1,label2,label3,label4,label5; JFrame mw=new JFrame("我是一个窗口");//创建一个窗口容器对象 mw.setSize(250,200); Container con=mw.getContentPane(); con.setLayout(new BorderLayout()); label1=new JLabel("东标签");//默认左对齐 label2=new JLabel("南标签",JLabel.CENTER); label3=new JLabel("西标签"); label4=new JLabel("北标签",JLabel.CENTER); label5=new JLabel("中标签",JLabel.CENTER); con.add(label1,"East"); con.add(label2,"South"); con.add(label3,"West"); con.add(label4,"North"); con.add(label5,"Center"); mw.setVisible(true); } }
3.GridLayout layout
GridLayout layout divides the container into a grid of rows and columns. The number of rows and columns is controlled by the program, and the components are placed in the small grids of the grid. middle. GridLayout layout is characterized by relatively precise component positioning. Since each grid in the GridLayout layout has the same shape and size, components placed in the container should also remain the same size.
The construction methods of GridLayout layout are:
(1) GridLayout(), which generates a single-column GridLayout layout. By default, there is no gap.
(2) GridLayout(int row,int col), sets a GridLayout layout with row row and column col.
(3) GridLayout(int row,int col,int horz,int vert), sets the number of rows and columns of the layout, and the horizontal and vertical spacing of the components.
GridLayout layout is based on behavior. When the number of placed components exceeds the limit, columns will be automatically added; conversely, if there are too few components, the columns will be automatically reduced. The number of rows remains unchanged and the components are arranged in row priority order (according to the component Automatically increase or decrease columns). Each grid in the GridLayout layout must be filled with components. If you want a grid to be blank, you can replace it with a blank label (add(new Label())).
[Example 11-6] The applet first puts several buttons and labels into JPanel, then puts the JPanel into JScrollPane, and finally, puts the JScrollPane into the window of the applet. The program The created JScrollPane always has horizontal and vertical scroll bars. The visible range of the scroll panel is smaller than the actual requirement of the panel. You can move the slider of the scroll bar to display the area of the panel that was not originally within the visible range.
import java.applet.*; import javax.swing.*; import java.awt.*; class MyWindow extends JFrame{ public MyWindow(int w,int h){ setTitle("滚动面板实例"); Container con=getContentPane(); con.setPreferredSize(new Dimension(w,h)); con.setLayout(new BorderLayout()); JPanel p=new JPanel(); p.setLayout(new GridLayout(6,6)); for (int i=0;i<6;i++){ p.add(new JLabel()); for(int j=1;j<=2;j++){ p.add(new JButton("按钮"+(2*i+j))); p.add(new JLabel("标签"+(2*i+j))); } p.add(new JLabel()); } p.setBackground(Color.blue); p.setPreferredSize(new Dimension(w+60,h+60)); JScrollPane ScrollPane=new JScrollPane(p); ScrollPane.setPreferredSize(new Dimension(w-60,h-60)); add(ScrollPane,BorderLayout.CENTER);//小程序添加滚动面板 setVisible(true); pack(); } } class ScrollPane extends JScrollPane{ public ScrollPane(Component p){ super(p); setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); } } public class J506 extends Applet{ MyWindow myWindow; public void init(){ myWindow=new MyWindow(400,350); } }
GridLayout布局要求所有组件的大小保持一致,这可能会使用界面外观不够美观。一个补救的办法是让一些小组件合并放在一个容器中,然后把这个容器作为组件,再放入到GridLayout布局中。这就是前面所说的容器嵌套。例如,容器A使用GridLayout布局,将容器均分为网格;另有容器B和C各放入若干组件后,把B和C分别作为组件添加到容器A中。容器B和C也可以设置为GridLayout布局,把自己分为若干网格,也可以设置成其他布局。这样,从外观来看,各组件的大小就有了差异。
四.CardLayout布局
采用CardLayout布局的容器虽可容纳多个组件,但是多个组件拥有同一个显示空间,某一时刻只能显示一个组件。就像一叠扑克牌每次只能显示最上面的一张一样,这个显示的组件将占据容器的全部空间。CardLayout布局设计步骤如下:
先创建CardLayout布局对象。然后,使用setLayout()方法为容器设置布局。最的,调用容器的add()方法将组件加入容器。CardLayout布局策略加入组件的方法是:
add(组件代号,组件);
其中组件代号是字符串,是另给的,与组件名无关。
例如,以下代码为一个JPanel容器设定CardLayout布局:
CardLayout myCard = new CardLayout();//创建CardLayout布局对象 JPanel p = new JPanel();//创建Panel对象 p.setLayout(myCard);
用CardLayout类提供的方法显示某一组件的方式有两种:
(1) 使用show(容器名,组件代号)形式的代码,指定某个容器中的某个组件显示。例如,以下代码指定容器p的组件代号k,显示这个组件:
myCard.show(p,k);
(2) 按组件加入容器的顺序显示组件。
first(容器):例如,代码myCard.first(p);
last(容器):例如 , myCard.last(p);
next(容器):例如,myCard.next(p);
previous(容器):myCard.previous(p);
【例11-7】小应用程序使用CardLayout布局,面板容器p使用CardLayout布局策略设置10个标签组件。窗口设有4个按钮,分别负责显示p的第一个组件、最后一个组件、当前组件的前一个组件和当前的组件的最后一个组件。
import java.applet.*;import java.awt.*; import java.awt.event.*;import javax.swing.*; class MyPanel extends JPanel{ int x;JLabel label1; MyPanel(int a){ x=a;getSize(); label1=new JLabel("我是第"+x+"个标签");add(label1); } public Dimension getPreferredSize(){ return new Dimension(200,50); } } public class J507 extends Applet implements ActionListener{ CardLayout mycard;MyPanel myPanel[];JPanel p; private void addButton(JPanel pan,String butName,ActionListener listener){ JButton aButton=new JButton(butName); aButton.addActionListener(listener); pan.add(aButton); } public void init(){ setLayout(new BorderLayout());//小程序的布局是边界布局 mycard=new CardLayout(); this.setSize(400,150); p=new JPanel();p.setLayout(mycard);//p的布局设置为卡片式布局 myPanel=new MyPanel[10]; for(int i=0;i<10;i++){ myPanel[i]=new MyPanel(i+1); p.add("A"+i,myPanel[i]); } JPanel p2=new JPanel(); addButton(p2,"第一个",this); addButton(p2,"最后一个",this); addButton(p2,"前一个",this); addButton(p2,"后一个",this); add(p,"Center"); add(p2,"South"); } public void actionPerformed(ActionEvent e){ if (e.getActionCommand().equals("第一个"))mycard.first(p); else if(e.getActionCommand().equals("最后一个"))mycard.last(p); else if(e.getActionCommand().equals("前一个"))mycard.previous(p); else if(e.getActionCommand().equals("后一个"))mycard.next(p); } }
五.null布局与setBounds方法
空布局就是把一个容器的布局设置为null布局。空布局采用setBounds()方法设置组件本身的大小和在容器中的位置:
setBounds(int x,int y,int width,int height)
组件所占区域是一个矩形,参数x,y是组件的左上角在容器中的位置坐标;参数weight,height是组件的宽和高。空布局安置组件的办法分两个步骤:先使用add()方法身容器添加组件。然后调用setBounds()方法设置组件在容器中的位置和组件本身的大小。与组件相关的其他方法:
1.getSize().width,
2.getSize().height
3.setVgap(ing vgap)
4.setHgap(int hgap);
更多java图形界面之布局设计相关文章请关注PHP中文网!