[英]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. 我正在研究Java Swing,并且对使用BorderLayout对象有疑问。
I have this simple example program that create a ToolBar: 我有一个创建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对象:
JToolBar toolbar = new JToolBar();
and then it put it in the NORTH position of the BorderLayout object by the line: 然后将其通过以下行放置在BorderLayout对象的NORTH位置:
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? 我的疑问是:它指向谁的BorderLayout
对象? At the external JFrame container? 在外部JFrame容器上?
It means that it puts the toolbar object in the NORTH position of my JFrame ? 这意味着它将工具栏对象放在JFrame的NORTH位置? Or what? 要不然是啥?
You put the toolbar in the NORTH position of your ToolBar
instance named ex
. 您将工具栏放置在名为ex
的ToolBar
实例的NORTH位置。
Your ToolBar
class extends JFrame
. 您的ToolBar
类扩展了JFrame
。 The add
method is inherited by ToolBar
from JFrame
. add
方法由ToolBar
从JFrame
继承。 In your main
you call ToolBar
constructor, which creates a new instance of ToolBar
and saves the reference to ex
. 在你的main
,你叫ToolBar
的构造函数,它创建的新实例, ToolBar
和保存参考ex
。 It also calls the initUI
method on ex
, which calls add
on ex
. 它还呼吁该initUI
的方法ex
,要求add
对ex
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.