- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
Layout management II in Java Gnome
In this chapter we will continue working with layouts in Java Gnome toolkit.
New Folder
The following code example will create a new folder dialog.
newfolder.java
package com.zetcode; import org.gnome.gdk.Event; import org.gnome.gtk.Alignment; import org.gnome.gtk.Button; import org.gnome.gtk.Entry; import org.gnome.gtk.Gtk; import org.gnome.gtk.HBox; import org.gnome.gtk.Label; import org.gnome.gtk.TextView; import org.gnome.gtk.VBox; import org.gnome.gtk.Widget; import org.gnome.gtk.Window; import org.gnome.gtk.WindowPosition; /** * ZetCode Java Gnome tutorial * * This program creates a new * folder window. * * @author jan bodnar * website zetcode.com * last modified March 2009 */ public class GNewFolder extends Window { public GNewFolder() { setTitle("New Folder"); initUI(); connect(new Window.DeleteEvent() { public boolean onDeleteEvent(Widget source, Event event) { Gtk.mainQuit(); return false; } }); setDefaultSize(270, 290); setPosition(WindowPosition.CENTER); showAll(); } public void initUI() { VBox vbox = new VBox(false, 10); Label label = new Label("Name:"); Entry entry = new Entry(); HBox hbox1 = new HBox(false, 5); hbox1.packStart(label, false, false, 0); hbox1.packStart(entry, true, true, 0); vbox.packStart(hbox1, false, false, 0); TextView tw = new TextView(); vbox.packStart(tw); HBox hbox2 = new HBox(true, 5); Button ok = new Button("OK"); Button close = new Button("Close"); close.setSizeRequest(65, 30); hbox2.packStart(ok); hbox2.packStart(close); Alignment halign = new Alignment(1, 0, 0, 0); halign.add(hbox2); vbox.packStart(halign, false, false, 0); add(vbox); setBorderWidth(10); } public static void main(String[] args) { Gtk.init(args); new GNewFolder(); Gtk.main(); } }
This is the new folder window in Java Gnome.
VBox vbox = new VBox(false, 10);
A vertical box is the base container.
HBox hbox1 = new HBox(false, 5); hbox1.packStart(label, false, false, 0); hbox1.packStart(entry, true, true, 0); vbox.packStart(hbox1, false, false, 0);
Label and entry widgets are placed inside a horizontal box. Label should retain its default size, the entry widget is horizontally expandable.
TextView tw = new TextView(); vbox.packStart(tw);
The text view takes the bulk of the area. It is horizontally and vertically expandable.
hbox2.packStart(ok); hbox2.packStart(close);
OK and close buttons go into the horizontal box.
Alignment halign = new Alignment(1, 0, 0, 0); halign.add(hbox2); vbox.packStart(halign, false, false, 0);
The horizontal box mentioned above is added into an alignment widget. This will make the buttons aligned to the right and make them retain their default sizes.

Windows
Next we will create a more advanced example. We show a window, that can be found in the JDeveloper IDE.
windows.java
package com.zetcode; import org.gnome.gdk.Color; import org.gnome.gdk.Event; import org.gnome.gtk.Alignment; import org.gnome.gtk.Button; import org.gnome.gtk.Gtk; import org.gnome.gtk.HBox; import org.gnome.gtk.Label; import org.gnome.gtk.StateType; import org.gnome.gtk.TextView; import org.gnome.gtk.VBox; import org.gnome.gtk.Widget; import org.gnome.gtk.Window; import org.gnome.gtk.WindowPosition; public class GWindows extends Window { public GWindows() { setTitle("Windows"); initUI(); connect(new Window.DeleteEvent() { public boolean onDeleteEvent(Widget source, Event event) { Gtk.mainQuit(); return false; } }); setDefaultSize(350, 300); setPosition(WindowPosition.CENTER); showAll(); } public void initUI() { VBox vbox = new VBox(false, 9); // first row Label windows = new Label("Windows"); windows.setAlignment(0, 0); vbox.packStart(windows, false, false, 5); // second row HBox hbox1 = new HBox(false, 9); TextView view = new TextView(); hbox1.packStart(view); VBox vbox2 = new VBox(false, 5); Button activate = new Button("Activate"); activate.setSizeRequest(80, 30); Alignment align2 = new Alignment(0, 0, 0, 0); align2.add(activate); Button close = new Button("Close"); close.setSizeRequest(80, 30); Alignment align3 = new Alignment(0, 0, 0, 0); align3.add(close); vbox2.packStart(align2, false, false, 0); vbox2.packStart(align3, false, false, 0); hbox1.packStart(vbox2, false, false, 0); vbox.packStart(hbox1); // third row HBox hbox2 = new HBox(true, 0); Button help = new Button("Help"); help.setSizeRequest(80, 30); Alignment alignHelp = new Alignment(0, 0, 0, 0); alignHelp.add(help); Button ok = new Button("OK"); ok.setSizeRequest(80, 30); Alignment alignOk = new Alignment(1, 0, 0, 0); alignOk.add(ok); hbox2.packStart(alignHelp); hbox2.packStart(alignOk); vbox.packStart(hbox2, false, false, 0); add(vbox); setBorderWidth(9); } public static void main(String[] args) { Gtk.init(args); new GWindows(); Gtk.main(); } }
We divide the window layout into several parts. The main container is the vertical box. We put three rows into this vertical box. The first is a simple label widget. The second one is a horizontal box which consists of a view widget and an additional vertical box. Finally, the third row is a horizontal box which has two buttons.
VBox vbox = new VBox(false, 9);
This is the main vertical box.
Button activate = new Button("Activate"); activate.setSizeRequest(80, 30); Alignment align2 = new Alignment(0, 0, 0, 0); align2.add(activate);
The activate button is resized to have 80x30 px. It is placed inside an Alignment
widget, so that it does not shrink or grow.
Button ok = new Button("OK"); ok.setSizeRequest(80, 30); Alignment alignOk = new Alignment(1, 0, 0, 0); alignOk.add(ok);
OK button is aligned to the right.

Find/Replace window
In the following example, we will create a window that you can find in the Eclipse IDE.
replace.java
package com.zetcode; import org.gnome.gdk.Event; import org.gnome.gtk.Alignment; import org.gnome.gtk.AttachOptions; import org.gnome.gtk.Button; import org.gnome.gtk.CheckButton; import org.gnome.gtk.Frame; import org.gnome.gtk.Gtk; import org.gnome.gtk.HBox; import org.gnome.gtk.Label; import org.gnome.gtk.Table; import org.gnome.gtk.TextComboBox; import org.gnome.gtk.VBox; import org.gnome.gtk.Widget; import org.gnome.gtk.Window; import org.gnome.gtk.WindowPosition; /** * ZetCode Java Gnome tutorial * * This program creates a complicated * layout. It uses both box and table * containers. * * @author jan bodnar * website zetcode.com * last modified March 2009 */ public class GReplace extends Window { public GReplace() { setTitle("Replace/Find"); initUI(); connect(new Window.DeleteEvent() { public boolean onDeleteEvent(Widget source, Event event) { Gtk.mainQuit(); return false; } }); setPosition(WindowPosition.CENTER); showAll(); } public void initUI() { VBox vbox = new VBox(false, 10); Label findLabel = new Label("Find"); Label replaceLabel = new Label("Replace With"); TextComboBox combo1 = new TextComboBox(); combo1.appendText(""); TextComboBox combo2 = new TextComboBox(); combo2.appendText(""); HBox hbox1 = new HBox(false, 10); hbox1.packStart(findLabel, false, false, 0); hbox1.packStart(combo1); HBox hbox2 = new HBox(false, 10); hbox2.packStart(replaceLabel, false, false, 0); hbox2.packStart(combo2); vbox.packStart(hbox1, false, false, 0); vbox.packStart(hbox2, false, false, 0); // second row Frame direction = new Frame("Direction"); VBox v1 = new VBox(true, 0); v1.setBorderWidth(5); CheckButton cb1 = new CheckButton("Forward"); CheckButton cb2 = new CheckButton("Backward"); v1.packStart(cb1); v1.packStart(cb2); direction.add(v1); Frame scope = new Frame("Scope"); VBox v2 = new VBox(true, 0); v2.setBorderWidth(5); CheckButton cb3 = new CheckButton("All"); CheckButton cb4 = new CheckButton("Selected Lines"); v2.packStart(cb3); v2.packStart(cb4); scope.add(v2); HBox framesBox = new HBox(true, 5); framesBox.packStart(direction); framesBox.packStart(scope); vbox.packStart(framesBox, false, false, 0); // third row Frame options = new Frame("Options"); Table table1 = new Table(3, 2, false); CheckButton cb5 = new CheckButton("Case Sensitive"); CheckButton cb6 = new CheckButton("Whole World"); CheckButton cb7 = new CheckButton("Regular Expressions"); CheckButton cb8 = new CheckButton("Wrap Search"); CheckButton cb9 = new CheckButton("Incremental"); table1.attach(cb5, 0, 1, 0, 1, AttachOptions.FILL, AttachOptions.FILL, 0, 0); table1.attach(cb6, 0, 1, 1, 2, AttachOptions.FILL, AttachOptions.FILL, 0, 0); table1.attach(cb7, 0, 1, 2, 3, AttachOptions.FILL, AttachOptions.FILL, 0, 0); table1.attach(cb8, 1, 2, 0, 1, AttachOptions.FILL, AttachOptions.FILL, 0, 0); table1.attach(cb9, 1, 2, 1, 2, AttachOptions.FILL, AttachOptions.FILL, 0, 0); table1.setBorderWidth(5); options.add(table1); vbox.packStart(options, true, true, 0); // fourth row Table table2 = new Table(2, 2, true); Button find = new Button("Find"); Button replace = new Button("Replace"); Button replaceFind = new Button("Replace/Find"); Button replaceAll = new Button("Replace All"); table2.attach(find, 0, 1, 0, 1); table2.attach(replace, 0, 1, 1, 2); table2.attach(replaceFind, 1, 2, 0, 1); table2.attach(replaceAll, 1, 2, 1, 2); vbox.packStart(table2, false, false, 0); // fifth row Button close = new Button("Close"); close.setSizeRequest(80, -1); Alignment halign = new Alignment(1, 0, 0, 0); halign.add(close); vbox.packStart(halign); add(vbox); setBorderWidth(15); } public static void main(String[] args) { Gtk.init(args); new GReplace(); Gtk.main(); } }
This example looks quite complicated. But if you divide the layout into parts, it will get easier. In our case, we separate the layout into five rows.
HBox hbox1 = new HBox(false, 10); hbox1.packStart(findLabel, false, false, 0); hbox1.packStart(combo1);
In the first row, we create two labels and two combo boxes. In the above code, the label retains its position and size. The combo box expands and grows as we resize the window. All this is controlled by the expand and fill parameters.
In the second row, we have two frames. Each of the frames has two check boxes.
VBox v1 = new VBox(true, 0); v1.setBorderWidth(5); CheckButton cb1 = new CheckButton("Forward"); CheckButton cb2 = new CheckButton("Backward"); v1.packStart(cb1); v1.packStart(cb2); direction.add(v1);
We set a vertical box for the frame widget. Inside this box, we place the check buttons.
HBox framesBox = new HBox(true, 5); framesBox.packStart(direction); framesBox.packStart(scope); vbox.packStart(framesBox, false, false, 0);
The two frames go inside a horizontal box. And the horizontal box goes into the base vertical box container.
The third row is a frame, which expands both horizontally and vertically. This time we place five check boxes inside the frame. For this, we use the table container.
The fourth row consists of four buttons. They are all of the same size. Table widget is ideal for this kind of layout.
Button close = new Button("Close"); close.setSizeRequest(80, -1); Alignment halign = new Alignment(1, 0, 0, 0); halign.add(close);
Finally, the last row. We use the Alignment
widget to right align the button. The Alignment
widget also causes the button to retain its initial value. In other words, it does not grow or shrink.

In this part of the Java Gnome tutorial, we created some more sophisticated layouts.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论