现在很多开发工具都采用MVC的开发模式,WPF是比较具有代表性的,AWT也具有一定的MVC特征,相对来说肯定WPF更具有灵活性,现在来类比两者中的布局管理器
1:JAVA-BorderLayout与WPF-DockPanel
特点:可以将组建停靠在四边,WPF中没有Center,想有Java的CENTER效果,要设置LastChildFill为True
1
2
3
4
5
6
7
8
9 |
//Java BorderLayout private JPanel borderPanel= new JPanel( new BorderLayout()); borderPanel.add( new JButton( "North" ),BorderLayout.NORTH); borderPanel.add( new JButton( "South" ),BorderLayout.SOUTH); borderPanel.add( new JButton( "Center" ),BorderLayout.CENTER); borderPanel.add( new JButton( "West" ),BorderLayout.WEST); borderPanel.add( new JButton( "East" ),BorderLayout.EAST); |
1
2
3
4
5
6
7
8
9 |
<!--WPF XAML DockPanel--> < DockPanel Margin = "5" LastChildFill = "True" > < Button DockPanel.Dock = "Top" >Top</ Button > < Button DockPanel.Dock = "Bottom" >Button</ Button > < Button DockPanel.Dock = "Left" >Left</ Button > < Button DockPanel.Dock = "Right" >Right</ Button > < Button ></ Button > </ DockPanel > |
设置边距:Java-setHgap,setVgap--WPF-Margin
绝对位置:Java-东南西北--WPF-左右上下
2:Java-GridLayout与WPF-UniformGrid
特点:先确定行列,然后将组建按添加顺序依次排放
1
2
3
4
5
6
7 |
//Java GridLayout private JPanel gridPanel= new JPanel( new GridLayout( 2 , 2 )); gridPanel.add( new JButton( "0" )); gridPanel.add( new JButton( "1" )); gridPanel.add( new JButton( "2" )); |
1
2
3
4
5
6
7
8
9 |
<!--WPF XAML UniformGrid--> < UniformGrid Rows = "2" Columns = "3" > < Button FontSize = "24" >One</ Button > < Button FontSize = "24" >Two</ Button > < Button FontSize = "24" >Three</ Button > < Button FontSize = "24" >Four</ Button > < Button FontSize = "24" >Six</ Button > </ UniformGrid > |
3:Java:FlowLayout与WPF:WarpLayout
特点:按顺序排列下去,如果剩余长度不足,则自动换行
1
2
3
4
5
6
7
8
9 |
//Java FlowLayout private JPanel flowPanel= new JPanel( new FlowLayout()); flowPanel.add( new JButton( "One" )); flowPanel.add( new JButton( "Two" )); flowPanel.add( new JButton( "Three" )); flowPanel.add( new JButton( "Four" )); flowPanel.add( new JButton( "Five" )); flowPanel.add( new JButton( "Six" )); |
1
2
3
4
5
6
7
8
9 |
<!--WPF XAML WrapPanel--> < WrapPanel > < Button Width = "50" Height = "20" >One</ Button > < Button Width = "100" Height = "20" >Two</ Button > < Button Width = "100" Height = "20" >Three</ Button > < Button Width = "100" Height = "20" >Four</ Button > < Button Width = "100" Height = "20" >Five</ Button > </ WrapPanel > |
4:最重要
Java:GridBagLayout与WPF:Grid
特点:面板以单元格布局,组建可以定位到置顶单元格
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 |
private JPanel gridBagPanel= new JPanel( new GridBagLayout()); GridBagConstraints c= new GridBagConstraints(); c.fill=GridBagConstraints.BOTH; c.weightx= 1.0 ; c.weighty= 1.0 ; c.gridwidth=GridBagConstraints.REMAINDER; gridBagPanel.add( new JButton( "One" ),c); c.gridx= 1 ; c.gridy= 1 ; gridBagPanel.add( new JButton( "TWO" ),c); c.gridx= 1 ; c.gridy= 2 ; gridBagPanel.add( new JButton( "Three" ),c); c.gridx= 0 ; c.gridy= 1 ; c.gridheight= 2 ; c.gridwidth= 1 ; gridBagPanel.add( new JButton( "Four" ),c); |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 |
< Grid > < Grid.RowDefinitions > < RowDefinition ></ RowDefinition > < RowDefinition ></ RowDefinition > </ Grid.RowDefinitions > < Grid.ColumnDefinitions > < ColumnDefinition ></ ColumnDefinition > < ColumnDefinition ></ ColumnDefinition > < ColumnDefinition ></ ColumnDefinition > </ Grid.ColumnDefinitions > < Button Grid.Column = "0" Grid.Row = "1" Margin = "5" KeyDown = "Key_Down" Name = "tp" Grid.ColumnSpan = "3" > </ Button > < Button Grid.Column = "2" Grid.Row = "0" Margin = "5" Name = "tb" > < GroupBox Header = "asdf" Margin = "0" HorizontalAlignment = "Stretch" VerticalAlignment = "Stretch" > < StackPanel > < RadioButton >aaa</ RadioButton > < RadioButton >aaa</ RadioButton > < RadioButton >aaa</ RadioButton > </ StackPanel > </ GroupBox > </ Button > < ToggleButton Grid.Column = "0" Grid.Row = "0" ToolTipService.BetweenShowDelay = "100" > </ ToggleButton > < GridSplitter Grid.Column = "1" Grid.Row = "0" Grid.RowSpan = "2" VerticalAlignment = "Stretch" Width = "3" ></ GridSplitter > </ Grid > |
总体上感觉,AWT的布局管理和WPF的布局管理原理上相似,当不如WPF那么灵活