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

Java 在调整父级大小时,如何使JScrollPane增长和收缩?,java,swing,jscrollpane,layout-manager,gridbaglayout,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.

我有一个带有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>
}
package com.protocase.notes.views;
导入com.protocase.notes.controller.NotesController;
导入com.protocase.notes.model.Note;
导入java.awt.CardLayout;
导入java.awt.Color;
导入java.awt.Component;
导入java.awt.Dimension;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.text.DateFormat;
导入java.text.simpleDataFormat;
导入javax.swing.BoxLayout;
导入javax.swing.JButton;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JScrollPane;
导入javax.swing.JTextArea;
导入javax.swing.border.BevelBorder;
/**
*@author dah01
*/
公共类NotesPanel扩展了JPanel{
// 
私人票据;
专用记事本控制器;
私人卡布局卡布局;
//
// 
public NotesController getController(){
返回控制器;
}
公共无效设置控制器(NotesController控制器){
this.controller=控制器;
}
公共注释getNote(){
退货单;
}
公共无效设置注释(注释){
this.note=注释;
}
//
// 
/**
*设置一个显示有关便笺的所有内容的便笺面板。
*@param注释
*/
公共NotesPanel(Note Note,NotesController控制器){
this.note=注释;
cardLayout=新的cardLayout();
这个.setLayout(cardlyout);
//--设置布局管理器。
这是挫折(新颜色(199187192));
此.setboorder(新的BevelOrder(BevelOrder.RAISED));
//--视图面板
添加(“ViewPanel”,makeViewPanel());
add(“EditPanel”,makeEditPanel());
}
//
// 
私有JPanel makeEditPanel(){
JPanel editPanel=新的JPanel();
setLayout(新的GridBagLayout());
GridBagConstraints c=新的GridBagConstraints();
c、 填充=GridBagConstraints.HORIZONTAL;
c、 gridy=0;
c、 权重x=1;
c、 权重y=0.3;
add(makeCreatorLabel(),c);
c、 gridy++;
add(makeEditTextScroll(),c);
c、 gridy++;
add(makeEditorLabel(),c);
c、 gridy++;
c、 锚点=GridBagConstraints.EAST;
add(makeSaveButton(),c);
返回编辑面板;
}
私有JScrollPane makeEditTextScroll(){
JTextArea notesContentsArea=新的JTextArea(note.getContents());
notesContentsArea.setLineWrap(真);
notesContentsArea.setWrapStyleWord(true);
JScrollPane scrollPane=新的JScrollPane(注内容区域);
scrollPane.setAlignmentX(JScrollPane.LEFT_对齐);
返回滚动窗格;
}
私有JButton makeSaveButton(){
最终CardLayout l=此.CardLayout;
最终JPanel p=此;
最终注释控制器c=this.controller;
最终注释n=本注释;
ActionListener al=新的ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
//控制器.saveNote(n);
l、 其次(p);
}
};
JButton saveButton=新JButton(“保存”);
saveButton.addActionListener(al);
返回保存按钮;
}
//
// 
私有JPanel makeViewPanel(){
JPanel viewPanel=新的JPanel();
setLayout(新的GridBagLayout());
GridBagConstraints c=新的GridBagConstraints();
c、 填充=GridBagConstraints.HORIZONTAL;
c、 gridy=0;
c、 权重x=1;
c、 权重y=0.3;
添加(makeCreatorLabel(),c);
c、 gridy++;
添加(makeNoteTextArea(),c);
c、 gridy++;
添加(makeEditorLabel(),c);
c、 gridy++;
c、 锚点=GridBagConstraints.EAST;
添加(makeEditButton(),c);
返回视图面板;
}
私有JLabel makeCreatorLabel(){
DateFormat格式化程序=新的SimpleDataFormat(“yyyy-MM-dd”);
如果(注意!=null){
字符串noteBy=“noteBy”+Note.getCreator();
字符串noteCreated=formatter.format(note.getDateCreated());
JLabel creatorLabel=新的JLabel(noteBy+“@”+noteCreated);
creatorLabel.setAlignmentX(JLabel.LEFT_对齐);
返回创建者标签;
}否则{
System.out.println(“注释为空”);
返回null;
}
}
私有JScrollPane makeNoteTextArea(){
//--设置注释区域。
JTextArea notesContentsArea=新的JTextArea(note.getContents());
notesContentsArea.setEditable(假);
notesContentsArea.setLineWrap(真);
notesContentsArea.setWrapStyleWord(true);
JScrollPane scrollPane=新的JScrollPane(注内容区域);
scrollPane.setAlignmentX(JScrollPane.LEFT_对齐);
返回滚动窗格;
}
私有JLabel makeEditorLabel(){
//--设置“编辑人”标签。
JLabel editorLabel=new JLabel(“--Last edited by”+note.getlastdedited()+”位于“+note.getDateModified()”);
editorLabel.setAlignmentX(组件左对齐);
返回编辑器标签;
}
私有JButton makeEditButton(){
最终CardLayout l=此.CardLayout;
最终JPanel p=此;
ActionListener ar=新的ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
l、 其次(p);
}
};
JButton editButton=新JButton(“编辑”);
editButton.addActionListener(ar);
返回编辑按钮;
}
//
//