私は 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 位置に配置することを意味します。または何?