原文: http://zetcode.com/gui/vbgtk/layoutmanagement/

在本章中,我们将展示如何在窗口或对话框中布置窗口小部件。

在设计应用的 GUI 时,我们决定要使用哪些小部件以及如何在应用中组织这些小部件。 为了组织窗口小部件,我们使用专门的不可见窗口小部件,称为布局容器。 在本章中,我们将提到AlignmentFixedVBoxTable

Fixed

Fixed容器将子窗口小部件放置在固定位置并具有固定大小。 此容器不执行自动布局管理。 在大多数应用中,我们不使用此容器。 我们在某些专业领域使用它。 例如游戏,使用图表的专用应用,可以移动的可调整大小的组件(如电子表格应用中的图表),小型教育示例。

  1. ' ZetCode Mono Visual Basic GTK# tutorial
  2. '
  3. ' In this program, we lay out widgets
  4. ' using absolute positioning
  5. '
  6. ' author jan bodnar
  7. ' last modified May 2009
  8. ' website www.zetcode.com
  9. Imports Gtk
  10. Public Class GtkVBApp
  11. Inherits Window
  12. Private Dim rotunda As Gdk.Pixbuf
  13. Private Dim bardejov As Gdk.Pixbuf
  14. Private Dim mincol As Gdk.Pixbuf
  15. Public Sub New
  16. MyBase.New("Fixed")
  17. Me.InitUI
  18. Me.SetDefaultSize(300, 280)
  19. Me.SetPosition(WindowPosition.Center)
  20. AddHandler Me.DeleteEvent, AddressOf Me.OnDelete
  21. Me.ShowAll
  22. End Sub
  23. Private Sub InitUI
  24. Me.ModifyBg(StateType.Normal, New Gdk.Color(40, 40, 40))
  25. Try
  26. bardejov = New Gdk.Pixbuf("bardejov.jpg")
  27. rotunda = New Gdk.Pixbuf("rotunda.jpg")
  28. mincol = New Gdk.Pixbuf("mincol.jpg")
  29. Catch e As Exception
  30. Console.WriteLine("Cannot load images")
  31. Console.WriteLine(e.Message)
  32. Environment.Exit(1)
  33. End Try
  34. Dim image1 As New Image(bardejov)
  35. Dim image2 As New Image(rotunda)
  36. Dim image3 As New Image(mincol)
  37. Dim fixed As New Fixed
  38. fixed.Put(image1, 20, 20)
  39. fixed.Put(image2, 40, 160)
  40. fixed.Put(image3, 170, 50)
  41. Me.Add(fixed)
  42. End Sub
  43. Sub OnDelete(ByVal sender As Object, _
  44. ByVal args As DeleteEventArgs)
  45. Application.Quit
  46. End Sub
  47. Public Shared Sub Main
  48. Application.Init
  49. Dim app As New GtkVBApp
  50. Application.Run
  51. End Sub
  52. End Class

在我们的示例中,我们在窗口上显示了三个小图像。 我们明确指定放置这些图像的 x,y 坐标。

  1. vbnc -r:/usr/lib/mono/gtk-sharp-2.0/gtk-sharp.dll -r:/usr/lib/mono/gtk-sharp-2.0/gdk-sharp.dll
  2. absolute.vb

在此示例中,我们还使用gdk-sharp程序集。

  1. Me.ModifyBg(StateType.Normal, New Gdk.Color(40, 40, 40))

为了获得更好的视觉体验,我们将背景色更改为深灰色。

  1. bardejov = New Gdk.Pixbuf("bardejov.jpg")

我们将图像从磁盘加载到Gdk.Pixbuf对象。

  1. Dim image1 As New Image(bardejov)
  2. Dim image2 As New Image(rotunda)
  3. Dim image3 As New Image(mincol)

Image是用于显示图像的小部件。 它在构造器中使用Gdk.Pixbuf对象。

  1. Dim fixed As New Fixed

我们创建Fixed容器。

  1. fixed.Put(image1, 20, 20)

我们将第一个图像放置在x = 20y = 20坐标处。

  1. Me.Add(fixed)

最后,我们将Fixed容器添加到窗口中。

布局管理 - 图1

图:固定

Alignment

Alignment容器控制其子窗口小部件的对齐方式和大小。

  1. ' ZetCode Mono Visual Basic GTK# tutorial
  2. '
  3. ' In this program, we position two buttons
  4. ' in the bottom right corner of the window.
  5. ' We use horizontal and vertical boxes.
  6. '
  7. ' author jan bodnar
  8. ' last modified May 2009
  9. ' website www.zetcode.com
  10. Imports Gtk
  11. Public Class GtkVBApp
  12. Inherits Window
  13. Public Sub New
  14. MyBase.New("Buttons")
  15. Me.InitUI
  16. Me.SetDefaultSize(260, 150)
  17. Me.SetPosition(WindowPosition.Center)
  18. AddHandler Me.DeleteEvent, AddressOf Me.OnDelete
  19. Me.ShowAll
  20. End Sub
  21. Private Sub InitUI
  22. Dim vbox As New VBox(False, 5)
  23. Dim hbox As New HBox(True, 3)
  24. Dim valign As New Alignment(0, 1, 0, 0)
  25. vbox.PackStart(valign)
  26. Dim ok As New Button("OK")
  27. ok.SetSizeRequest(70, 30)
  28. Dim close As New Button("Close")
  29. hbox.Add(ok)
  30. hbox.Add(close)
  31. Dim halign As New Alignment(1, 0, 0, 0)
  32. halign.Add(hbox)
  33. vbox.PackStart(halign, False, False, 3)
  34. Me.Add(vbox)
  35. End Sub
  36. Sub OnDelete(ByVal sender As Object, _
  37. ByVal args As DeleteEventArgs)
  38. Application.Quit
  39. End Sub
  40. Public Shared Sub Main
  41. Application.Init
  42. Dim app As New GtkVBApp
  43. Application.Run
  44. End Sub
  45. End Class

在代码示例中,我们在窗口的右下角放置了两个按钮。 为此,我们使用一个水平框和一个垂直框以及两个对齐容器。

  1. Dim valign As New Alignment(0, 1, 0, 0)

这会将子窗口小部件置于底部。

  1. vbox.PackStart(valign)

在这里,我们将Alignment小部件放置到垂直框中。

  1. Dim hbox As New HBox(True, 3)
  2. ...
  3. Dim ok As New Button("OK")
  4. ok.SetSizeRequest(70, 30)
  5. Dim close As New Button("Close")
  6. hbox.Add(ok)
  7. hbox.Add(close)

我们创建一个水平框,并在其中放置两个按钮。

  1. Dim halign As New Alignment(1, 0, 0, 0)
  2. halign.Add(hbox)
  3. vbox.PackStart(halign, False, False, 3)

这将创建一个对齐容器,它将其子窗口小部件放在右侧。 我们将水平框添加到对齐容器中,然后将对齐容器包装到垂直框中。 我们必须记住,对齐容器仅包含一个子窗口小部件。 这就是为什么我们必须使用盒子。

布局管理 - 图2

图:按钮

计算器骨架

Table小部件按行和列排列小部件。

  1. ' ZetCode Mono Visual Basic GTK# tutorial
  2. '
  3. ' In this program we create a skeleton of
  4. ' a calculator. We use the Table widget.
  5. '
  6. ' author jan bodnar
  7. ' last modified May 2009
  8. ' website www.zetcode.com
  9. Imports Gtk
  10. Public Class GtkVBApp
  11. Inherits Window
  12. Public Sub New
  13. MyBase.New("Calculator")
  14. Me.InitUI
  15. Me.SetDefaultSize(300, 250)
  16. Me.SetPosition(WindowPosition.Center)
  17. AddHandler Me.DeleteEvent, AddressOf Me.OnDelete
  18. Me.ShowAll
  19. End Sub
  20. Private Sub InitUI
  21. Dim vbox As New VBox(False, 2)
  22. Dim mb As New MenuBar
  23. Dim filemenu As New Menu
  24. Dim file As MenuItem = New MenuItem("File")
  25. file.Submenu = filemenu
  26. mb.Append(file)
  27. vbox.PackStart(mb, False, False, 0)
  28. Dim table As New Table(5, 4, True)
  29. table.Attach(New Button("Cls"), 0, 1, 0, 1)
  30. table.Attach(New Button("Bck"), 1, 2, 0, 1)
  31. table.Attach(New Label(), 2, 3, 0, 1)
  32. table.Attach(New Button("Close"), 3, 4, 0, 1)
  33. table.Attach(New Button("7"), 0, 1, 1, 2)
  34. table.Attach(New Button("8"), 1, 2, 1, 2)
  35. table.Attach(New Button("9"), 2, 3, 1, 2)
  36. table.Attach(New Button("/"), 3, 4, 1, 2)
  37. table.Attach(New Button("4"), 0, 1, 2, 3)
  38. table.Attach(New Button("5"), 1, 2, 2, 3)
  39. table.Attach(New Button("6"), 2, 3, 2, 3)
  40. table.Attach(New Button("*"), 3, 4, 2, 3)
  41. table.Attach(New Button("1"), 0, 1, 3, 4)
  42. table.Attach(New Button("2"), 1, 2, 3, 4)
  43. table.Attach(New Button("3"), 2, 3, 3, 4)
  44. table.Attach(New Button("-"), 3, 4, 3, 4)
  45. table.Attach(New Button("0"), 0, 1, 4, 5)
  46. table.Attach(New Button("."), 1, 2, 4, 5)
  47. table.Attach(New Button("="), 2, 3, 4, 5)
  48. table.Attach(New Button("+"), 3, 4, 4, 5)
  49. vbox.PackStart(New Entry, False, False, 0)
  50. vbox.PackEnd(table, True, True, 0)
  51. Me.Add(vbox)
  52. End Sub
  53. Sub OnDelete(ByVal sender As Object, _
  54. ByVal args As DeleteEventArgs)
  55. Application.Quit
  56. End Sub
  57. Public Shared Sub Main
  58. Application.Init
  59. Dim app As New GtkVBApp
  60. Application.Run
  61. End Sub
  62. End Class

我们使用Table小部件创建一个计算器框架。

  1. Dim table As New Table(5, 4, True)

我们创建一个具有 5 行 4 列的表小部件。 第三个参数是同质参数。 如果设置为true,则表中的所有小部件都具有相同的大小。 所有窗口小部件的大小等于表容器中最大的窗口小部件。

  1. table.Attach(New Button("Cls"), 0, 1, 0, 1)

我们在表格容器上附加一个按钮。 到表格的左上方单元格。 前两个参数是单元格的左侧和右侧,后两个参数是单元格的顶部和左侧。

  1. vbox.PackEnd(table, True, True, 0)

我们将表格小部件打包到垂直框中。

布局管理 - 图3

图:计算机骨架

窗口

接下来,我们将创建一个更高级的示例。 我们显示一个窗口,可以在 JDeveloper IDE 中找到它。

  1. ' ZetCode Mono Visual Basic GTK# tutorial
  2. '
  3. ' This is a more complicated layout example.
  4. ' We use Alignment and Table widgets.
  5. '
  6. ' author jan bodnar
  7. ' last modified May 2009
  8. ' website www.zetcode.com
  9. Imports Gtk
  10. Public Class GtkVBApp
  11. Inherits Window
  12. Public Sub New
  13. MyBase.New("Windows")
  14. Me.InitUI
  15. Me.SetDefaultSize(300, 250)
  16. Me.SetPosition(WindowPosition.Center)
  17. AddHandler Me.DeleteEvent, AddressOf Me.OnDelete
  18. Me.ShowAll
  19. End Sub
  20. Private Sub InitUI
  21. Me.BorderWidth = 15
  22. Dim table As New Table(8, 4, False)
  23. table.ColumnSpacing = 3
  24. Dim title As New Label("Windows")
  25. Dim halign As New Alignment(0, 0, 0, 0)
  26. halign.Add(title)
  27. table.Attach(halign, 0, 1, 0, 1, AttachOptions.Fill, _
  28. AttachOptions.Fill, 0, 0)
  29. Dim frame As New Frame
  30. table.Attach(frame, 0, 2, 1, 3, AttachOptions.Fill Or AttachOptions.Expand, _
  31. AttachOptions.Fill Or AttachOptions.Expand, 1, 1)
  32. Dim activate As New Button("Activate")
  33. activate.SetSizeRequest(50, 30)
  34. table.Attach(activate, 3, 4, 1, 2, AttachOptions.Fill, _
  35. AttachOptions.Shrink, 1, 1)
  36. Dim valign As New Alignment(0, 0, 0, 0)
  37. Dim close As New Button("Close")
  38. close.SetSizeRequest(70, 30)
  39. valign.Add(close)
  40. table.SetRowSpacing(1, 3)
  41. table.Attach(valign, 3, 4, 2, 3, AttachOptions.Fill, _
  42. AttachOptions.Fill Or AttachOptions.Expand, 1, 1)
  43. Dim halign2 As New Alignment(0, 1, 0, 0)
  44. Dim help As New Button("Help")
  45. help.SetSizeRequest(70, 30)
  46. halign2.Add(help)
  47. table.SetRowSpacing(3, 6)
  48. table.Attach(halign2, 0, 1, 4, 5, AttachOptions.Fill, _
  49. AttachOptions.Fill, 0, 0)
  50. Dim ok As New Button("OK")
  51. ok.SetSizeRequest(70, 30)
  52. table.Attach(ok, 3, 4, 4, 5, AttachOptions.Fill, _
  53. AttachOptions.Fill, 0, 0)
  54. Me.Add(table)
  55. End Sub
  56. Sub OnDelete(ByVal sender As Object, _
  57. ByVal args As DeleteEventArgs)
  58. Application.Quit
  59. End Sub
  60. Public Shared Sub Main
  61. Application.Init
  62. Dim app As New GtkVBApp
  63. Application.Run
  64. End Sub
  65. End Class

该代码示例显示了如何在 GTK# 中创建类似的窗口。

  1. Dim table As New Table(8, 4, False)
  2. table.ColumnSpacing = 3

该示例基于Table容器。 列之间将有 3px 的间隔。

  1. Dim title As New Label("Windows")
  2. Dim halign As New Alignment(0, 0, 0, 0)
  3. halign.Add(title)
  4. table.Attach(halign, 0, 1, 0, 1, AttachOptions.Fill, _
  5. AttachOptions.Fill, 0, 0)

这段代码创建了一个向左对齐的标签。 标签放置在Table容器的第一行中。

  1. Dim frame As New Frame
  2. table.Attach(frame, 0, 2, 1, 3, AttachOptions.Fill Or AttachOptions.Expand, _
  3. AttachOptions.Fill Or AttachOptions.Expand, 1, 1)

框架视图小部件跨越两行两列。 我们使小部件不可编辑并隐藏光标。

  1. Dim valign As New Alignment(0, 0, 0, 0)
  2. Dim close As New Button("Close")
  3. close.SetSizeRequest(70, 30)
  4. valign.Add(close)
  5. table.SetRowSpacing(1, 3)
  6. table.Attach(valign, 3, 4, 2, 3, AttachOptions.Fill, _
  7. AttachOptions.Fill Or AttachOptions.Expand, 1, 1)

我们将关闭按钮放在框架小部件旁边,进入第四列。 (我们从零开始计数)将按钮添加到对齐小部件中,以便可以将其对齐到顶部。

布局管理 - 图4

图:窗口

在 Visual Basic GTK# 教程的这一部分中,我们提到了小部件的布局管理。