2

次のコードでは、NotesPanel を保持する JFrame のサイズが変更されたときに、NotesPanel 内にあるパネルのサイズを変更しようとしています。

実行可能な CSSE:

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.util.Date;
import javax.swing.BoxLayout;
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);
        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 BoxLayout(this,BoxLayout.Y_AXIS));
        this.setBorder(new BevelBorder(BevelBorder.RAISED));



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


        // -- 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);

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

        // -- Add everything to the view.
        Dimension wD = new Dimension(Short.MAX_VALUE, Short.MAX_VALUE);
        scrollPane.setMaximumSize(wD);
        this.setBackground(Color.yellow);
        this.setMaximumSize(wD);
        this.add(creatorLabel);
        this.add(scrollPane);
        this.add(editorLabel);
    }
    //</editor-fold>

}

編集:これは BoxLayout の問題です。最大サイズを設定しても何のメリットもないようです。

4

1 に答える 1

4

テストするためにコードを少し変更しました。

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

/**
 * @author dah01
 */
public class T {
    public static void main(String[] args) {
        JFrame f= new JFrame();     
        f.setSize(500,500);
        f.add(new NotesPanel());
        f.setVisible(true);
    }
}
class NotesPanel extends JPanel{
    public NotesPanel(){
        // -- Setup the layout manager.
        setBackground(new Color(199, 187, 192));
        setLayout(new GridLayout());


        // -- Setup the border for the notes pane.
        this.setBorder(new EmptyBorder(8, 3, 8, 3));


        // -- Setup the border for the notes pane.
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
        panel.setAlignmentX(JPanel.LEFT_ALIGNMENT);
        panel.setBorder(new BevelBorder(BevelBorder.RAISED));




        // -- Setup the creator section.
        JLabel creatorLabel = new JLabel("Note by "+ " @ ");
        creatorLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT);


        // -- Setup the notes area.
        JTextArea notesContentsArea = new JTextArea("Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.");
        notesContentsArea.setEditable(false);
        notesContentsArea.setLineWrap(true);
        notesContentsArea.setWrapStyleWord(true);

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

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

        // -- Add everything to the view.
        panel.add(creatorLabel);
        panel.add(scrollPane);
        panel.add(editorLabel);
        this.add(panel);
    }
    //</editor-fold>

}

すべてが適切にサイズ変更されているため、問題がどこにあるのかわかりません。

これは、さまざまなウィンドウサイズで私がどのように見えるかのスクリーンショットです。 1 2 3

どのような違いを達成したいですか?

于 2012-08-28T16:38:42.750 に答える