異なるサイズのパネルにコンポーネントを配置しようとしています。しかし、私はGridLayoutが同じサイズの部品を分割することに気付きました。画像異なるサイズのスイングパネル
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class PanelDemo {
PanelDemo() {
// Create a new JFrame container. Use the default
// border layout.
JFrame jfrm = new JFrame("Use Three JPanels with Different Sizes");
// Specify FlowLayout manager.
jfrm.getContentPane().setLayout(new GridLayout(1, 3));
// Give the frame an initial size.
jfrm.setSize(900, 300);
// Terminate the program when the user closes the application.
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create the first JPanel.
JPanel jpnl = new JPanel();
jpnl.setLayout(new GridLayout(2, 4));
// Set the preferred size of the first panel.
jpnl.setPreferredSize(new Dimension(500, 300));
// Make the panel opaque.
jpnl.setOpaque(true);
// Add a blue border to the panel.
jpnl.setBorder(
BorderFactory.createLineBorder(Color.BLUE));
// Create the second JPanel.
JPanel jpnl2 = new JPanel();
//jpnl2.setLayout(new FlowLayout());
// Set the preferred size of the second panel.
jpnl2.setPreferredSize(new Dimension(300, 300));
// Make the panel opaque.
jpnl2.setOpaque(true);
jpnl2.setBorder(
BorderFactory.createLineBorder(Color.RED));
JPanel jpnl3 = new JPanel();
jpnl3.setOpaque(true);
jpnl3.setPreferredSize(new Dimension(100, 300));
jpnl3.setBorder(
BorderFactory.createLineBorder(Color.ORANGE));
// Add the panels to the frame.
jfrm.getContentPane().add(jpnl);
jfrm.getContentPane().add(jpnl3);
jfrm.getContentPane().add(jpnl2);
// Display the frame.
jfrm.setVisible(true);
}
public static void main(String args[]) {
// Create the frame on the event dispatching thread.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new PanelDemo();
}
});
}
}
'BorderLayout'がCENTER'&' LINE_END'制約はそれを行うことができ、 '、' LINE_START'を使用。しかし、GUIがサイズ変更可能な場合、センターパネルは余分なスペースを取得します.. –
BTW - 各パネルには何がありますか? 'components'の場合は、明示的に設定するのではなく、パネルに使用されているレイアウトに推奨サイズに関するヒントを与えることをお勧めします。 'jfrm.setSize(900、300);を呼び出すのではなく、 'jfrm.pack();'(すべてのコンポーネントが追加された後).. –
BoxLayout? GridBagLayout? [Layout Manager](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html)のSwingチュートリアルを読んでください。各レイアウトマネージャは、フレームサイズが変更されると、サイズ変更のための異なるルールを持ちます。使用するレイアウトは、正確な要件に依存します。各レイアウトマネージャーと一緒に遊んで、あなたの要件に最も適したものを決定してください。 – camickr