原文: http://zetcode.com/gui/javagnome/advancedwidgets/

在 Java Gnome 编程教程的这一部分中,我们将介绍一些更高级的小部件。

TexView

TexView小部件用于显示和编辑多行文本。 TextView小部件也具有 MVC 设计。 TextView 代表视图组件,TexBuffer代表模型组件。 TextBuffer用于处理文本数据。 TextTag是可以应用于文本的属性。 TextIter表示文本中两个字符之间的位置。 所有使用文本的操作都是使用文本迭代器完成的。

textview.java

  1. package com.zetcode;
  2. import org.gnome.gdk.Event;
  3. import org.gnome.gtk.Gtk;
  4. import org.gnome.gtk.TextBuffer;
  5. import org.gnome.gtk.TextIter;
  6. import org.gnome.gtk.TextTag;
  7. import org.gnome.gtk.TextView;
  8. import org.gnome.gtk.Widget;
  9. import org.gnome.gtk.Window;
  10. import org.gnome.gtk.WindowPosition;
  11. import org.gnome.gtk.WrapMode;
  12. import org.gnome.pango.FontDescription;
  13. import org.gnome.pango.Style;
  14. /**
  15. * ZetCode Java Gnome tutorial
  16. *
  17. * This program demonstrates the
  18. * TextView widget.
  19. *
  20. * @author jan bodnar
  21. * website zetcode.com
  22. * last modified March 2009
  23. */
  24. public class GTextView extends Window {
  25. public GTextView() {
  26. setTitle("TextView");
  27. initUI();
  28. connect(new Window.DeleteEvent() {
  29. public boolean onDeleteEvent(Widget source, Event event) {
  30. Gtk.mainQuit();
  31. return false;
  32. }
  33. });
  34. setDefaultSize(250, 200);
  35. setPosition(WindowPosition.CENTER);
  36. showAll();
  37. }
  38. public void initUI() {
  39. final TextView view;
  40. final FontDescription desc;
  41. final TextBuffer buffer;
  42. final TextTag colorText, italics, background, mleft;
  43. TextIter pointer;
  44. view = new TextView();
  45. desc = new FontDescription("Sans, 10");
  46. view.modifyFont(desc);
  47. buffer = new TextBuffer();
  48. colorText = new TextTag();
  49. colorText.setForeground("blue");
  50. italics = new TextTag();
  51. italics.setStyle(Style.ITALIC);
  52. background = new TextTag();
  53. background.setBackground("lightgray");
  54. mleft = new TextTag();
  55. mleft.setLeftMargin(8);
  56. pointer = buffer.getIterStart();
  57. buffer.insert(pointer, "Plain text \n");
  58. buffer.insert(pointer, "Colored text\n", new TextTag[] {colorText, mleft});
  59. buffer.insert(pointer, "Text with colored background\n", background);
  60. buffer.insert(pointer, "Text in italics", italics);
  61. view.setBuffer(buffer);
  62. view.setPaddingAboveParagraph(5);
  63. add(view);
  64. }
  65. public static void main(String[] args) {
  66. Gtk.init(args);
  67. new GTextView();
  68. Gtk.main();
  69. }
  70. }

该示例显示了一些应用了不同TextTags的文本。

  1. view = new TextView();

TextView 小部件已创建。

  1. desc = new FontDescription("Sans, 10");
  2. view.modifyFont(desc);

我们将TextView的字体设置为 Sans 10。

  1. colorText = new TextTag();
  2. colorText.setForeground("blue");

TextTag小部件已创建。 如果应用,此标签会将文本的颜色设置为蓝色。

  1. buffer.insert(pointer, "Text in italics", italics);

在这里,我们为指定的文本应用斜体文本标签。

Java Gnome 中的高级小部件 - 图1

图:TextView

ListView

在下面的示例中,我们在列表视图模式下使用TreeView小部件。 ListStore用于存储数据。

listview.java

  1. package com.zetcode;
  2. import org.gnome.gdk.Event;
  3. import org.gnome.gtk.CellRendererText;
  4. import org.gnome.gtk.DataColumn;
  5. import org.gnome.gtk.DataColumnString;
  6. import org.gnome.gtk.Gtk;
  7. import org.gnome.gtk.ListStore;
  8. import org.gnome.gtk.Statusbar;
  9. import org.gnome.gtk.TreeIter;
  10. import org.gnome.gtk.TreePath;
  11. import org.gnome.gtk.TreeView;
  12. import org.gnome.gtk.TreeViewColumn;
  13. import org.gnome.gtk.VBox;
  14. import org.gnome.gtk.Widget;
  15. import org.gnome.gtk.Window;
  16. import org.gnome.gtk.WindowPosition;
  17. /**
  18. * ZetCode Java Gnome tutorial
  19. *
  20. * This program demonstrates the
  21. * TreeView widget in listview mode.
  22. *
  23. * @author jan bodnar
  24. * website zetcode.com
  25. * last modified March 2009
  26. */
  27. public class GListView extends Window {
  28. private Actress[] actresses =
  29. {
  30. new Actress("Jessica Alba", "Pomona", 1981),
  31. new Actress("Sigourney Weaver", "New York", 1949),
  32. new Actress("Angelina Jolie", "Los Angeles", 1975),
  33. new Actress("Natalie Portman", "Jerusalem", 1981),
  34. new Actress("Rachel Weissz", "London", 1971),
  35. new Actress("Scarlett Johansson", "New York", 1984)
  36. };
  37. private Statusbar statusbar;
  38. public GListView() {
  39. setTitle("ListView");
  40. initUI();
  41. connect(new Window.DeleteEvent() {
  42. public boolean onDeleteEvent(Widget source, Event event) {
  43. Gtk.mainQuit();
  44. return false;
  45. }
  46. });
  47. setDefaultSize(350, 300);
  48. setPosition(WindowPosition.CENTER);
  49. showAll();
  50. }
  51. public void initUI() {
  52. final TreeView view;
  53. final ListStore model;
  54. TreeIter row;
  55. CellRendererText renderer;
  56. TreeViewColumn column;
  57. final DataColumnString nameCol;
  58. final DataColumnString placeCol;
  59. final DataColumnString yearCol;
  60. statusbar = new Statusbar();
  61. model = new ListStore(new DataColumn[] {
  62. nameCol = new DataColumnString(),
  63. placeCol = new DataColumnString(),
  64. yearCol = new DataColumnString(),
  65. });
  66. for (Actress act : actresses) {
  67. row = model.appendRow();
  68. model.setValue(row, nameCol, act.name);
  69. model.setValue(row, placeCol, act.place);
  70. model.setValue(row, yearCol, String.valueOf(act.year));
  71. }
  72. view = new TreeView(model);
  73. column = view.appendColumn();
  74. column.setTitle("Name");
  75. renderer = new CellRendererText(column);
  76. renderer.setText(nameCol);
  77. column = view.appendColumn();
  78. column.setTitle("Place");
  79. renderer = new CellRendererText(column);
  80. renderer.setText(placeCol);
  81. column = view.appendColumn();
  82. column.setTitle("Year");
  83. renderer = new CellRendererText(column);
  84. renderer.setText(yearCol);
  85. view.connect(new TreeView.RowActivated() {
  86. public void onRowActivated(TreeView treeView,
  87. TreePath treePath,
  88. TreeViewColumn treeViewColumn) {
  89. final TreeIter row;
  90. final String place;
  91. final String name;
  92. final String year;
  93. final String text;
  94. row = model.getIter(treePath);
  95. place = model.getValue(row, placeCol);
  96. name = model.getValue(row, nameCol);
  97. year = model.getValue(row, yearCol);
  98. text = name + ", " + place + ", " + year;
  99. statusbar.setMessage(text);
  100. }
  101. });
  102. VBox vbox = new VBox(false, 0);
  103. vbox.packStart(view);
  104. vbox.packStart(statusbar, false, false, 0);
  105. add(vbox);
  106. }
  107. class Actress {
  108. public String name;
  109. public String place;
  110. public int year;
  111. Actress(String name, String place, int year) {
  112. this.name = name;
  113. this.place = place;
  114. this.year = year;
  115. }
  116. }
  117. public static void main(String[] args) {
  118. Gtk.init(args);
  119. new GListView();
  120. Gtk.main();
  121. }
  122. }

在我们的示例中,我们在TreeView小部件中显示了六个女演员的列表。 每行分别显示名称,出生地和出生年份。

  1. class Actress {
  2. public String name;
  3. public String place;
  4. public int year;
  5. ...
  6. }

Actress类用于存储有关女演员的数据。

  1. model = new ListStore(new DataColumn[] {
  2. nameCol = new DataColumnString(),
  3. placeCol = new DataColumnString(),
  4. yearCol = new DataColumnString(),
  5. });

这些行声明模型具有三个字符串数据列。

  1. for (Actress act : actresses) {
  2. row = model.appendRow();
  3. model.setValue(row, nameCol, act.name);
  4. model.setValue(row, placeCol, act.place);
  5. model.setValue(row, yearCol, String.valueOf(act.year));
  6. }

该模型充满了女演员的数据。

  1. column = view.appendColumn();
  2. column.setTitle("Name");
  3. renderer = new CellRendererText(column);
  4. renderer.setText(nameCol);

将创建一个带有"Name"标题的新列。 每列都有一个单元格渲染器。 单元格渲染器负责显示数据。 在我们的情况下,我们有最常用的单元格渲染器CellRendererText。 顾名思义,它在TreeViewColumn中显示文本数据。

  1. row = model.getIter(treePath);
  2. place = model.getValue(row, placeCol);
  3. name = model.getValue(row, nameCol);
  4. year = model.getValue(row, yearCol);
  5. text = name + ", " + place + ", " + year;
  6. statusbar.setMessage(text);

如果双击某个项目,则会在状态栏中显示整行。

Java Gnome 中的高级小部件 - 图2

图:ListView

TreeView

在本章的最后一个示例中,我们使用TreeView小部件显示分层的数据树。

tree.java

  1. package com.zetcode;
  2. import org.gnome.gdk.Event;
  3. import org.gnome.gtk.CellRendererText;
  4. import org.gnome.gtk.DataColumn;
  5. import org.gnome.gtk.DataColumnString;
  6. import org.gnome.gtk.Gtk;
  7. import org.gnome.gtk.Statusbar;
  8. import org.gnome.gtk.TreeIter;
  9. import org.gnome.gtk.TreePath;
  10. import org.gnome.gtk.TreeStore;
  11. import org.gnome.gtk.TreeView;
  12. import org.gnome.gtk.TreeViewColumn;
  13. import org.gnome.gtk.VBox;
  14. import org.gnome.gtk.Widget;
  15. import org.gnome.gtk.Window;
  16. import org.gnome.gtk.WindowPosition;
  17. /**
  18. * ZetCode Java Gnome tutorial
  19. *
  20. * This program demonstrates the
  21. * TreeView widget in tree view mode.
  22. *
  23. * @author jan bodnar
  24. * website zetcode.com
  25. * last modified March 2009
  26. */
  27. public class GTree extends Window {
  28. private DataColumnString dataCol;
  29. private TreeStore model;
  30. private TreeIter row;
  31. private TreeView view;
  32. private TreeIter rowChild;
  33. private TreeViewColumn column;
  34. private CellRendererText renderer;
  35. private Statusbar statusbar;
  36. public GTree() {
  37. setTitle("GTree");
  38. initUI();
  39. connect(new Window.DeleteEvent() {
  40. public boolean onDeleteEvent(Widget source, Event event) {
  41. Gtk.mainQuit();
  42. return false;
  43. }
  44. });
  45. setDefaultSize(350, 300);
  46. setPosition(WindowPosition.CENTER);
  47. showAll();
  48. }
  49. public void initUI() {
  50. statusbar = new Statusbar();
  51. model = new TreeStore(new DataColumn[] {
  52. dataCol = new DataColumnString(),
  53. });
  54. row = model.appendRow();
  55. model.setValue(row, dataCol, "Scripting languages");
  56. rowChild = model.appendChild( row );
  57. model.setValue(rowChild, dataCol, "PHP");
  58. rowChild = model.appendChild( row );
  59. model.setValue(rowChild, dataCol, "Python");
  60. rowChild = model.appendChild( row );
  61. model.setValue(rowChild, dataCol, "PERL");
  62. rowChild = model.appendChild( row );
  63. model.setValue(rowChild, dataCol, "Ruby");
  64. row = model.appendRow();
  65. model.setValue(row, dataCol, "Compiling languages");
  66. rowChild = model.appendChild( row );
  67. model.setValue( rowChild, dataCol, "Java");
  68. rowChild = model.appendChild( row );
  69. model.setValue( rowChild, dataCol, "C++");
  70. rowChild = model.appendChild( row );
  71. model.setValue( rowChild, dataCol, "C#");
  72. rowChild = model.appendChild( row );
  73. model.setValue( rowChild, dataCol, "C");
  74. view = new TreeView(model);
  75. column = view.appendColumn();
  76. column.setTitle("Programming Languages");
  77. renderer = new CellRendererText(column);
  78. renderer.setText(dataCol);
  79. view.connect(new TreeView.RowActivated() {
  80. public void onRowActivated(TreeView treeView,
  81. TreePath treePath,
  82. TreeViewColumn treeViewColumn) {
  83. final TreeIter row;
  84. final String text;
  85. row = model.getIter(treePath);
  86. text = model.getValue(row, dataCol);
  87. statusbar.setMessage(text);
  88. }
  89. });
  90. VBox vbox = new VBox(false, 0);
  91. vbox.packStart(view);
  92. vbox.packStart(statusbar, false, false, 0);
  93. add(vbox);
  94. }
  95. public static void main(String[] args) {
  96. Gtk.init(args);
  97. new GTree();
  98. Gtk.main();
  99. }
  100. }

这次我们使用TreeView小部件显示分层数据。

  1. model = new TreeStore(new DataColumn[] {
  2. dataCol = new DataColumnString(),
  3. });

在这里,我们声明TreeStore具有一个字符串数据列。

  1. model.setValue(row, dataCol, "Scripting languages");

我们附加一个顶级节点。

  1. rowChild = model.appendChild( row );
  2. model.setValue(rowChild, dataCol, "PHP");

我们为顶层节点附加一个子行。

  1. view = new TreeView(model);

我们创建TreeView小部件的实例。

  1. column = view.appendColumn();
  2. column.setTitle("Programming Languages");
  3. renderer = new CellRendererText(column);
  4. renderer.setText(dataCol);

我们创建列和单元格渲染器。

  1. row = model.getIter(treePath);
  2. text = model.getValue(row, dataCol);
  3. statusbar.setMessage(text);

双击后,我们将在状态栏中显示选定的节点。

Java Gnome 中的高级小部件 - 图3

图:Tree

在本章中,我们讨论的是高级 Java Gnome 小部件。