在调整父级调整大小时,如何使我的JScrollPane增长和缩小?

时间:2012-09-11 12:20:57

标签: java swing jscrollpane layout-manager gridbaglayout

我有一个带有JPanel的框架,它有一个JScroll,我想要的是,如果调整框架大小,JScrollPane的宽度会增长以适应它。这个功能似乎工作正常,但是,在调整到较小的维度时,我的JScrollPane不会缩小。

package com.protocase.notes.views;

import com.protocase.notes.controller.NotesController;
import com.protocase.notes.model.Note;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.swing.BoxLayout;
import javax.swing.JButton;
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 {
    // <editor-fold defaultstate="collapsed" desc="Attributes">
    private Note note;
    private NotesController controller;
    private CardLayout cardLayout;
    //</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) {
        this.note = note;
        cardLayout = new CardLayout();
        this.setLayout(cardLayout);

        // -- Setup the layout manager.
        this.setBackground(new Color(199, 187, 192));
        this.setBorder(new BevelBorder(BevelBorder.RAISED));

        // -- ViewPanel
        this.add("ViewPanel", makeViewPanel());
        this.add("EditPanel", makeEditPanel());
    }
    //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="EditPanel">
    private JPanel makeEditPanel() {
        JPanel editPanel = new JPanel();
        editPanel.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridy = 0;
        c.weightx = 1;
        c.weighty = 0.3;
        editPanel.add(makeCreatorLabel(), c);

        c.gridy++;
        editPanel.add(makeEditTextScroll(), c);

        c.gridy++;
        editPanel.add(makeEditorLabel(), c);

        c.gridy++;
        c.anchor = GridBagConstraints.EAST;
        editPanel.add(makeSaveButton(), c);

        return editPanel;
    }

    private JScrollPane makeEditTextScroll() {
        JTextArea notesContentsArea = new JTextArea(note.getContents());
        notesContentsArea.setLineWrap(true);
        notesContentsArea.setWrapStyleWord(true);
        JScrollPane scrollPane = new JScrollPane(notesContentsArea);
        scrollPane.setAlignmentX(JScrollPane.LEFT_ALIGNMENT);
        return scrollPane;
    }

    private JButton makeSaveButton() {
        final CardLayout l = this.cardLayout;
        final JPanel p = this;

        final NotesController c = this.controller;
        final Note n = this.note;
        ActionListener al = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //controller.saveNote(n);
                l.next(p);
            }
        };

        JButton saveButton = new JButton("Save");
        saveButton.addActionListener(al);

        return saveButton;
    }

    //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="ViewPanel">
    private JPanel makeViewPanel() {
        JPanel viewPanel = new JPanel();
        viewPanel.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        c.fill = GridBagConstraints.HORIZONTAL  ;
        c.gridy = 0;
        c.weightx = 1;
        c.weighty = 0.3;
        viewPanel.add(makeCreatorLabel(), c);

        c.gridy++;
        viewPanel.add(makeNoteTextArea(), c);

        c.gridy++;
        viewPanel.add(makeEditorLabel(), c);

        c.gridy++;
        c.anchor = GridBagConstraints.EAST;
        viewPanel.add(makeEditButton(), c);

        return viewPanel;
    }

    private JLabel makeCreatorLabel() {
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        if (note != null) {
            String noteBy = "Note by " + note.getCreator();

            String noteCreated = formatter.format(note.getDateCreated());
            JLabel creatorLabel = new JLabel(noteBy + " @ " + noteCreated);
            creatorLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT);
            return creatorLabel;
        } else {
            System.out.println("NOTE IS NULL");
            return null;
        }


    }

    private JScrollPane makeNoteTextArea() {
        // -- 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);
        return scrollPane;
    }

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

    private JButton makeEditButton() {
        final CardLayout l = this.cardLayout;
        final JPanel p = this;

        ActionListener ar = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                l.next(p);
            }
        };

        JButton editButton = new JButton("Edit");
        editButton.addActionListener(ar);

        return editButton;
    }
    //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Grow Width When Resized">

    @Override
    public Dimension getPreferredSize() {
        int fw  = this.getParent().getSize().width;
        int fh = super.getPreferredSize().height;
        return new Dimension(fw,fh);
    }
    //</editor-fold>
}

2 个答案:

答案 0 :(得分:1)

解决方案是停止覆盖外部组件的getPrefferedSize ....

答案 1 :(得分:-1)

在JTextArea上尝试setLineWrap(false)。