Some doubts related to the use of the BorderLayout object
I am studying Java Swing and I have a doubt related to the use of the
BorderLayout object.
I have this simple example program that create a ToolBar:
package com.andrea.menu;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
public class ToolBar extends JFrame {
public ToolBar() {
initUI();
}
public final void initUI() {
JMenuBar menubar = new JMenuBar(); // The menu bar containing
the main menu voices
JMenu file = new JMenu("File"); // Creo un menu a tendina
con etichetta "File" e lo aggiungo
menubar.add(file);
setJMenuBar(menubar); // Sets the menubar for
this frame.
JToolBar toolbar = new JToolBar();
ImageIcon icon = new ImageIcon(getClass().getResource("exit.png"));
JButton exitButton = new JButton(icon);
toolbar.add(exitButton);
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
add(toolbar, BorderLayout.NORTH);
setTitle("Simple toolbar");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ToolBar ex = new ToolBar();
ex.setVisible(true);
}
});
}
}
So it create a JToolBar object by the line:
JToolBar toolbar = new JToolBar();
and then it put it in the NORTH position of the BorderLayout object by the
line:
add(toolbar, BorderLayout.NORTH);
Reading the documentation I know that:
A border layout lays out a container, arranging and resizing its
components to fit in five regions: north, south, east, west, and center
My doubt is: the BorderLayout object whom it refers? At the external
JFrame container?
It means that it put the toolbar object in the NORTH position of my
JFrame? Or what?
Tnx
Andrea
No comments:
Post a Comment