原文: http://zetcode.com/gui/gtksharp/layout/

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

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

Fixed

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

fixed.cs

  1. using Gtk;
  2. using System;
  3. class SharpApp : Window {
  4. private Gdk.Pixbuf rotunda;
  5. private Gdk.Pixbuf bardejov;
  6. private Gdk.Pixbuf mincol;
  7. public SharpApp() : base("Fixed")
  8. {
  9. SetDefaultSize(300, 280);
  10. SetPosition(WindowPosition.Center);
  11. ModifyBg(StateType.Normal, new Gdk.Color(40, 40, 40));
  12. DeleteEvent += delegate { Application.Quit(); };
  13. try {
  14. bardejov = new Gdk.Pixbuf("bardejov.jpg");
  15. rotunda = new Gdk.Pixbuf("rotunda.jpg");
  16. mincol = new Gdk.Pixbuf("mincol.jpg");
  17. } catch {
  18. Console.WriteLine("Images not found");
  19. Environment.Exit(1);
  20. }
  21. Image image1 = new Image(bardejov);
  22. Image image2 = new Image(rotunda);
  23. Image image3 = new Image(mincol);
  24. Fixed fix = new Fixed();
  25. fix.Put(image1, 20, 20);
  26. fix.Put(image2, 40, 160);
  27. fix.Put(image3, 170, 50);
  28. Add(fix);
  29. ShowAll();
  30. }
  31. public static void Main()
  32. {
  33. Application.Init();
  34. new SharpApp();
  35. Application.Run();
  36. }
  37. }

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

  1. ModifyBg(StateType.Normal, new Gdk.Color(40, 40, 40));

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

  1. bardejov = new Gdk.Pixbuf("bardejov.jpg");

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

  1. Image image1 = new Image(bardejov);
  2. Image image2 = new Image(rotunda);
  3. Image image3 = new Image(mincol);

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

  1. Fixed fix = new Fixed();

我们创建Fixed容器。

  1. fix.Put(image1, 20, 20);

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

  1. Add(fix);

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

GTK# 中的布局管理 - 图1

图:固定

Alignment

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

alignment.cs

  1. using Gtk;
  2. using System;
  3. class SharpApp : Window {
  4. public SharpApp() : base("Alignment")
  5. {
  6. SetDefaultSize(260, 150);
  7. SetPosition(WindowPosition.Center);
  8. DeleteEvent += delegate { Application.Quit(); };
  9. VBox vbox = new VBox(false, 5);
  10. HBox hbox = new HBox(true, 3);
  11. Alignment valign = new Alignment(0, 1, 0, 0);
  12. vbox.PackStart(valign);
  13. Button ok = new Button("OK");
  14. ok.SetSizeRequest(70, 30);
  15. Button close = new Button("Close");
  16. hbox.Add(ok);
  17. hbox.Add(close);
  18. Alignment halign = new Alignment(1, 0, 0, 0);
  19. halign.Add(hbox);
  20. vbox.PackStart(halign, false, false, 3);
  21. Add(vbox);
  22. ShowAll();
  23. }
  24. public static void Main()
  25. {
  26. Application.Init();
  27. new SharpApp();
  28. Application.Run();
  29. }
  30. }

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

  1. Alignment valign = new Alignment(0, 1, 0, 0);

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

  1. vbox.PackStart(valign);

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

  1. HBox hbox = new HBox(true, 3);
  2. ...
  3. Button ok = new Button("OK");
  4. ok.SetSizeRequest(70, 30);
  5. Button close = new Button("Close");
  6. hbox.Add(ok);
  7. hbox.Add(close);

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

  1. Alignment halign = new Alignment(1, 0, 0, 0);
  2. halign.Add(hbox);
  3. vbox.PackStart(halign, false, false, 3);

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

GTK# 中的布局管理 - 图2

图:对齐

Table

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

calculator.cs

  1. using Gtk;
  2. using System;
  3. class SharpApp : Window {
  4. public SharpApp() : base("Calculator")
  5. {
  6. SetDefaultSize(250, 230);
  7. SetPosition(WindowPosition.Center);
  8. DeleteEvent += delegate { Application.Quit(); };
  9. VBox vbox = new VBox(false, 2);
  10. MenuBar mb = new MenuBar();
  11. Menu filemenu = new Menu();
  12. MenuItem file = new MenuItem("File");
  13. file.Submenu = filemenu;
  14. mb.Append(file);
  15. vbox.PackStart(mb, false, false, 0);
  16. Table table = new Table(5, 4, true);
  17. table.Attach(new Button("Cls"), 0, 1, 0, 1);
  18. table.Attach(new Button("Bck"), 1, 2, 0, 1);
  19. table.Attach(new Label(), 2, 3, 0, 1);
  20. table.Attach(new Button("Close"), 3, 4, 0, 1);
  21. table.Attach(new Button("7"), 0, 1, 1, 2);
  22. table.Attach(new Button("8"), 1, 2, 1, 2);
  23. table.Attach(new Button("9"), 2, 3, 1, 2);
  24. table.Attach(new Button("/"), 3, 4, 1, 2);
  25. table.Attach(new Button("4"), 0, 1, 2, 3);
  26. table.Attach(new Button("5"), 1, 2, 2, 3);
  27. table.Attach(new Button("6"), 2, 3, 2, 3);
  28. table.Attach(new Button("*"), 3, 4, 2, 3);
  29. table.Attach(new Button("1"), 0, 1, 3, 4);
  30. table.Attach(new Button("2"), 1, 2, 3, 4);
  31. table.Attach(new Button("3"), 2, 3, 3, 4);
  32. table.Attach(new Button("-"), 3, 4, 3, 4);
  33. table.Attach(new Button("0"), 0, 1, 4, 5);
  34. table.Attach(new Button("."), 1, 2, 4, 5);
  35. table.Attach(new Button("="), 2, 3, 4, 5);
  36. table.Attach(new Button("+"), 3, 4, 4, 5);
  37. vbox.PackStart(new Entry(), false, false, 0);
  38. vbox.PackEnd(table, true, true, 0);
  39. Add(vbox);
  40. ShowAll();
  41. }
  42. public static void Main()
  43. {
  44. Application.Init();
  45. new SharpApp();
  46. Application.Run();
  47. }
  48. }

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

  1. Table table = 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);

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

GTK# 中的布局管理 - 图3

图:计算机骨架

窗口

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

windows.cs

  1. using Gtk;
  2. using System;
  3. class SharpApp : Window {
  4. public SharpApp() : base("Windows")
  5. {
  6. SetDefaultSize(300, 250);
  7. SetPosition(WindowPosition.Center);
  8. BorderWidth = 15;
  9. DeleteEvent += delegate { Application.Quit(); };
  10. Table table = new Table(8, 4, false);
  11. table.ColumnSpacing = 3;
  12. Label title = new Label("Windows");
  13. Alignment halign = new Alignment(0, 0, 0, 0);
  14. halign.Add(title);
  15. table.Attach(halign, 0, 1, 0, 1, AttachOptions.Fill,
  16. AttachOptions.Fill, 0, 0);
  17. TextView wins = new TextView();
  18. wins.ModifyFg(StateType.Normal, new Gdk.Color(20, 20, 20));
  19. wins.CursorVisible = false;
  20. table.Attach(wins, 0, 2, 1, 3, AttachOptions.Fill | AttachOptions.Expand,
  21. AttachOptions.Fill | AttachOptions.Expand, 1, 1);
  22. Button activate = new Button("Activate");
  23. activate.SetSizeRequest(50, 30);
  24. table.Attach(activate, 3, 4, 1, 2, AttachOptions.Fill,
  25. AttachOptions.Shrink, 1, 1);
  26. Alignment valign = new Alignment(0, 0, 0, 0);
  27. Button close = new Button("Close");
  28. close.SetSizeRequest(70, 30);
  29. valign.Add(close);
  30. table.SetRowSpacing(1, 3);
  31. table.Attach(valign, 3, 4, 2, 3, AttachOptions.Fill,
  32. AttachOptions.Fill | AttachOptions.Expand, 1, 1);
  33. Alignment halign2 = new Alignment(0, 1, 0, 0);
  34. Button help = new Button("Help");
  35. help.SetSizeRequest(70, 30);
  36. halign2.Add(help);
  37. table.SetRowSpacing(3, 6);
  38. table.Attach(halign2, 0, 1, 4, 5, AttachOptions.Fill,
  39. AttachOptions.Fill, 0, 0);
  40. Button ok = new Button("OK");
  41. ok.SetSizeRequest(70, 30);
  42. table.Attach(ok, 3, 4, 4, 5, AttachOptions.Fill,
  43. AttachOptions.Fill, 0, 0);
  44. Add(table);
  45. ShowAll();
  46. }
  47. public static void Main()
  48. {
  49. Application.Init();
  50. new SharpApp();
  51. Application.Run();
  52. }
  53. }

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

  1. Table table = new Table(8, 4, false);
  2. table.ColumnSpacing = 3;

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

  1. Label title = new Label("Windows");
  2. Alignment halign = 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. TextView wins = new TextView();
  2. wins.ModifyFg(StateType.Normal, new Gdk.Color(20, 20, 20));
  3. wins.CursorVisible = false;
  4. table.Attach(wins, 0, 2, 1, 3, AttachOptions.Fill | AttachOptions.Expand,
  5. AttachOptions.Fill | AttachOptions.Expand, 1, 1);

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

  1. Alignment valign = new Alignment(0, 0, 0, 0);
  2. Button close = 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 | AttachOptions.Expand, 1, 1);

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

GTK# 中的布局管理 - 图4

图:窗口

在本章中,我们介绍了 GTK# 小部件的布局管理。