Java Swing Управление позиционированием

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

(Различия между версиями)
Перейти к: навигация, поиск
(нарушало авторские права)
 
Строка 1: Строка 1:
-
The Java Swing toolkit has two kind of components. Containers and children. The containers group children into
 
-
suitable layouts.
 
-
To create layouts, we use <b>layout managers</b>. Layout managers are one of the most difficult parts of modern
 
-
GUI programming. Many beginning programmers have too much respect for layout managers. Mainly because they are
 
-
usually poorly documented. I believe, that GUI builder's like Matisse cannot replace the proper understanding of
 
-
layout managers.
 
-
== No manager ==
 
-
We can use no layout manager, if we want. There might be situations, where we might not need a layout manager.
 
-
For example, in my code examples, I often go without a manager. It is because I did not want to make the examples too
 
-
complex. But to create truly portable, complex applications, we need layout managers.
 
-
 
-
Without layout manager, we position components using absolute values.
 
-
 
-
<source lang="java">
 
-
import javax.swing.JButton;
 
-
import javax.swing.JFrame;
 
-
 
-
 
-
public class Absolute extends JFrame {
 
-
 
-
 
-
    public Absolute() {
 
-
 
-
        setTitle("Absolute positioning");
 
-
 
-
        setLayout(null);
 
-
 
-
        JButton ok = new JButton("OK");
 
-
        ok.setBounds(50, 150, 80, 25);
 
-
 
-
        JButton close = new JButton("Close");
 
-
        close.setBounds(150, 150, 80, 25);
 
-
 
-
        add(ok);
 
-
        add(close);
 
-
 
-
        setSize(300, 250);
 
-
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
-
        setLocationRelativeTo(null);
 
-
        setVisible(true);
 
-
    }
 
-
 
-
    public static void main(String[] args) {
 
-
        new Absolute();
 
-
    }
 
-
}
 
-
</source>
 
-
 
-
This simple example shows two buttons.
 
-
 
-
<source lang="java">
 
-
setLayout(null);
 
-
</source>
 
-
 
-
We use absolute positioning by providing null to the <b>setLayout()</b> method.
 
-
 
-
<source lang="java">
 
-
ok.setBounds(50, 150, 80, 25);
 
-
</source>
 
-
 
-
The <b>setBounds()</b> method positions the ok button. The parameters are the x, y location values and
 
-
the width and height of the component.
 
-
 
-
 
-
== FlowLayout manager ==
 
-
 
-
This is the simplestlayout manager in the Java Swing toolkit. It is mainly used in combination
 
-
with other layout managers. When calculating it's children size, a flow layout lets each component assume its natural (preferred) size.
 
-
 
-
 
-
The manager puts components into a row. In the order, they were added. If they do not fit into one row, they
 
-
go into the next one. The components can be added from the right to the left or vice versa. The manager allows to align
 
-
the components. Implicitly, the components are centered and there is 5px space among components and components and the
 
-
edges of the container.
 
-
 
-
<source lang="java">
 
-
FlowLayout()
 
-
FlowLayout(int align)
 
-
FlowLayout(int align, int hgap, int vgap)
 
-
 
-
</source>
 
-
 
-
There are three constructors available for the FlowLayout manager. The first one creates a manager with implicit values.
 
-
Centered and 5px spaces. The others allow to specify those parametes.
 
-
 
-
<source lang="java">
 
-
import java.awt.Dimension;
 
-
 
-
import javax.swing.JButton;
 
-
import javax.swing.JFrame;
 
-
import javax.swing.JPanel;
 
-
import javax.swing.JTextArea;
 
-
import javax.swing.JTree;
 
-
 
-
 
-
public class FlowLayoutExample extends JFrame {
 
-
 
-
 
-
    public FlowLayoutExample() {
 
-
 
-
        setTitle("FlowLayout Example");
 
-
 
-
        JPanel panel = new JPanel();
 
-
 
-
        JTextArea area = new JTextArea("text area");
 
-
        area.setPreferredSize(new Dimension(100, 100));
 
-
 
-
        JButton button = new JButton("button");
 
-
        panel.add(button);
 
-
 
-
        JTree tree = new JTree();
 
-
        panel.add(tree);
 
-
 
-
        panel.add(area);
 
-
 
-
        add(panel);
 
-
 
-
        pack();
 
-
 
-
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
-
        setLocationRelativeTo(null);
 
-
        setVisible(true);
 
-
    }
 
-
 
-
    public static void main(String[] args) {
 
-
        new FlowLayoutExample();
 
-
    }
 
-
}
 
-
</source>
 
-
 
-
 
-
The example shows a button, a tree and a text area component in the window. Interesingly, if we create an empty
 
-
tree component, there are some default values inside the component.
 
-
 
-
 
-
<source lang="java">
 
-
 
-
JPanel panel = new JPanel();
 
-
</source>
 
-
 
-
The implicit layout manager of the <b>JPanel</b> component is a flow layout manager. We do not have to set it manually.
 
-
 
-
<source lang="java">
 
-
JTextArea area = new JTextArea("text area");
 
-
area.setPreferredSize(new Dimension(100, 100));
 
-
</source>
 
-
 
-
 
-
The flow layout manager sets a <b>preferred</b> size for it's components. This means, that in our case, the area component will have 100x100 px. If we didn't set the preferred size, the component would have a size of it's text. No more, no less.
 
-
Without the text, the component would not be visible at all. Try to write or delete some text in the area component. The
 
-
component will grow and shrink accordingly.
 
-
 
-
 
-
<source lang="java">
 
-
panel.add(area);
 
-
</source>
 
-
 
-
To put a component inside a container, we simply call the <b>add()</b> method.
 
-
 
-
[[image: java_swing_flowlayout.png | center]]
 
-
 
-
== GridLayout ==
 
-
 
-
The <b>GridLayout</b> layout manager lays out components in a rectangular grid. The container is divided into equally sized rectangles. One component is placed in each rectangle.
 
-
 
-
 
-
<source lang="java">
 
-
import java.awt.GridLayout;
 
-
 
-
import javax.swing.BorderFactory;
 
-
import javax.swing.JButton;
 
-
import javax.swing.JFrame;
 
-
import javax.swing.JLabel;
 
-
import javax.swing.JPanel;
 
-
 
-
 
-
public class GridLayoutExample extends JFrame {
 
-
 
-
    public GridLayoutExample() {
 
-
 
-
        setTitle("GridLayout");
 
-
 
-
        JPanel panel = new JPanel();
 
-
 
-
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
 
-
        panel.setLayout(new GridLayout(5, 4, 5, 5));
 
-
 
-
        String[] buttons = {
 
-
            "Cls", "Bck", "", "Close", "7", "8", "9", "/", "4",
 
-
            "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+"
 
-
        };
 
-
 
-
 
-
        for (int i = 0; i < buttons.length; i++) {
 
-
 
-
            if (i == 2)
 
-
                panel.add(new JLabel(buttons[i]));
 
-
            else
 
-
                panel.add(new JButton(buttons[i]));
 
-
        }
 
-
 
-
        add(panel);
 
-
 
-
        setSize(350, 300);
 
-
 
-
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
-
        setLocationRelativeTo(null);
 
-
        setVisible(true);
 
-
 
-
    }
 
-
 
-
    public static void main(String[] args) {
 
-
        new GridLayoutExample();
 
-
    }
 
-
}
 
-
</source>
 
-
 
-
The example shows a skeleton of a simple calculator tool. It is an ideal example for this layout manager.
 
-
We put 19 buttons and one label into the manager. Notice, that each button is of the same size.
 
-
 
-
<source lang="java">
 
-
panel.setLayout(new GridLayout(5, 4, 5, 5));
 
-
</source>
 
-
 
-
 
-
Here we set the grid layout manager for the panel component. The layout manager takes four parameters. The number of rows,
 
-
the number of columns and the horizontal and vertical gaps between components.
 
-
 
-
 
-
[[image: java_swing_gridlayout.png | center]]
 
-
 
-
 
-
 
-
== BorderLayout ==
 
-
 
-
A <b>BorderLayout</b> manager is a very handy layout manager. I haven't seen it elsewhere.
 
-
It divides the space into five regions. North, West, South, East and Centre. Each region can have only one component.
 
-
If we need to put more components into a region, we can simply put a panel there with a manager of our choice.
 
-
The components in N, W, S, E regions get their <b>preferred</b> size. The component in the centre takes up the whole space left.
 
-
 
-
 
-
 
-
It does not look good, if child components are too close to each other. We must put some space among them.
 
-
Each component in Swing toolkit can have borders around it's edges. To create a border, we either create a
 
-
new instance of an <b>EmptyBorder</b> class or we use a <b>BorderFactory</b>.
 
-
Except for EmptyBorder, there are other types of borders as well. But for layout management we will use only
 
-
this one.
 
-
 
-
 
-
<source lang="java">
 
-
import java.awt.BorderLayout;
 
-
import java.awt.Color;
 
-
import java.awt.Dimension;
 
-
import java.awt.Insets;
 
-
 
-
import javax.swing.JFrame;
 
-
import javax.swing.JPanel;
 
-
import javax.swing.border.EmptyBorder;
 
-
 
-
 
-
public class BorderExample extends JFrame {
 
-
 
-
 
-
    public BorderExample() {
 
-
 
-
        setTitle("Border Example");
 
-
 
-
        JPanel panel = new JPanel(new BorderLayout());
 
-
        JPanel top = new JPanel();
 
-
 
-
        top.setBackground(Color.gray);
 
-
        top.setPreferredSize(new Dimension(250, 150));
 
-
        panel.add(top);
 
-
 
-
        panel.setBorder(new EmptyBorder(new Insets(20, 20, 20, 20)));
 
-
 
-
        add(panel);
 
-
 
-
        pack();
 
-
 
-
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
-
        setLocationRelativeTo(null);
 
-
        setVisible(true);
 
-
    }
 
-
 
-
    public static void main(String[] args) {
 
-
        new BorderExample();
 
-
    }
 
-
}
 
-
</source>
 
-
 
-
The example will display a gray panel and border around it.
 
-
 
-
<source lang="java">
 
-
JPanel panel = new JPanel(new BorderLayout());
 
-
JPanel top = new JPanel();
 
-
</source>
 
-
 
-
We place a panel into a panel. We used a <b>BorderLayout</b> manager for the first panel,
 
-
because this manager will resize it's children.
 
-
 
-
<source lang="java">
 
-
 
-
panel.add(top);
 
-
</source>
 
-
 
-
Here we placed a top panel into the panel component. More precisely, we placed into the center area of the <b>BorderLayout</b> manager.
 
-
 
-
<source lang="java">
 
-
panel.setBorder(new EmptyBorder(new Insets(20, 20, 20, 20)));
 
-
</source>
 
-
 
-
 
-
Here we created a 20px border around the panel. The border values are as follows: top, left, bottom and right. They go counterclockwise.
 
-
 
-
 
-
[[image: java_swing_borderexample.png | center]]
 
-
 
-
 
-
 
-
The next example will show a typical usage of a border layout manager.
 
-
 
-
 
-
<source lang="java">
 
-
import java.awt.BorderLayout;
 
-
import java.awt.Dimension;
 
-
import java.awt.Insets;
 
-
 
-
import javax.swing.ImageIcon;
 
-
import javax.swing.JButton;
 
-
import javax.swing.JFrame;
 
-
import javax.swing.JLabel;
 
-
import javax.swing.JMenu;
 
-
import javax.swing.JMenuBar;
 
-
import javax.swing.JTextArea;
 
-
import javax.swing.JToolBar;
 
-
import javax.swing.border.EmptyBorder;
 
-
import javax.swing.border.LineBorder;
 
-
 
-
 
-
public class BorderLayoutExample extends JFrame {
 
-
 
-
 
-
    public BorderLayoutExample() {
 
-
 
-
        setTitle("BorderLayout");
 
-
 
-
        JMenuBar menubar = new JMenuBar();
 
-
        JMenu file = new JMenu("File");
 
-
 
-
        menubar.add(file);
 
-
        setJMenuBar(menubar);
 
-
 
-
        JToolBar toolbar = new JToolBar();
 
-
        toolbar.setFloatable(false);
 
-
 
-
        ImageIcon exit = new ImageIcon("exit.png");
 
-
        JButton bexit = new JButton(exit);
 
-
        bexit.setBorder(new EmptyBorder(0 ,0, 0, 0));
 
-
        toolbar.add(bexit);
 
-
 
-
        add(toolbar, BorderLayout.NORTH);
 
-
 
-
        JToolBar vertical = new JToolBar(JToolBar.VERTICAL);
 
-
        vertical.setFloatable(false);
 
-
        vertical.setMargin(new Insets(10, 5, 5, 5));
 
-
 
-
        ImageIcon select = new ImageIcon("drive.png");
 
-
        ImageIcon freehand = new ImageIcon("computer.png");
 
-
        ImageIcon shapeed = new ImageIcon("printer.png");
 
-
 
-
        JButton selectb = new JButton(select);
 
-
        selectb.setBorder(new EmptyBorder(3, 0, 3, 0));
 
-
 
-
        JButton freehandb = new JButton(freehand);
 
-
        freehandb.setBorder(new EmptyBorder(3, 0, 3, 0));
 
-
        JButton shapeedb = new JButton(shapeed);
 
-
        shapeedb.setBorder(new EmptyBorder(3, 0, 3, 0));
 
-
 
-
        vertical.add(selectb);
 
-
        vertical.add(freehandb);
 
-
        vertical.add(shapeedb);
 
-
 
-
        add(vertical, BorderLayout.WEST);
 
-
 
-
        add(new JTextArea(), BorderLayout.CENTER);
 
-
 
-
        JLabel statusbar = new JLabel(" Statusbar");
 
-
        statusbar.setPreferredSize(new Dimension(-1, 22));
 
-
        statusbar.setBorder(LineBorder.createGrayLineBorder());
 
-
        add(statusbar, BorderLayout.SOUTH);
 
-
 
-
        setSize(350, 300);
 
-
 
-
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
-
        setLocationRelativeTo(null);
 
-
        setVisible(true);
 
-
    }
 
-
 
-
    public static void main(String[] args) {
 
-
        new BorderLayoutExample();
 
-
    }
 
-
}
 
-
</source>
 
-
 
-
The example shows a typical application skeleton. We show a vertical and horizontal toolbars, a statusbar and a central
 
-
component. (a text area)
 
-
 
-
A default layout manager for a <b>JFrame</b> component is <b>BorderLayout</b> manager. So we don't have to set it.
 
-
 
-
 
-
<source lang="java">
 
-
 
-
add(toolbar, BorderLayout.NORTH);
 
-
</source>
 
-
 
-
Simply put the toolbar to the north of the layout.
 
-
 
-
<source lang="java">
 
-
add(vertical, BorderLayout.WEST);
 
-
</source>
 
-
 
-
Put the vertical toobar to the west.
 
-
 
-
<source lang="java">
 
-
add(new JTextArea(), BorderLayout.CENTER);
 
-
</source>
 
-
 
-
Put the text area to the center.
 
-
 
-
<source lang="java">
 
-
add(statusbar, BorderLayout.SOUTH);
 
-
</source>
 
-
 
-
Put the statusbar to the south.
 
-
 
-
 
-
That's it.
 
-
 
-
[[image: java_swing_borderlayout.png | center]]
 
-
 
-
 
-
 
-
== BoxLayout ==
 
-
 
-
BoxLayout is a powerful manager, that can be used to create sofisticated layouts. This layout manager puts components
 
-
into a row or into a column. It enables <b>nesting</b>, a powerful feature, that makes this manager very flexible.
 
-
It means, that we can put a box layout into another box layout.
 
-
 
-
 
-
The box layout manager is often used with the <b>Box</b> class. This class creates several invisible
 
-
components, which affect the final layout.
 
-
 
-
<ul style="list-style-image: url(../images/square.png);">
 
-
<li>glue</li>
 
-
<li>strut</li>
 
-
<li>rigid area</li>
 
-
</ul>
 
-
 
-
 
-
 
-
 
-
Let's say, we want to put two buttons into the right bottom corner of the window. We will use the boxlayut managers to
 
-
accomplish this.
 
-
 
-
<source lang="java">
 
-
import java.awt.Dimension;
 
-
 
-
import javax.swing.Box;
 
-
import javax.swing.BoxLayout;
 
-
import javax.swing.JButton;
 
-
import javax.swing.JFrame;
 
-
import javax.swing.JPanel;
 
-
 
-
 
-
public class TwoButtons extends JFrame {
 
-
 
-
    public TwoButtons() {
 
-
 
-
        setTitle("Two Buttons");
 
-
 
-
        JPanel basic = new JPanel();
 
-
        basic.setLayout(new BoxLayout(basic, BoxLayout.Y_AXIS));
 
-
        add(basic);
 
-
 
-
        basic.add(Box.createVerticalGlue());
 
-
 
-
        JPanel bottom = new JPanel();
 
-
        bottom.setAlignmentX(1f);
 
-
        bottom.setLayout(new BoxLayout(bottom, BoxLayout.X_AXIS));
 
-
 
-
        JButton ok = new JButton("OK");
 
-
        JButton close = new JButton("Close");
 
-
 
-
        bottom.add(ok);
 
-
        bottom.add(Box.createRigidArea(new Dimension(5, 0)));
 
-
        bottom.add(close);
 
-
        bottom.add(Box.createRigidArea(new Dimension(15, 0)));
 
-
 
-
        basic.add(bottom);
 
-
        basic.add(Box.createRigidArea(new Dimension(0, 15)));
 
-
 
-
        setSize(300, 250);
 
-
 
-
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
-
        setLocationRelativeTo(null);
 
-
        setVisible(true);
 
-
 
-
    }
 
-
 
-
    public static void main(String[] args) {
 
-
        new TwoButtons();
 
-
    }
 
-
}
 
-
</source>
 
-
 
-
The following drawing illustrates the example.
 
-
 
-
 
-
[[image: java_swing_twobuttons.png | center]]
 
-
 
-
 
-
We will create two panels. The basic panel has a vertical box layout. The bottom panel has a horizontal one. We will put a bottom panel into the basic panel. We will right align the bottom panel. The space between the top of the window and the bottom panel is expandable. It is done by the vertical glue.
 
-
 
-
 
-
<source lang="java">
 
-
basic.setLayout(new BoxLayout(basic, BoxLayout.Y_AXIS));
 
-
</source>
 
-
 
-
 
-
Here we create a basic panel with the vertical <b>BoxLayout</b>.
 
-
 
-
 
-
<source lang="java">
 
-
JPanel bottom = new JPanel();
 
-
bottom.setAlignmentX(1f);
 
-
bottom.setLayout(new BoxLayout(bottom, BoxLayout.X_AXIS));
 
-
</source>
 
-
 
-
The bottom panel is right aligned. This is done by the <b>setAlignmentX()</b> method. The panel has
 
-
a horizontal layout.
 
-
 
-
<source lang="java">
 
-
bottom.add(Box.createRigidArea(new Dimension(5, 0)));
 
-
</source>
 
-
 
-
We put some rigid space between the buttons.
 
-
 
-
<source lang="java">
 
-
basic.add(bottom);
 
-
</source>
 
-
 
-
Here we put the bottom panel with a horizontal box layout to the vertical basic panel.
 
-
 
-
 
-
 
-
<source lang="java">
 
-
basic.add(Box.createRigidArea(new Dimension(0, 15)));
 
-
</source>
 
-
 
-
We also put some space between the bottom panel and the border of the window.
 
-
 
-
 
-
[[image: java_swing_twobuttons2.png | center]]
 
-
 
-
 
-
When we use a <b>BoxLayout</b> manager, we can set a rigid area among our components.
 
-
 
-
<source lang="java">
 
-
import java.awt.Dimension;
 
-
 
-
import java.awt.Insets;
 
-
 
-
import javax.swing.Box;
 
-
import javax.swing.BoxLayout;
 
-
import javax.swing.JButton;
 
-
import javax.swing.JFrame;
 
-
import javax.swing.JPanel;
 
-
import javax.swing.border.EmptyBorder;
 
-
 
-
 
-
public class RigidArea extends JFrame {
 
-
 
-
    public RigidArea() {
 
-
 
-
        setTitle("RigidArea");
 
-
 
-
        JPanel panel = new JPanel();
 
-
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
 
-
 
-
        panel.setBorder(new EmptyBorder(new Insets(40, 60, 40, 60)));
 
-
 
-
        panel.add(new JButton("Button"));
 
-
        panel.add(Box.createRigidArea(new Dimension(0, 5)));
 
-
        panel.add(new JButton("Button"));
 
-
        panel.add(Box.createRigidArea(new Dimension(0, 5)));
 
-
        panel.add(new JButton("Button"));
 
-
        panel.add(Box.createRigidArea(new Dimension(0, 5)));
 
-
        panel.add(new JButton("Button"));
 
-
 
-
        add(panel);
 
-
 
-
        pack();
 
-
 
-
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
-
        setLocationRelativeTo(null);
 
-
        setVisible(true);
 
-
    }
 
-
 
-
    public static void main(String[] args) {
 
-
        new RigidArea();
 
-
    }
 
-
}
 
-
</source>
 
-
 
-
In this example, we display four buttons. By default, there is no space among the buttons. To put some space among them,
 
-
we add some rigid area.
 
-
 
-
 
-
<source lang="java">
 
-
 
-
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
 
-
</source>
 
-
 
-
We use a vertical <b>BoxLayout</b> manager for our panel.
 
-
 
-
<source lang="java">
 
-
panel.add(new JButton("Button"));
 
-
panel.add(Box.createRigidArea(new Dimension(0, 5)));
 
-
panel.add(new JButton("Button"));
 
-
</source>
 
-
 
-
 
-
We add buttons and create a rigid area in between them.
 
-
 
-
 
-
[[image: java_swing_rigidarea.png | center]]
 
-
 
-
 
-
 
-
 
-
== Tip of the Day ==
 
-
 
-
JDeveloper has a dialog window called Tip of the Day.
 
-
 
-
 
-
[[image: java_swing_tipofday1.png | center]]
 
-
 
-
 
-
We will create a similar dialog. We will use a combination of various layout managers. Namely a border layout, flow layout
 
-
and box layout manager.
 
-
 
-
<source lang="java">
 
-
import java.awt.BorderLayout;
 
-
import java.awt.Color;
 
-
import java.awt.Dimension;
 
-
import java.awt.FlowLayout;
 
-
import java.awt.event.KeyEvent;
 
-
 
-
import javax.swing.BorderFactory;
 
-
import javax.swing.BoxLayout;
 
-
import javax.swing.ImageIcon;
 
-
import javax.swing.JButton;
 
-
import javax.swing.JCheckBox;
 
-
import javax.swing.JDialog;
 
-
import javax.swing.JLabel;
 
-
import javax.swing.JPanel;
 
-
import javax.swing.JSeparator;
 
-
import javax.swing.JTextPane;
 
-
 
-
 
-
public class TipOfDay extends JDialog {
 
-
 
-
 
-
    public TipOfDay() {
 
-
 
-
        setTitle("Tip of the Day");
 
-
 
-
        JPanel basic = new JPanel();
 
-
        basic.setLayout(new BoxLayout(basic, BoxLayout.Y_AXIS));
 
-
        add(basic);
 
-
 
-
        JPanel topPanel = new JPanel(new BorderLayout(0, 0));
 
-
        topPanel.setMaximumSize(new Dimension(450, 0));
 
-
        JLabel hint = new JLabel("JDeveloper Productivity Hints");
 
-
        hint.setBorder(BorderFactory.createEmptyBorder(0, 25, 0, 0));
 
-
        topPanel.add(hint);
 
-
 
-
        ImageIcon icon = new ImageIcon("jdev.png");
 
-
        JLabel label = new JLabel(icon);
 
-
        label.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
 
-
        topPanel.add(label, BorderLayout.EAST);
 
-
 
-
        JSeparator separator = new JSeparator();
 
-
        separator.setForeground(Color.gray);
 
-
 
-
        topPanel.add(separator, BorderLayout.SOUTH);
 
-
 
-
        basic.add(topPanel);
 
-
 
-
        JPanel textPanel = new JPanel(new BorderLayout());
 
-
        textPanel.setBorder(BorderFactory.createEmptyBorder(15, 25, 15, 25));
 
-
        JTextPane pane = new JTextPane();
 
-
 
-
        pane.setContentType("text/html");
 
-
        String text = "&lt;p&gt;&lt;b&gt;Closing windows using the mouse wheel&lt;/b&gt;&lt;/p&gt;" +
 
-
            "&lt;p&gt;Clicking with the mouse wheel on an editor tab closes the window. " +
 
-
            "This method works also with dockable windows or Log window tabs.&lt;/p&gt;";
 
-
        pane.setText(text);
 
-
        pane.setEditable(false);
 
-
        textPanel.add(pane);
 
-
 
-
        basic.add(textPanel);
 
-
 
-
        JPanel boxPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 20, 0));
 
-
 
-
        JCheckBox box = new JCheckBox("Show Tips at startup");
 
-
        box.setMnemonic(KeyEvent.VK_S);
 
-
 
-
        boxPanel.add(box);
 
-
        basic.add(boxPanel);
 
-
 
-
        JPanel bottom = new JPanel(new FlowLayout(FlowLayout.RIGHT));
 
-
 
-
        JButton ntip = new JButton("Next Tip");
 
-
        ntip.setMnemonic(KeyEvent.VK_N);
 
-
        JButton close = new JButton("Close");
 
-
        close.setMnemonic(KeyEvent.VK_C);
 
-
 
-
        bottom.add(ntip);
 
-
        bottom.add(close);
 
-
        basic.add(bottom);
 
-
 
-
        bottom.setMaximumSize(new Dimension(450, 0));
 
-
 
-
        setSize(new Dimension(450, 350));
 
-
        setResizable(false);
 
-
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
 
-
        setLocationRelativeTo(null);
 
-
        setVisible(true);
 
-
    }
 
-
 
-
 
-
    public static void main(String[] args) {
 
-
        new TipOfDay();
 
-
    }
 
-
}
 
-
 
-
</source>
 
-
 
-
The example uses a mix of layout managers. Simply we put four panels into the vertically organized basic panel.
 
-
 
-
<source lang="java">
 
-
JPanel basic = new JPanel();
 
-
basic.setLayout(new BoxLayout(basic, BoxLayout.Y_AXIS));
 
-
add(basic);
 
-
</source>
 
-
 
-
This is the very bottom panel. It has a vertical box layout manager. The basic panel is added to the default
 
-
<b>JDialog</b> component. This component has a border layout manager by default.
 
-
 
-
 
-
<source lang="java">
 
-
JPanel topPanel = new JPanel(new BorderLayout(0, 0));
 
-
</source>
 
-
 
-
The topPanel panel has a border layout manager. We will put three components into it. Two labels and a separator.
 
-
 
-
<source lang="java">
 
-
topPanel.setMaximumSize(new Dimension(450, 0));
 
-
</source>
 
-
 
-
 
-
If we want to have a panel, that is not greater than it's components, we must set it's maximum size.
 
-
The zero value is ignored. The manager calculates the necessary heights.
 
-
 
-
<source lang="java">
 
-
JPanel textPanel = new JPanel(new BorderLayout());
 
-
...
 
-
textPanel.add(pane);
 
-
</source>
 
-
 
-
The text pane component is added to the center area of the border layout manager. It takes all space left.
 
-
Exactly, as we wanted.
 
-
 
-
<source lang="java">
 
-
JPanel boxPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 20, 0));
 
-
</source>
 
-
 
-
The check box is shown in the boxPanel panel. It is left aligned. The flow layout manager has a 20px horizontal gap.
 
-
Other components have 25px. Why is that? It is because the flow layout manager puts some space to between the component and
 
-
the edge as well.
 
-
 
-
<source lang="java">
 
-
JPanel bottom = new JPanel(new FlowLayout(FlowLayout.RIGHT));
 
-
...
 
-
bottom.setMaximumSize(new Dimension(450, 0));
 
-
</source>
 
-
 
-
The bottom panel displays two buttons. It has a right aligned flow layout manager. In order to show the buttons
 
-
on the right edge of the dialog, the panel must stretch horizontally from the beginning to the end.
 
-
 
-
 
-
 
-
[[image: java_swing_tipofday2.png | center]]
 
-
 
-
[[Категория:Java]]
 

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