- 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
Widgets in Java Gnome
In this part of the Java Gnome programming tutorial, we will introduce some widgets.
Widgets are basic building blocks of a GUI application. Over the years, several widgets became a standard in all toolkits on all OS platforms. For example a button, a check box or a scroll bar. The GTK+ toolkit's philosophy is to keep the number of widgets at a minimum level. More specialised widgets are created as custom widgets.
Label
The Label
widget shows text.
label.java
package com.zetcode; import org.gnome.gdk.Event; import org.gnome.gtk.Gtk; import org.gnome.gtk.Label; import org.gnome.gtk.Widget; import org.gnome.gtk.Window; import org.gnome.gtk.WindowPosition; /** * ZetCode Java Gnome tutorial * * This program uses Label widget * to display text. * * @author jan bodnar * website zetcode.com * last modified March 2009 */ public class GLabel extends Window { public GLabel() { setTitle("Death song"); initUI(); connect(new Window.DeleteEvent() { public boolean onDeleteEvent(Widget source, Event event) { Gtk.mainQuit(); return false; } }); setPosition(WindowPosition.CENTER); showAll(); } public void initUI() { Label lyrics = new Label("Lets sing the death song kids\n\n" + "We light a candle on an earth\n" + "We made into hell\n" + "And pretend that were in heaven\n" + "Each time we do we get\n" + "The blind mans ticket\n" + "And we know that nothings true\n" + "I saw priest kill a cop on the tv\n" + "And I know now theyre our heroes too\n\n" + "We sing the death song kids\n" + "Because weve got no future\n" + "And we want to be just like you\n" + "And we want to be just like you\n"); add(lyrics); setBorderWidth(8); } public static void main(String[] args) { Gtk.init(args); new GLabel(); Gtk.main(); } }
The code example shows some lyrics on the window.
Label lyrics = new Label("Lets sing the death song kids\n\n" + "We light a candle on an earth\n" + ...
This is the text we will display in the Label widget.
setBorderWidth(8);
The Label
is surrounded by some empty space.

HSeparator
HSeparator
is an ornament widget, which can be used to separate items on the window.
separator.java
package com.zetcode; import org.gnome.gdk.Event; import org.gnome.gtk.Gtk; import org.gnome.gtk.HSeparator; import org.gnome.gtk.Label; import org.gnome.gtk.VBox; import org.gnome.gtk.Widget; import org.gnome.gtk.Window; import org.gnome.gtk.WindowPosition; /** * Java Gnome tutorial * * This program shows how to use * a horizontal separator. * * @author jan bodnar * website zetcode.com * last modified March 2009 */ public class GHSeparator extends Window { private final int VERTICAL_SPACE = 15; private final int BORDER_AROUND = 25; private final float Y_ALIGN = 0f; private final float X_ALIGN = 0f; public GHSeparator() { setTitle("HSeparator"); 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, VERTICAL_SPACE); Label zinc = new Label("Zinc is a moderately reactive, blue" + "gray metal that tarnishes in moist air and burns in air of zinc " + "oxide. It reacts with acids, alkalis and other non-metals. If not" + "completely pure, zinc reacts with dilute acids to release hydrogen."); zinc.setLineWrap(true); zinc.setAlignment(X_ALIGN, Y_ALIGN); vbox.packStart(zinc); HSeparator hsep = new HSeparator(); vbox.packStart(hsep); Label copper = new Label("Copper is an essential trace nutrient to" + "all high plants and animals. In animals, including humans," + "it is found primarily in the bloodstream, as a co-factor in various" + "enzymes, and in copper-based pigments. However, in sufficient" + "amounts, copper can be poisonous and even fatal to organisms."); copper.setAlignment(X_ALIGN, Y_ALIGN); copper.setLineWrap(true); vbox.packStart(copper); add(vbox); setResizable(false); setBorderWidth(BORDER_AROUND); } public static void main(String[] args) { Gtk.init(args); new GHSeparator(); Gtk.main(); } }
In our code example, we have descriptions of two chemical elements, which are separated by the horizontal separator.
zinc.setLineWrap(true);
This line wraps the text. This is necessary for longer texts.
zinc.setAlignment(X_ALIGN, Y_ALIGN);
This code line aligns the text to the left.
HSeparator hsep = new HSeparator(); vbox.packStart(hsep);
The HSeparator
widget is created and placed in between the two label widgets.

CheckButton
CheckButton
is a widget that has two states: on and off. The on state is visualised by a check mark. It is used to denote some boolean property.
checkbutton.java
package com.zetcode; import org.gnome.gdk.Event; import org.gnome.gtk.CheckButton; import org.gnome.gtk.Fixed; import org.gnome.gtk.Gtk; import org.gnome.gtk.ToggleButton; import org.gnome.gtk.Widget; import org.gnome.gtk.Window; import org.gnome.gtk.WindowPosition; /** * ZetCode Java Gnome tutorial * * This program uses a CheckButton to * toggle the visibility of a window title. * * @author jan bodnar * website zetcode.com * last modified March 2009 */ public class GCheckButton extends Window implements ToggleButton.Toggled { CheckButton check; Window window; private String title = "Check Button"; public GCheckButton() { setTitle(title); initUI(); connect(new Window.DeleteEvent() { public boolean onDeleteEvent(Widget source, Event event) { Gtk.mainQuit(); return false; } }); setSizeRequest(250, 200); showAll(); } public void initUI() { setBorderWidth(10); Fixed fixed = new Fixed(); check = new CheckButton("Show title"); check.setActive(true); check.connect(this); check.setCanFocus(false); fixed.put(check, 50, 50); add(fixed); setPosition(WindowPosition.CENTER); } public void onToggled(ToggleButton toggleButton) { if (check.getActive()) { setTitle(title); } else { setTitle(""); } } public static void main(String[] args) { Gtk.init(args); new GCheckButton(); Gtk.main(); } }
We will display a title in the titlebar of the window, depending on the state of the CheckButton
.
check = new CheckButton("Show title");
CheckButton
widget is created.
check.setActive(true);
The title is visible by default, so we check the check button by default.
if (check.getActive()) { setTitle(title); } else { setTitle(""); }
Depending on the state of the CheckButton
, we show or hide the title of the window.

TextComboBox
TextComboBox
is a widget that allows the user to choose from a list of textual options.
textcombobox.java
package com.zetcode; import org.gnome.gdk.Event; import org.gnome.gtk.ComboBox; import org.gnome.gtk.Fixed; import org.gnome.gtk.Gtk; import org.gnome.gtk.Label; import org.gnome.gtk.TextComboBox; import org.gnome.gtk.Widget; import org.gnome.gtk.Window; import org.gnome.gtk.WindowPosition; /** * ZetCode Java Gnome tutorial * * This program shows how to use * a TextComboBox. * * @author jan bodnar * website zetcode.com * last modified March 2009 */ public class GTextComboBox extends Window implements ComboBox.Changed { TextComboBox cb; Label label; public GTextComboBox() { setTitle("TextComboBox"); initUI(); setSizeRequest(250, 200); setPosition(WindowPosition.CENTER); showAll(); } public void initUI() { connect(new Window.DeleteEvent() { public boolean onDeleteEvent(Widget source, Event event) { Gtk.mainQuit(); return false; } }); Fixed fixed = new Fixed(); label = new Label(""); fixed.put(label, 55, 130); cb = new TextComboBox(); cb.appendText("Ubuntu"); cb.appendText("Mandriva"); cb.appendText("Fedora"); cb.appendText("Mint"); cb.appendText("Debian"); cb.appendText("Gentoo"); cb.connect(this); fixed.put(cb, 50, 50); add(fixed); } public void onChanged(ComboBox comboBox) { String text = cb.getActiveText(); label.setLabel(text); } public static void main(String[] args) { Gtk.init(args); new GTextComboBox(); Gtk.main(); } }
The example shows a text combo box and a label. The text combo box has a list of six options. These are the names of Linux Distros. The label widget shows the selected option from the text combo box.
cb = new TextComboBox();
The TextComboBox
widget is created.
cb.appendText("Ubuntu");
The appendText()
method is called to fill the text combo box.
public void onChanged(ComboBox comboBox) { String text = cb.getActiveText(); label.setLabel(text); }
The onChanged()
method is called, when we select an option from the text combo box. We get the selected text and set it to the label widget.

Image
The next example introduces the Image
widget. This widget displays pictures.
image.java
package com.zetcode; import org.gnome.gdk.Event; import org.gnome.gtk.Gtk; import org.gnome.gtk.Image; import org.gnome.gtk.Widget; import org.gnome.gtk.Window; import org.gnome.gtk.WindowPosition; /** * ZetCode Java Gnome tutorial * * This program shows an image of a castle * in the window. * * @author jan bodnar * website zetcode.com * last modified March 2009 */ public class GImage extends Window { public GImage() { setTitle("Red Rock"); initUI(); setPosition(WindowPosition.CENTER); showAll(); } public void initUI() { connect(new Window.DeleteEvent() { public boolean onDeleteEvent(Widget source, Event event) { Gtk.mainQuit(); return false; } }); Image image = new Image("redrock.png"); int width = image.getRequisition().getWidth(); int height = image.getRequisition().getHeight(); setSizeRequest(width, height); add(image); setBorderWidth(2); } public static void main(String[] args) { Gtk.init(args); new GImage(); Gtk.main(); } }
We show the Red Rock castle in the window.
Image image = new Image("redrock.png");
We create an instance of the Image
widget. Notice that we do not catch for any exceptions. The Image widget already handles them. If the Image widget cannot find the redrock.png image, it will display a missing image by default.
Image image = new Image(castle); Add(image);
Image
widget is created and added to the window.

In this chapter, we showed the first pack of basic widgets of the Java Gnome programming library.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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