6
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.HeadlessException;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
 * @author dah01
 */
public class Main {
    private static int panelNumber = 1;
    public static final String fillerText = ""
            + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec "
            + "placerat aliquam magna, in faucibus ante pharetra porta. "
            + "Vestibulum ante ipsum primis in faucibus orci luctus et ultrices "
            + "posuere cubilia Curae; Quisque eu dui ligula. Donec consequat "
            + "fringilla enim eu tempus. Ut pharetra velit id sapien vehicula "
            + "scelerisque. Proin sit amet tellus enim, et gravida dui. Ut "
            + "laoreet tristique tincidunt. Integer pharetra pulvinar neque id "
            + "dignissim. Praesent gravida dapibus ornare. Aenean facilisis "
            + "consectetur tincidunt. Donec ante tellus, vehicula vitae "
            + "ultrices eget, scelerisque cursus turpis. Quisque volutpat odio "
            + "sed risus posuere adipiscing. In pharetra elit id risus ornare "
            + "ut bibendum orci luctus. In sollicitudin gravida neque quis "
            + "lobortis. Morbi id justo enim. Aliquam eget orci lorem.";
    public static void main(String[] args) {
        JPanel innerPanelOne = getPanel();

        //SECOND PANEL
        JPanel innerPanelTwo = getPanel();

        //MIDDLE PANEL
        JPanel middlePanel = new JPanel();
        middlePanel.setBackground(Color.RED);
        middlePanel.setLayout(new BoxLayout(middlePanel, BoxLayout.PAGE_AXIS));
        middlePanel.add(innerPanelOne);
        middlePanel.add(innerPanelTwo);

        //OUTER PANEL
        JPanel outerPanel = new JPanel();
        outerPanel.setBackground(Color.BLUE);
        outerPanel.add(middlePanel);

        createAndShowGUI(outerPanel);
    }

    private static void createAndShowGUI(JPanel outerPanel) throws HeadlessException {
        //FRAME
        JFrame frame = new JFrame();
        frame.setContentPane(outerPanel);
        frame.setSize(500, 500);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private static JPanel getPanel() {
        //TEXT AREA
        JTextArea t = new JTextArea(fillerText);
        t.setWrapStyleWord(true);
        t.setLineWrap(true);


        //Scroll
        JScrollPane scrollOne = new JScrollPane();
        scrollOne.setViewportView(t);

        //Label
        JLabel l = new JLabel("LABEL " +panelNumber++);
        //FIRST PANEL
        JPanel p = new JPanel();
        p.setLayout(new BorderLayout());
        p.setBackground(Color.GREEN);
        p.add(l,BorderLayout.NORTH);
        p.add(scrollOne,BorderLayout.SOUTH);

        t.setMaximumSize(new Dimension(1920,1080));
        p.setMaximumSize(new Dimension(1920,1080));
        l.setMaximumSize(new Dimension(1920,1080));
        return p;
    }
}

編集:

package com.protocase.notes.views;

import com.protocase.notes.controller.NotesController;
import com.protocase.notes.model.Note;
import com.protocase.notes.model.User;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.BevelBorder;

/**
 * @author dah01
 */
public class NotesPanel extends JPanel {

    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setSize(500, 500);
        Note note = new Note();
        User u = new User();
        note.setCreator(u);
        note.setLastEdited(u);
        note.setDateCreated(new Date());
        JPanel panel = new JPanel();
        panel.add(new NotesPanel(note, null));
        panel.add(new NotesPanel(note, null));
        panel.setBackground(Color.red);

        panel.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE));
        f.setContentPane(panel);
        f.setVisible(true);
    }
    // <editor-fold defaultstate="collapsed" desc="Attributes">
    private Note note;
    private NotesController controller;
    //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Getters N' Setters">

    public NotesController getController() {
        return controller;
    }

    public void setController(NotesController controller) {
        this.controller = controller;
    }

    public Note getNote() {
        return note;
    }

    public void setNote(Note note) {
        this.note = note;
    }
    //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Constructor">

    /**
     * Sets up a note panel that shows everything about the note.
     * @param note 
     */
    public NotesPanel(Note note, NotesController controller) {
        // -- Setup the layout manager.
        this.setBackground(new Color(199, 187, 192));
        this.setLayout(new GridBagLayout());
        this.setBorder(new BevelBorder(BevelBorder.RAISED));
        GridBagConstraints c = new GridBagConstraints();

        // -- Setup the creator section.
        JLabel creatorLabel = new JLabel("Note by " + note.getCreator() + " @ " + note.getDateCreated());
        creatorLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT);
        c.fill = GridBagConstraints.HORIZONTAL;

        c.gridy = 0;
        c.weightx = 1;
        c.weighty = 0.3;
        this.add(creatorLabel, c);

        // -- Setup the notes area.
        JTextArea notesContentsArea = new JTextArea(note.getContents());
        notesContentsArea.setEditable(false);
        notesContentsArea.setLineWrap(true);
        notesContentsArea.setWrapStyleWord(true);



        JScrollPane scrollPane = new JScrollPane(notesContentsArea);
        scrollPane.setAlignmentX(JScrollPane.LEFT_ALIGNMENT);


        c.gridy = 1;


        this.add(scrollPane, c);

        // -- Setup the edited by label.
        JLabel editorLabel = new JLabel(" -- Last edited by " + note.getLastEdited() + " at " + note.getDateModified());
        editorLabel.setAlignmentX(Component.LEFT_ALIGNMENT);


        c.gridy = 2;


        this.add(editorLabel, c);

        // -- Add everything to the view.

        this.setBackground(Color.yellow);
        //this.add(creatorLabel);
        //this.add(scrollPane);
        //this.add(editorLabel);
    }
    //</editor-fold>

    @Override
    public Dimension getPreferredSize() {
        Dimension d = super.getPreferredSize();
        d.width = this.getParent().getSize().width;
        return d;
    }

}
4

4 に答える 4

11

コンテナがそのコンポーネントをすべてその幅に一致させたい場合は、を使用する必要がない限り、これをより適切に行うことができる多くの選択肢がありますBoxLayout。1つは標準GridBagLayoutです。panelコンテナと3つのコンポーネントabおよびが与えられたc場合、コードは次のようになります。

panel.setLayout(new GridBagLayout());
GridBagConstraints cons = new GridBagConstraints();
cons.fill = GridBagConstraints.HORIZONTAL;
cons.weightx = 1;
cons.gridx = 0;

panel.add(a, cons);
panel.add(b, cons);
panel.add(c, cons);

コンポーネントがコンテナの幅と一致させたい場合(これは通常はお勧めできません)、代わりにコンポーネントのコンストラクタを作成して、コンテナへの参照を受け取りgetPreferredSize、渡されたコンポーネントに基づいて計算を行うようにオーバーライドします。ただし、レイアウトはほとんどの場合、親に任されている必要があります。

于 2012-08-29T09:13:43.867 に答える
3

BoxLayoutはコンポーネントの推奨サイズによってガイドされるため、次を使用できます。

JPanel middlePanel = new JPanel() {
   public Dimension getPreferredSize() {
       return outerPanel.getSize();
   };
};

親の寸法を取ります。

于 2012-08-28T21:35:06.860 に答える
0

私にとっての良い回避策は、SpringLayoutを使用することでした。

このレイアウトの使用に役立つOracleのテンプレートを使用するだけです

その後、コンポーネントを(制約なしで)追加し、これを呼び出す必要があります:

SpringUtilities.makeCompactGrid(yourPanel, x, 1, 0, 0, 0, 0);
// x stands for rowCount. Zeros are margins.
于 2014-11-25T10:33:32.530 に答える
0

BoxLayoutの使用方法のドキュメントには次のように書かれています。

すべてのコンポーネントのXアライメントが同じである場合、すべてのコンポーネントはコンテナと同じ幅になります。

したがって、BoxLayoutを使用する場合は、すべてのコンポーネントに同じXアライメントを設定するだけで十分です。

        c1.setAlignmentX(Component.CENTER_ALIGNMENT)
        c2.setAlignmentX(Component.CENTER_ALIGNMENT)
        ...

于 2021-02-25T19:12:43.453 に答える