在下面的代码中,我希望在NotesPanel中的面板可以调整大小,因为容纳NotesPanel的JFrame可以调整大小。
可运行的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的问题,设置最大大小似乎没有任何益处。
最佳答案
我对您的代码做了一些更改以对其进行测试。
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>
}
由于一切大小调整正确,我看不到您的问题在哪里。
以下是在不同窗口大小下对我的显示效果的屏幕截图:
您想实现什么不同?
关于java - 如何在具有灵活宽度的情况下将组件沿y轴动态添加到JPanel?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12163114/