Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何确保调整父帧的大小时JPanel收缩?_Java_Swing - Fatal编程技术网

Java 如何确保调整父帧的大小时JPanel收缩?

Java 如何确保调整父帧的大小时JPanel收缩?,java,swing,Java,Swing,我有一个基本的notes面板,我希望在调整父jframe的大小时缩小它的宽度,但它没有发生。我正在使用嵌套的gridbaglayouts package com.protocase.notes.views; import com.protocase.notes.controller.NotesController; import com.protocase.notes.model.Subject; import com.protocase.notes.model.Note; import co

我有一个基本的notes面板,我希望在调整父jframe的大小时缩小它的宽度,但它没有发生。我正在使用嵌套的gridbaglayouts

package com.protocase.notes.views;

import com.protocase.notes.controller.NotesController;
import com.protocase.notes.model.Subject;
import com.protocase.notes.model.Note;
import com.protocase.notes.model.database.PMSNotesAdapter;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

/**
 * @author DavidH
 */
public class NotesViewer extends JPanel {
    // <editor-fold defaultstate="collapsed" desc="Attributes">

    private Subject subject;
    private NotesController controller;

    //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Getters N' Setters">
    /**
     * Gets back the current subject.
     * @return 
     */
    public Subject getSubject() {
        return subject;
    }

    public NotesController getController() {
        return controller;
    }

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

    /**
     * Should clear the panel of the current subject and load the details for 
     * the other object.
     * @param subject 
     */
    public void setSubject(Subject subject) {
        this.subject = subject;
    }
    //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Constructor">

    /**
     * -- Sets up a note viewer with a subject and a controller. Likely this 
     *    would be the constructor used if you were passing off from another
     *    NoteViewer or something else that used a notes adapter or controller.
     * @param subject
     * @param controller 
     */
    public NotesViewer(Subject subject, NotesController controller) {
        this.subject = subject;
        this.controller = controller;
        initComponents();
    }

    /** 
     * -- Sets up a note view with a subject and creates a new controller. This
     *    would be the constructor typically chosen if choosing notes was 
     *    infrequent and only one or two notes needs to be displayed.
     * @param subject 
     */
    public NotesViewer(Subject subject) {
        this(subject, new NotesController(new PMSNotesAdapter()));
    }

    /** 
     *  -- Sets up a note view without a subject and creates a new controller. 
     *     This would be for a note viewer without any notes, perhaps populating
     *     as you choose values in another form.
     * @param subject 
     */
    public NotesViewer() {
        this(null);
    }
    //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="initComponents()">

    /**
     * Sets up the view for the NotesViewer
     */
    private void initComponents() {
        // -- Make a new panel for the header
        JPanel panel = new JPanel();

        panel.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();


        c.gridx = 0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridy = 0;
        c.weightx = .5;
        //c.anchor = GridBagConstraints.NORTHWEST;
        JLabel label = new JLabel("Viewing Notes for [Subject]");
        label.setAlignmentX(JLabel.LEFT_ALIGNMENT);
        label.setBorder(BorderFactory.createLineBorder(Color.YELLOW));

        panel.add(label);

        JButton newNoteButton = new JButton("New");
        c = new GridBagConstraints();
//        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 0;
        c.weightx = .5;
        c.anchor = GridBagConstraints.EAST;
        panel.add(newNoteButton, c);

        // -- NotePanels
        c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1;
        c.weighty = 1;
        c.gridx = 0;
        c.gridwidth = 2;


        int y = 1;
        for (Note n : subject.getNotes()) {
            c.gridy = y++;
            panel.add(new NotesPanel(n, controller), c);
        }


        this.setLayout(new GridBagLayout());
        GridBagConstraints pc = new GridBagConstraints();
        pc.gridx = 0;
        pc.gridy = 0;
        pc.weightx = 1;
        pc.weighty = 1;
        pc.fill = GridBagConstraints.BOTH;

        panel.setBackground(Color.blue);
        JScrollPane scroll = new JScrollPane();
        scroll.setViewportView(panel);
        //scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        this.add(scroll, pc);
        //this.add(panel, pc);
        // -- Add it all to the layout
    }
    //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="private methods">
    //</editor-fold>
}
package com.protocase.notes.views;
导入com.protocase.notes.controller.NotesController;
导入com.protocase.notes.model.Subject;
导入com.protocase.notes.model.Note;
导入com.protocase.notes.model.database.PMSNotesAdapter;
导入java.awt.Color;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入javax.swing.BorderFactory;
导入javax.swing.JButton;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JScrollPane;
/**
*@author DavidH
*/
公共类NotesViewer扩展了JPanel{
// 
私人主体;
专用记事本控制器;
//
// 
/**
*返回当前主题。
*@返回
*/
公共主题getSubject(){
返回主题;
}
public NotesController getController(){
返回控制器;
}
公共无效设置控制器(NotesController控制器){
this.controller=控制器;
}
/**
*应清除面板中的当前主题,并加载
*另一个对象。
*@param主题
*/
公共无效设置主体(主体){
this.subject=主语;
}
//
// 
/**
*--设置带有主题和控制器的便笺查看器。可能是这样
*如果你是从另一个冒充来的,会使用构造函数吗
*NoteViewer或使用notes适配器或控制器的其他东西。
*@param主题
*@param控制器
*/
公共NotesViewer(主题、NotesController){
this.subject=主语;
this.controller=控制器;
初始化组件();
}
/** 
*--设置带有主题的便笺视图并创建新控制器。此
*如果选择notes是正确的,则通常会选择构造函数
*不经常,只需要显示一个或两个注释。
*@param主题
*/
公共便笺浏览者(主题){
此(主题,新的NotesController(新的PMSNotesAdapter());
}
/** 
*--设置不带主题的便笺视图并创建新控制器。
*这将适用于没有任何注释的注释查看器,可能是填充
*当您选择另一种形式的值时。
*@param主题
*/
公共注释查看者(){
这个(空);
}
//
// 
/**
*设置NotesViewer的视图
*/
私有组件(){
//--为收割台制作一个新面板
JPanel面板=新的JPanel();
panel.setLayout(新的GridBagLayout());
GridBagConstraints c=新的GridBagConstraints();
c、 gridx=0;
c、 填充=GridBagConstraints.HORIZONTAL;
c、 gridy=0;
c、 权重x=0.5;
//c、 锚点=GridBagConstraints.NORTHWEST;
JLabel标签=新的JLabel(“查看[主题]注释”);
label.setAlignmentX(JLabel.LEFT_对齐);
label.setboorder(BorderFactory.createLineBorder(Color.YELLOW));
面板。添加(标签);
JButton newNoteButton=新JButton(“新”);
c=新的GridBagConstraints();
//c.fill=gridbag.HORIZONTAL;
c、 gridx=1;
c、 gridy=0;
c、 权重x=0.5;
c、 锚点=GridBagConstraints.EAST;
面板。添加(newNoteButton,c);
//--记事板
c=新的GridBagConstraints();
c、 填充=GridBagConstraints.HORIZONTAL;
c、 权重x=1;
c、 权重=1;
c、 gridx=0;
c、 网格宽度=2;
int y=1;
for(注意n:subject.getNotes()){
c、 gridy=y++;
面板。添加(新的NotesPanel(n,控制器),c);
}
this.setLayout(新的GridBagLayout());
GridBagConstraints pc=新的GridBagConstraints();
pc.gridx=0;
pc.gridy=0;
pc.weightx=1;
pc.weighty=1;
pc.fill=GridBagConstraints.BOTH;
面板.立根背景(颜色.蓝色);
JScrollPane scroll=新建JScrollPane();
滚动.setViewportView(面板);
//scroll.setHorizontalScrollBarPolicy(JScrollPane.HorizontalScrollBar\uNever);
添加(滚动,pc);
//添加(面板、pc);
//--将其全部添加到布局中
}
//
// 
//
}

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.BorderFactory;
导入javax.swing.JButton;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JScrollPane;
导入javax.swing.JTextArea;
导入javax.swing.JTextField;
导入javax.swing.border.BevelBorder;
导入javax.swing.border.border;
导入javax.swing.border.MatteBorder;
/**
*@author dah01
*/
公共类NotesPanel扩展了JPanel{
// 
私人票据;
专用记事本控制器;
私人卡布局卡布局;
私人JTextArea viewTextArea;
私人JTextArea editTextArea;
//
// 
public NotesController getController(){
返回控制器;
}
公共无效设置控制器(NotesController控制器){
this.controller=控制器;
}
公共注释getNote(){
退货单;
}
公共无效设置注释(注释){
this.note=注释;
}
//
// 
/**
*设置一个显示有关便笺的所有内容的便笺面板。
*@param注释
*/
公共便笺(便笺,便笺控制器
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.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.MatteBorder;

/**
 * @author dah01
 */
public class NotesPanel extends JPanel {
    // <editor-fold defaultstate="collapsed" desc="Attributes">
    private Note note;
    private NotesController controller;
    private CardLayout cardLayout;
    private JTextArea viewTextArea;
    private JTextArea editTextArea;
    //</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", initViewPanel());
        this.add("EditPanel", initEditPanel());

    }
    //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="EditPanel">
    private JPanel initEditPanel() {
        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(initCreatorLabel(), c);

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

        c.gridy++;
        c.anchor = GridBagConstraints.WEST;
        c.fill = GridBagConstraints.NONE;
        editPanel.add(initEditorLabel(), c);

        c.gridx++;
        c.anchor = GridBagConstraints.EAST;
        editPanel.add(initSaveButton(), c);




        return editPanel;
    }

    private JScrollPane initEditTextScroll() {
        this.editTextArea = new JTextArea(note.getContents());
        editTextArea.setLineWrap(true);
        editTextArea.setWrapStyleWord(true);
        JScrollPane scrollPane = new JScrollPane(editTextArea);
        scrollPane.setAlignmentX(JScrollPane.LEFT_ALIGNMENT);
        Border b = scrollPane.getViewportBorder();

        MatteBorder mb = BorderFactory.createMatteBorder(2, 2, 2, 2, Color.BLUE);

        scrollPane.setBorder(mb);
        return scrollPane;
    }

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

        final NotesController c = this.controller;
        final Note n = this.note;
        final JTextArea noteText = this.viewTextArea;
        final JTextArea textToSubmit = this.editTextArea;
        ActionListener al = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //controller.saveNote(n);
                noteText.setText(textToSubmit.getText());
                l.next(p);
            }
        };

        JButton saveButton = new JButton("Save");
        saveButton.addActionListener(al);
        saveButton.setPreferredSize(new Dimension(62, 26));



        return saveButton;
    }

    //</editor-fold>
    // <editor-fold defaultstate="collapsed" desc="ViewPanel">
    private JPanel initViewPanel() {
        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(initCreatorLabel(), c);

        c.gridy++;

        viewPanel.add(this.initNoteTextArea(), c);

        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.WEST;
        c.gridy++;
        viewPanel.add(initEditorLabel(), c);

        c.gridx++;
        c.anchor = GridBagConstraints.EAST;
        viewPanel.add(initEditButton(), c);

        return viewPanel;
    }

    private JLabel initCreatorLabel() {
        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 initNoteTextArea() {
        // -- Setup the notes area.
        this.viewTextArea = new JTextArea(note.getContents());
        viewTextArea.setEditable(false);
        viewTextArea.setLineWrap(true);
        viewTextArea.setWrapStyleWord(true);
        JScrollPane scrollPane = new JScrollPane(viewTextArea);
        scrollPane.setAlignmentX(JScrollPane.LEFT_ALIGNMENT);

        return scrollPane;
    }

    private JLabel initEditorLabel() {
        // -- 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 initEditButton() {
        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.setPreferredSize(new Dimension(62,26));
        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>
}
MyParentComponent.addComponentListener(new java.awt.event.ComponentAdapter() {
    public void componentResized(java.awt.event.ComponentEvent evt) {
        ComponentResized(evt);
    }
});
private void ComponentResized(ComponentEvent evt)
{
    MyChildComponent.setSize(newWidth, newHeight);
}