Я делаю панель с тремя JTextAreas, каждая из которых является имитацией обертки JLabel. Здесь у вас есть пример кода:
public static String getLoremIpsumString() {
return "Lorem ipsum dolor sit amet, consectetur adipisicing "
+ "elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
+ "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip "
+ "ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate "
+ "velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat "
+ "non proident, sunt in culpa qui officia deserunt mollit anim id est laborum";
}
public static void main(String[] args) {
final JFrame frameMain = new JFrame(){
@Override
public Dimension getPreferredSize() {
Dimension prefDim = super.getPreferredSize();
prefDim.width = 600;
return prefDim;
}
};
JPanel pnlMain = new JPanel();
JTextArea txtAreaLeft = new JTextArea(getLoremIpsumString());
JTextArea txtAreaRight = new JTextArea(getLoremIpsumString());
JTextArea txtAreaBottom = new JTextArea(getLoremIpsumString());
txtAreaLeft.setWrapStyleWord(true);
txtAreaRight.setWrapStyleWord(true);
txtAreaBottom.setWrapStyleWord(true);
txtAreaLeft.setLineWrap(true);
txtAreaRight.setLineWrap(true);
txtAreaBottom.setLineWrap(true);
txtAreaLeft.setEditable(false);
txtAreaRight.setEditable(false);
txtAreaBottom.setEditable(false);
GroupLayout layout = new GroupLayout(pnlMain);
pnlMain.setLayout(layout);
layout.setAutoCreateContainerGaps(true);
layout.setAutoCreateGaps(true);
layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(txtAreaLeft)
.addComponent(txtAreaRight)
)
.addComponent(txtAreaBottom)
);
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(txtAreaLeft)
.addComponent(txtAreaRight)
)
.addComponent(txtAreaBottom)
);
txtAreaLeft.setBackground(txtAreaLeft.getParent().getBackground());
txtAreaRight.setBackground(txtAreaRight.getParent().getBackground());
txtAreaBottom.setBackground(txtAreaBottom.getParent().getBackground());
frameMain.setContentPane(pnlMain);
frameMain.setVisible(true);
frameMain.pack();
frameMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
На изображении ниже показан результат:
И вопросы:
1) Что я должен сделать, чтобы заставить его в самом начале выглядеть так:
2) После того, как я изменил размер кадра на более крупный (кадр должен быть изменен), а затем я попытаюсь вернуться к меньшему размеру, текстовые поля не будут изменены (не станут меньше). Как мне это исправить? Я бы хотел, чтобы они изменили размер вместе с изменением размера рамки.
Размер каждого компонента в GroupLayout ограничен тремя значениями; минимальный размер, предпочтительный размер и максимальный размер. Эти размеры определяют, как изменяется размер компонента внутри макета. Метод GroupLayout.addComponent(...) позволяет указать ограничения размера.
Кажется, вам просто нужно указать эти ограничения:
.addComponent(txtAreaLeft)
.addComponent(txtAreaRight)
)
.addComponent(txtAreaBottom)
и все будет готово.
Для будущего использования: после ответа elcodedocle я немного изменил детали размера макета. Теперь он работает:
Исправленный основной код:
public static void main(String[] args) {
final JFrame frameMain = new JFrame(){
@Override
public Dimension getPreferredSize() {
Dimension prefDim = super.getPreferredSize();
prefDim.width = 600;
return prefDim;
}
};
JPanel pnlMain = new JPanel();
JTextArea txtAreaLeft = new JTextArea(getLoremIpsumString(), 10, 15);
JTextArea txtAreaRight = new JTextArea(getLoremIpsumString(), 10, 15);
JTextArea txtAreaBottom = new JTextArea(getLoremIpsumString(), 5, 15);
txtAreaLeft.setWrapStyleWord(true);
txtAreaRight.setWrapStyleWord(true);
txtAreaBottom.setWrapStyleWord(true);
txtAreaLeft.setLineWrap(true);
txtAreaRight.setLineWrap(true);
txtAreaBottom.setLineWrap(true);
txtAreaLeft.setEditable(false);
txtAreaRight.setEditable(false);
txtAreaBottom.setEditable(false);
GroupLayout layout = new GroupLayout(pnlMain);
pnlMain.setLayout(layout);
layout.setAutoCreateContainerGaps(true);
layout.setAutoCreateGaps(true);
layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(txtAreaLeft, 1, GroupLayout.PREFERRED_SIZE, Short.MAX_VALUE)
.addComponent(txtAreaRight, 1, GroupLayout.PREFERRED_SIZE, Short.MAX_VALUE)
)
.addComponent(txtAreaBottom, 1, GroupLayout.PREFERRED_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(txtAreaLeft)
.addComponent(txtAreaRight)
)
.addComponent(txtAreaBottom)
);
txtAreaLeft.setBackground(txtAreaLeft.getParent().getBackground());
txtAreaRight.setBackground(txtAreaRight.getParent().getBackground());
txtAreaBottom.setBackground(txtAreaBottom.getParent().getBackground());
frameMain.setContentPane(pnlMain);
frameMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frameMain.pack();
frameMain.setVisible(true);
}