Java Gnome FAQ Layout management

Материал из Wiki.crossplatform.ru

(Различия между версиями)
Перейти к: навигация, поиск
(Новая: In this chapter we will show how to lay out widgets in windows or dialogs. When we design the GUI of our application, we decide what widgets we will use and how we will organize those ...)
(нарушало авторские права)
 
Строка 1: Строка 1:
-
In this chapter we will show how to lay out widgets in windows or dialogs.
 
-
When we design the GUI of our application, we decide what widgets we will use and how we will organize those widgets in the application.
 
-
To organize our widgets, we use specialized non visible widgets called <b>layout containers</b>. In this chapter, we will mention <b>Alignment</b>, <b>HBox</b> and <b>Table</b>.
 
-
 
-
== Fixed ==
 
-
 
-
The <b>Fixed</b> container places child widgets at fixed positions and with fixed sizes.
 
-
This container performs no automatic layout management. In most applications, we don't use  this container. There are some specialized areas, where we use it.
 
-
For example games, specialized applications that work with diagrams, resizable components that can be moved (like a chart in a spreadsheet application), small educational examples.
 
-
 
-
<source lang="java">
 
-
package com.zetcode;
 
-
 
-
import java.io.FileNotFoundException;
 
-
 
-
import org.gnome.gdk.Color;
 
-
import org.gnome.gdk.Event;
 
-
import org.gnome.gdk.Pixbuf;
 
-
import org.gnome.gtk.Fixed;
 
-
import org.gnome.gtk.Gtk;
 
-
import org.gnome.gtk.Image;
 
-
import org.gnome.gtk.StateType;
 
-
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
 
-
* the Fixed container.
 
-
*
 
-
* @author jan bodnar
 
-
* website zetcode.com
 
-
* last modified March 2009
 
-
*/
 
-
 
-
public class GAbsolute extends Window {
 
-
 
-
    private Pixbuf rotunda;
 
-
    private Pixbuf bardejov;
 
-
    private Pixbuf mincol;
 
-
 
-
 
-
    public GAbsolute() {
 
-
   
 
-
        setTitle("Absolute");
 
-
   
 
-
        initUI();
 
-
   
 
-
        connect(new Window.DeleteEvent() {
 
-
            public boolean onDeleteEvent(Widget source, Event event) {
 
-
                Gtk.mainQuit();
 
-
                return false;
 
-
            }
 
-
        });
 
-
   
 
-
        setDefaultSize(300, 280);
 
-
        setPosition(WindowPosition.CENTER);
 
-
        showAll();
 
-
    }
 
-
   
 
-
    public void initUI() {
 
-
   
 
-
        modifyBackground(StateType.NORMAL, new Color(15000, 15000, 15000));
 
-
       
 
-
        try {
 
-
            bardejov = new Pixbuf("bardejov.jpg");
 
-
            rotunda = new Pixbuf("rotunda.jpg");
 
-
            mincol = new Pixbuf("mincol.jpg");
 
-
        } catch (FileNotFoundException e) {
 
-
            System.out.println("Could not load images.");
 
-
            System.out.println(e.getMessage());
 
-
        }
 
-
       
 
-
        Image image1 = new Image(bardejov);
 
-
        Image image2 = new Image(rotunda);
 
-
        Image image3 = new Image(mincol);
 
-
       
 
-
        Fixed fix = new Fixed();
 
-
       
 
-
        fix.put(image1, 20, 20);
 
-
        fix.put(image2, 40, 160);
 
-
        fix.put(image3, 170, 50);
 
-
       
 
-
        add(fix);
 
-
    }
 
-
   
 
-
    public static void main(String[] args) {
 
-
        Gtk.init(args);
 
-
        new GAbsolute();
 
-
        Gtk.main();
 
-
    }
 
-
}
 
-
</source>
 
-
 
-
In our example, we show three small images on the window. We explicitely specify the x, y coordinates, where we place these images.
 
-
 
-
<source lang="java">
 
-
modifyBackground(StateType.NORMAL, new Color(15000, 15000, 15000));
 
-
</source>
 
-
 
-
For better visual experience, we change the background color to dark gray.
 
-
 
-
<source lang="java">
 
-
bardejov = new Pixbuf("bardejov.jpg");
 
-
</source>
 
-
 
-
We load the image from the disk to the <b>Pixbuf</b> object.
 
-
 
-
<source lang="java">
 
-
Image image1 = new Image(bardejov);
 
-
Image image2 = new Image(rotunda);
 
-
Image image3 = new Image(mincol);
 
-
</source>
 
-
 
-
The <b>Image</b> is a widget, that is used to display images. It takes <b>Pixbuf</b> object in the constructor.
 
-
 
-
<source lang="java">
 
-
Fixed fix = new Fixed();
 
-
 
-
</source>
 
-
 
-
We create the <b>Fixed</b> container.
 
-
 
-
<source lang="java">
 
-
fix.put(image1, 20, 20);
 
-
</source>
 
-
 
-
We place the first image at x=20, y=20 coordinates.
 
-
 
-
<source lang="java">
 
-
add(fix);
 
-
</source>
 
-
 
-
Finally, we add the <b>Fixed</b> container to the Window.
 
-
 
-
[[image: Java_Gnome_faq_fixed.jpg| center]]
 
-
 
-
== Alignment ==
 
-
 
-
The <b>Alignment</b> container controls the  alignment and the size of it's child widget.
 
-
 
-
<source lang="java">
 
-
package com.zetcode;
 
-
 
-
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.VBox;
 
-
import org.gnome.gtk.Widget;
 
-
import org.gnome.gtk.Window;
 
-
import org.gnome.gtk.WindowPosition;
 
-
 
-
/**
 
-
* ZetCode Java Gnome tutorial
 
-
*
 
-
* This program places two buttons
 
-
* in the right bottom corner of
 
-
* the window.
 
-
*
 
-
* @author jan bodnar
 
-
* website zetcode.com
 
-
* last modified March 2009
 
-
*/
 
-
 
-
public class GAlignment extends Window {
 
-
 
-
    public GAlignment() {
 
-
   
 
-
        setTitle("Alignment");
 
-
       
 
-
        initUI();
 
-
       
 
-
        connect(new Window.DeleteEvent() {
 
-
            public boolean onDeleteEvent(Widget source, Event event) {
 
-
                Gtk.mainQuit();
 
-
                return false;
 
-
            }
 
-
        });
 
-
       
 
-
        setDefaultSize(260, 150);
 
-
        setPosition(WindowPosition.CENTER);
 
-
        showAll();
 
-
    }
 
-
   
 
-
    public void initUI() {
 
-
        VBox vbox = new VBox(false, 5);
 
-
        HBox hbox = new HBox(true, 3);
 
-
       
 
-
        vbox.packStart(new Label(""));
 
-
       
 
-
        Button ok = new Button("OK");
 
-
        ok.setSizeRequest(70, 30);
 
-
        Button close = new Button("Close");
 
-
       
 
-
        hbox.add(ok);
 
-
        hbox.add(close);
 
-
       
 
-
        Alignment halign = new Alignment(1, 0, 0, 0);
 
-
        halign.add(hbox);
 
-
       
 
-
        vbox.packStart(halign, false, false, 3);
 
-
 
-
        add(vbox);
 
-
        setBorderWidth(5);
 
-
    }
 
-
   
 
-
    public static void main(String[] args) {
 
-
        Gtk.init(args);
 
-
        new GAlignment();
 
-
        Gtk.main();
 
-
    }
 
-
}
 
-
</source>
 
-
 
-
In the code example, we place two buttons into the right bottom corner of the window. To accomplish this,  we use one horizontal box and one vertical box and  an alignment container.
 
-
 
-
<source lang="java">
 
-
vbox.packStart(new Label(""));
 
-
</source>
 
-
 
-
This line will put an empty label into the  vertical box. The label expands horizontally and vertically. This will make the next widget  appended to the vertical box appear at the bottom of the window.
 
-
 
-
<source lang="java">
 
-
Button ok = new Button("OK");
 
-
ok.setSizeRequest(70, 30);
 
-
Button close = new Button("Close");
 
-
 
 
-
hbox.add(ok);
 
-
hbox.add(close);
 
-
 
-
</source>
 
-
 
-
The two buttons are added to the horizontal box.
 
-
 
-
<source lang="java">
 
-
Alignment halign = new Alignment(1, 0, 0, 0);
 
-
halign.add(hbox);
 
-
</source>
 
-
 
-
The horizontal box is added to an alignment widget.
 
-
The alignment widget will align the buttons to the right. We must keep in mind that the alignment container takes only one child widget. That's why we must use boxes.
 
-
 
-
<source lang="java">
 
-
vbox.packStart(halign, false, false, 3);
 
-
 
-
</source>
 
-
 
-
The alignment widget is packed inside the vertical box.
 
-
 
-
[[image: Java_Gnome_faq_alignment.png| center]]
 
-
 
-
== Table ==
 
-
 
-
The <b>Table</b> widget arranges widgets in rows and columns.
 
-
 
-
<source lang="java">
 
-
package com.zetcode;
 
-
 
-
import org.gnome.gdk.Event;
 
-
import org.gnome.gtk.Button;
 
-
import org.gnome.gtk.Entry;
 
-
import org.gnome.gtk.Gtk;
 
-
import org.gnome.gtk.Label;
 
-
import org.gnome.gtk.Menu;
 
-
import org.gnome.gtk.MenuBar;
 
-
import org.gnome.gtk.MenuItem;
 
-
import org.gnome.gtk.Table;
 
-
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 creates a calculator skeleton
 
-
* with the Table container widget.
 
-
*
 
-
* @author jan bodnar
 
-
* website zetcode.com
 
-
* last modified March 2009
 
-
*/
 
-
 
-
public class GCalculator extends Window {
 
-
 
-
    public GCalculator() {
 
-
   
 
-
        setTitle("Calculator");
 
-
   
 
-
        initUI();
 
-
   
 
-
        connect(new Window.DeleteEvent() {
 
-
            public boolean onDeleteEvent(Widget source, Event event) {
 
-
                Gtk.mainQuit();
 
-
                return false;
 
-
            }
 
-
        });
 
-
   
 
-
        setDefaultSize(250, 230);
 
-
        setPosition(WindowPosition.CENTER);
 
-
        showAll();
 
-
    }
 
-
   
 
-
   
 
-
    public void initUI() {
 
-
 
-
        VBox vbox = new VBox(false, 2);
 
-
       
 
-
        MenuBar mb = new MenuBar();
 
-
        Menu filemenu = new Menu();
 
-
        MenuItem file = new MenuItem("File");
 
-
        file.setSubmenu(filemenu);
 
-
        mb.append(file);
 
-
 
-
        vbox.packStart(mb, false, false, 0);
 
-
 
-
        Table table = new Table(5, 4, true);
 
-
 
-
        table.attach(new Button("Cls"), 0, 1, 0, 1);
 
-
        table.attach(new Button("Bck"), 1, 2, 0, 1);
 
-
        table.attach(new Label(""), 2, 3, 0, 1);
 
-
        table.attach(new Button("Close"), 3, 4, 0, 1);
 
-
 
-
        table.attach(new Button("7"), 0, 1, 1, 2);
 
-
        table.attach(new Button("8"), 1, 2, 1, 2);
 
-
        table.attach(new Button("9"), 2, 3, 1, 2);
 
-
        table.attach(new Button("/"), 3, 4, 1, 2);
 
-
 
-
        table.attach(new Button("4"), 0, 1, 2, 3);
 
-
        table.attach(new Button("5"), 1, 2, 2, 3);
 
-
        table.attach(new Button("6"), 2, 3, 2, 3);
 
-
        table.attach(new Button("*"), 3, 4, 2, 3);
 
-
 
-
        table.attach(new Button("1"), 0, 1, 3, 4);
 
-
        table.attach(new Button("2"), 1, 2, 3, 4);
 
-
        table.attach(new Button("3"), 2, 3, 3, 4);
 
-
        table.attach(new Button("-"), 3, 4, 3, 4);
 
-
 
-
        table.attach(new Button("0"), 0, 1, 4, 5);
 
-
        table.attach(new Button("."), 1, 2, 4, 5);
 
-
        table.attach(new Button("="), 2, 3, 4, 5);
 
-
 
-
        table.attach(new Button("+"), 3, 4, 4, 5);
 
-
 
-
        vbox.packStart(new Entry(), false, false, 0);
 
-
        vbox.packStart(table, true, true, 0);
 
-
       
 
-
        add(vbox);
 
-
    }
 
-
   
 
-
    public static void main(String[] args) {
 
-
        Gtk.init(args);
 
-
        new GCalculator();
 
-
        Gtk.main();
 
-
    }
 
-
}
 
-
</source>
 
-
 
-
We use the <b>Table</b> widget to create a calculator skeleton.
 
-
 
-
<source lang="java">
 
-
Table table = new Table(5, 4, true);
 
-
</source>
 
-
 
-
We create a table widget with 5 rows and 4 columns. The third parameter is the homogenous parameter. If set to true, all the widgets in the table are of same size. The size of all widgets is equal to the largest  widget in the table container.
 
-
 
-
<source lang="java">
 
-
table.attach(new Button("Cls"), 0, 1, 0, 1);
 
-
</source>
 
-
 
-
We attach a button to the table container. To the top-left cell of the table.
 
-
The first two parameters are the left and right sides of the cell, the last two parameters are the top and left sides of the cell.
 
-
In other words, it goes to the first cell of the table container. At 0, 0.
 
-
 
-
<source lang="java">
 
-
vbox.packStart(new Entry(), false, false, 0);
 
-
</source>
 
-
 
-
We first pack the <b>Entry</b> widget.
 
-
 
-
<source lang="java">
 
-
vbox.packStart(table, true, true, 0);
 
-
</source>
 
-
 
-
Then we add the table widget. We make it expand all the remaining space.
 
-
 
-
[[image: Java_Gnome_faq_calculator.png| center]]
 
-
 
-
== Expand and fill ==
 
-
 
-
The following example explains relationship between  fill and expand parameters of the Box container.
 
-
 
-
<source lang="java">
 
-
package com.zetcode;
 
-
 
-
import org.gnome.gdk.Event;
 
-
import org.gnome.gtk.Button;
 
-
import org.gnome.gtk.Gtk;
 
-
import org.gnome.gtk.HBox;
 
-
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 explains the
 
-
* relationship between the fill
 
-
* and expand parameters of the Box
 
-
* container.
 
-
*
 
-
* @author jan bodnar
 
-
* website zetcode.com
 
-
* last modified March 2009
 
-
*/
 
-
 
-
 
-
public class GBoxes extends Window {
 
-
 
-
    public GBoxes() {
 
-
   
 
-
        setTitle("Boxes");
 
-
       
 
-
        initUI();
 
-
       
 
-
        connect(new Window.DeleteEvent() {
 
-
            public boolean onDeleteEvent(Widget source, Event event) {
 
-
                Gtk.mainQuit();
 
-
                return false;
 
-
            }
 
-
        });
 
-
   
 
-
        setDefaultSize(250, 150);
 
-
        setPosition(WindowPosition.CENTER);
 
-
        showAll();
 
-
    }
 
-
   
 
-
   
 
-
    public void initUI() {
 
-
       
 
-
        VBox vbox = new VBox(false, 5);
 
-
       
 
-
        HBox hbox1 = new HBox(false, 0);
 
-
        HBox hbox2 = new HBox(false, 0);
 
-
        HBox hbox3 = new HBox(false, 0);
 
-
       
 
-
        Button button1 = new Button("Button");
 
-
        Button button2 = new Button("Button");
 
-
        Button button3 = new Button("Button");
 
-
       
 
-
        hbox1.packStart(button1, false, false, 0);
 
-
        hbox2.packStart(button2, true, false, 0);
 
-
        hbox3.packStart(button3, true, true, 0);
 
-
       
 
-
        vbox.packStart(hbox1, false, false, 0);
 
-
        vbox.packStart(hbox2, false, false, 0);
 
-
        vbox.packStart(hbox3, false, false, 0);
 
-
       
 
-
        add(vbox);
 
-
       
 
-
        setBorderWidth(8);
 
-
    }
 
-
   
 
-
   
 
-
    public static void main(String[] args) {
 
-
        Gtk.init(args);
 
-
        new GBoxes();
 
-
        Gtk.main();
 
-
    }
 
-
}
 
-
</source>
 
-
 
-
In our code example, we have a vertical base box and additional three horizontal boxes. And we have three  buttons. We will see how combinatinos of expand and fill parameters will affect the layout.
 
-
 
-
We place three buttons inside the vertical box. The first parameter of the <b>packStart()</b> method is the widget, we put into the container. The second  parameter is the expand, the third fill and the last parameter is the padding.
 
-
 
-
<source lang="java">
 
-
hbox1.packStart(button1, false, false, 0);
 
-
</source>
 
-
 
-
In this case, both expand and fill are false.  The button does not grow and retains its initial position.
 
-
 
-
<source lang="java">
 
-
hbox2.packStart(button2, true, false, 0);
 
-
</source>
 
-
 
-
Here the expand parameter is set to true. Extra space is given to this button widget, but the widget does not grow or shrink. So in our case, the button will be centered horizontally and will retain its initial size.
 
-
 
-
<source lang="java">
 
-
vbox.packStart(hbox3, false, false, 0);
 
-
</source>
 
-
 
-
Finally, we set both parameters to true. This will cause the  button to take all the horizontal space allocated to it.
 
-
 
-
[[image: Java_Gnome_faq_boxes.png| center]]
 
-
 
-
== New Folder ==
 
-
 
-
The following code example will create a new folder dialog.
 
-
 
-
<source lang="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();
 
-
    }
 
-
}
 
-
</source>
 
-
 
-
This is the new folder window in Java Gnome.
 
-
 
-
<source lang="java">
 
-
VBox vbox = new VBox(false, 10);
 
-
 
-
</source>
 
-
 
-
A vertical box is the base container.
 
-
 
-
<source lang="java">
 
-
HBox hbox1 = new HBox(false, 5);
 
-
hbox1.packStart(label, false, false, 0);
 
-
hbox1.packStart(entry, true, true, 0);
 
-
 
-
vbox.packStart(hbox1, false, false, 0);
 
-
</source>
 
-
 
-
Label and entry widgets are placed inside a horizontal box. Label shoud retain it's default size, the entry widget is horizontally expandable.
 
-
 
-
<source lang="java">
 
-
TextView tw = new TextView();
 
-
vbox.packStart(tw);
 
-
 
-
</source>
 
-
 
-
The text view takes the bulk of the area. It is horizontally and vertically expandable.
 
-
 
-
<source lang="java">
 
-
hbox2.packStart(ok);
 
-
hbox2.packStart(close);
 
-
</source>
 
-
 
-
OK and close buttons go into the horizontal box.
 
-
 
-
<source lang="java">
 
-
Alignment halign = new Alignment(1, 0, 0, 0);
 
-
halign.add(hbox2);
 
-
       
 
-
vbox.packStart(halign, false, false, 0);
 
-
 
-
</source>
 
-
 
-
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.
 
-
 
-
[[image: Java_Gnome_faq_newfolder.png| center]]
 
-
 
-
== Windows ==
 
-
 
-
Next we will create a more advanced example. We show a window, that can be found in the JDeveloper IDE.
 
-
 
-
<source lang="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();
 
-
    }
 
-
}
 
-
</source>
 
-
 
-
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.
 
-
 
-
<source lang="java">
 
-
VBox vbox = new VBox(false, 9);
 
-
</source>
 
-
 
-
This is the main vertical box.
 
-
 
-
<source lang="java">
 
-
Button activate = new Button("Activate");
 
-
activate.setSizeRequest(80, 30);
 
-
 
-
Alignment align2 = new Alignment(0, 0, 0, 0);
 
-
align2.add(activate);
 
-
</source>
 
-
 
-
The activate button is resized to have 80x30 px. It is placed inside an <b>Alignment</b> widget,  so that it does not shrink or grow.
 
-
 
-
<source lang="java">
 
-
Button ok = new Button("OK");
 
-
ok.setSizeRequest(80, 30);
 
-
 
-
Alignment alignOk = new Alignment(1, 0, 0, 0);
 
-
alignOk.add(ok);
 
-
</source>
 
-
 
-
OK button is aligned to the right.
 
-
 
-
[[image: Java_Gnome_faq_windows.png| center]]
 
-
 
-
== Find/Replace window ==
 
-
 
-
In the following example, we will create a window, that you can find in the Eclipse IDE.
 
-
 
-
<source lang="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();
 
-
    }   
 
-
}
 
-
</source>
 
-
 
-
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.
 
-
 
-
<source lang="java">
 
-
HBox hbox1 = new HBox(false, 10);
 
-
hbox1.packStart(findLabel, false, false, 0);
 
-
hbox1.packStart(combo1);
 
-
</source>
 
-
 
-
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.
 
-
 
-
<source lang="java">
 
-
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);
 
-
 
-
</source>
 
-
 
-
We set a vertical box for the frame widget. Inside this box, we place the check buttons.
 
-
 
-
<source lang="java">
 
-
HBox framesBox = new HBox(true, 5);
 
-
framesBox.packStart(direction);
 
-
framesBox.packStart(scope);
 
-
 
-
vbox.packStart(framesBox, false, false, 0);
 
-
</source>
 
-
 
-
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.
 
-
 
-
<source lang="java">
 
-
Button close = new Button("Close");
 
-
close.setSizeRequest(80, -1);
 
-
Alignment halign = new Alignment(1, 0, 0, 0);
 
-
halign.add(close);
 
-
</source>
 
-
 
-
Finally, the last row. We use the <b>Alignment</b> 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.
 
-
 
-
[[image: Java_Gnome_faq_replace.png| center]]
 
-
 
-
In this part of the Java Gnome tutorial, we created some more sophisticated layouts.
 
-
 
-
[[Категория:GTK+]]
 
-
[[Категория:Java]]
 

Текущая версия на 11:21, 7 апреля 2009