よく使いそうなレイアウト:BoxLayout
javaのレイアウトについてのメモ
BoxLayoutを使えば、解決できた。
コツは、1枚目のパネル(ボタンがついてるやつ)はFlowLayout、2枚目のパネル(2枚のTextArea)にBoxLayoutのを使うこと。
BoxLayout.Y_AXISで、縦に並ぶ。
それぞれのパネルは、FrameにBorderLayoutのNORTHとCENTERでaddしてOK。
import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; public class BoxFrame extends JFrame { private JPanel p1_ = new JPanel(); private JPanel p2_ = new JPanel(); private JButton save_btn_ = new JButton(" 保存 "); private JButton clear_btn_ = new JButton("クリア"); private JTextArea tarea1_ = new JTextArea(); private JTextArea tarea2_ = new JTextArea(); private JScrollPane scrollPane1_ = new JScrollPane(tarea1_); private JScrollPane scrollPane2_ = new JScrollPane(tarea2_); public BoxFrame() { super(); this.setTitle("BoxLayoutの使い方"); this.setDefaultCloseOperation(EXIT_ON_CLOSE); //×押したら終了 this.setBounds(10, 10, 600, 600); save_btn_.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("save"); } }); clear_btn_.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { tarea1_.setText(""); tarea2_.setText(""); } }); p1_.setLayout(new FlowLayout()); p1_.add(save_btn_); p1_.add(clear_btn_); p2_.setLayout(new BoxLayout(p2_, BoxLayout.Y_AXIS)); p2_.add(scrollPane1_); p2_.add(scrollPane2_); this.add(p1_, BorderLayout.NORTH); this.add(p2_, BorderLayout.CENTER); } }
あとは、mainファイルでこのFrameを生成する。
public class BoxMain { public static void main(String[] args) { BoxFrame frame = new BoxFrame(); frame.setVisible(true); } }
ちなみに、セパレーター(マウスでTextAreaの大きさを変更できるやつ)を入れると、ソースは大幅に変わってしまう。
BoxLayoutを使わなくて良くなる。
javax.swing.JSplitPaneを使う。
import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; //import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JSplitPane; public class SplitFrame extends JFrame { private JPanel p1_ = new JPanel(); private JPanel p2_ = new JPanel(); private JButton save_btn_ = new JButton(" 保存 "); private JButton clear_btn_ = new JButton("クリア"); private JTextArea tarea1_ = new JTextArea(); private JTextArea tarea2_ = new JTextArea(); private JScrollPane scrollPane1_ = new JScrollPane(tarea1_); private JScrollPane scrollPane2_ = new JScrollPane(tarea2_); private JSplitPane splitpane = new JSplitPane(); public SplitFrame() { super(); this.setTitle("Split"); this.setDefaultCloseOperation(EXIT_ON_CLOSE); //×押したら終了 this.setBounds(10, 10, 600, 600); save_btn_.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("save"); } }); clear_btn_.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { tarea1_.setText(""); tarea2_.setText(""); } }); p1_.setLayout(new FlowLayout()); p1_.add(save_btn_); p1_.add(clear_btn_); splitpane.setOrientation(JSplitPane.VERTICAL_SPLIT); splitpane.setTopComponent(scrollPane1_); splitpane.setBottomComponent(scrollPane2_); splitpane.setDividerLocation(350); splitpane.setDividerSize(10); this.add(p1_, BorderLayout.NORTH); this.add(splitpane, BorderLayout.CENTER); } }