简体   繁体   English

与BorderLayout对象的使用有关的一些疑问

[英]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 . 您将工具栏放置在名为exToolBar实例的NORTH位置。

Your ToolBar class extends JFrame . 您的ToolBar类扩展了JFrame The add method is inherited by ToolBar from JFrame . add方法由ToolBarJFrame继承。 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 ,要求addex

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 与Spring MVC模型对象有关的一些疑问 - Some doubts related to Spring MVC model object 一些疑问与Java中的接口使用有关(创建一个Hibernate DAO) - Some doubts related to the interface use in Java (creating an Hibernate DAO) 与Spring中的AOP配置有关的一些疑问 - Some doubts related to the AOP configuration in Spring 与在Java Swing程序中使用SwingUtilities.invokeLater()方法有关的一些疑问 - Some doubts related to the use of the SwingUtilities.invokeLater() method in Java Swing program 对多态的使用有疑问,以及多态与强制转换有什么关系? - Doubts about the use of polymorphism, and also about how is polymorphism related to casting? 怀疑与易失性,不可变对象有关,以及它们用于实现同步 - Doubts related to volatile , immutable objects, and their use to acheive synchronization 有关使用pathconvert Ant任务的一些疑问。 怎么办 - Some doubts about the use of pathconvert Ant task. What it do? 关于RowMapper在Spring Framework应用程序中使用JDBC的一些疑问 - Some doubts about RowMapper use in JDBC in a Spring Framework application 有关在Java Swing中使用适配器和侦听器的一些疑问 - Some doubts about the use of adapter and listener in Java Swing 关于在Spring Framework中使用** @Autowired **注释和接口声明的一些疑问 - Some doubts about the use of **@Autowired** annotation and interface declaration in Spring Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM