0

私は Java Swing を勉強していますが、BorderLayoutオブジェクトの使用に関して疑問があります。

ツールバーを作成するこの簡単なサンプル プログラムがあります。

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);
            }
        });
    }
}

したがって、次の行でJToolBarオブジェクトを作成します。

JToolBar toolbar = new JToolBar();

次に、それをBorderLayoutオブジェクトのNORTH位置に次の行で配置します。

add(toolbar, BorderLayout.NORTH);

私が知っているドキュメントを読む:

ボーダー レイアウトはコンテナーをレイアウトし、そのコンポーネントを配置してサイズを変更し、北、南、東、西、中央の 5 つの領域に収まるようにします。

私の疑問は次のとおりBorderLayoutです。それが参照するオブジェクトは? 外部JFrameコンテナで?

これは、ツールバーオブジェクトをJFrameの NORTH 位置に配置することを意味します。または何?

4

1 に答える 1

1

You put the toolbar in the NORTH position of your ToolBar instance named ex.

Your ToolBar class extends JFrame. The add method is inherited by ToolBar from JFrame. In your main you call ToolBar constructor, which creates a new instance of ToolBar and saves the reference to ex. It also calls the initUI method on ex, which calls add on ex.

于 2013-09-27T14:37:37.400 に答える