0

私はJFrameを持っていBorderLayoutます. JPanelの北側にを追加しましたJFrame。このパネルでは、コンポーネントを絶対配置で追加します。JFrame の中央側にJPanel、巨大なスペースを必要とする別のフレームを追加しました。JPanelただし、アプリケーションを実行すると、中心が!JPanelのすべてのスペースを占めていたため、北からは何も見えません。JFrame北 JPanel に垂直方向のスペースを与えるにはどうすればよいですか?

北の JPanel には絶対位置を使用する必要があります。

これが私のコードです:

public class AAAA extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    AAAA frame = new AAAA();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public AAAA() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 1136, 520);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.NORTH);
        panel.setLayout(null);

        JButton btnNewButton = new JButton("New button");
        btnNewButton.setBounds(0, 0, 117, 29);
        panel.add(btnNewButton);

        JPanel panel_1 = new JPanel();
        contentPane.add(panel_1, BorderLayout.CENTER);
    }

}
4

2 に答える 2

4

更新 1

既に回答を選択されているようです (時期尚早だと思います)。これは、あなたが達成しようとしていると私が信じていることの最初の反復です。境界や優先サイズを設定する必要はありません..

レイアウト 1

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class AAAA extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    AAAA frame = new AAAA();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public AAAA() {
        super("Laid Out");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // OMG! If you can make a GUI break at 1336 px wide, it should be 
        // possible to make it break at ..much smaller!
        //setBounds(100, 100, 1136, 520);
        setBackground(Color.YELLOW);
        contentPane = new JPanel();
        contentPane.setBackground(Color.BLUE);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        // make it a FlowLayout as FlowLayout.LEADING with no spacing to 
        // make the button snug up against the top left
        JPanel panel = new JPanel(
                new FlowLayout(FlowLayout.LEADING, 0, 0));
        panel.setBackground(Color.GREEN);
        contentPane.add(panel, BorderLayout.NORTH);
        //panel.setPreferredSize(new Dimension(1024,400));

        JButton btnNewButton = new JButton("New button");
        // we change the margin to make the button bigger than natural size.
        btnNewButton.setMargin(new Insets(6, 22, 6, 22));
        panel.add(btnNewButton);

        JPanel panel_1 = new JPanel();
        // create a solic color image to both pad the GUI and 
        // provide visual indication of where it is.
        BufferedImage bi = new BufferedImage(
                400,200,BufferedImage.TYPE_INT_RGB);
        JLabel padder = new JLabel(new ImageIcon(bi));
        panel_1.add(padder);
        panel_1.setBackground(Color.RED);
        contentPane.add(panel_1, BorderLayout.CENTER);
        pack();
        setMinimumSize(getSize());
    }
}
于 2014-01-16T16:38:05.847 に答える
3

北の JPanel には絶対位置を使用する必要があります。

なんで?これを行う必要があると思われる理由がわかれば、おそらくより良いアプローチを提供できます。

null レイアウトは使用しないでください。Swing は、レイアウト マネージャーで使用するように設計されています。

BorderLayout は、NORTH に追加されたコンポーネントの優先高を尊重します。優先高はゼロなので、何も表示されません。

注: パネルの適切な高さを設定することをお勧めしているわけではありません。これはレイアウト マネージャーの仕事であり、常にレイアウト マネージャーを使用する必要があるのはそのためです。レイアウト マネージャーは、コンポーネントのサイズ/位置を設定するだけではありません。

于 2014-01-16T16:27:37.590 に答える