import javax.swing.*;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import static java.awt.GridBagConstraints.BOTH;
public class mwe extends JFrame {
private JPanel x, y, z, u,v,w,jtext;
private class MyBorderedPanel extends JPanel {
MyBorderedPanel( String title ) {
this.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), title
));
}
}
private class MyTextPanel extends JPanel {
JTextArea textArea;
MyTextPanel( String title ) {
textArea= new JTextArea();
this.setBorder(BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(EtchedBorder.LOWERED),
title));
this.setLayout(new BorderLayout());
JScrollPane pane= new JScrollPane();
pane.add(textArea);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
this.add(pane);
}
}
public mwe() {
x= new MyBorderedPanel("x");
y= new MyBorderedPanel("y");
z= new MyBorderedPanel("z");
u= new MyBorderedPanel("u");
v= new MyBorderedPanel("v");
w= new MyBorderedPanel("w");
jtext= new MyTextPanel("textArea");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
manageLayout();
}
private void manageLayout() {
this.setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = BOTH;
}
}
我有上面的代码模拟了应用于 GridBagLayout() 的黄金比例的想法。输出如下:
现在,您可以看到 TextArea 是空的。我希望它的托管面板基本上充满“白色”空间,即使此时文本区域是空的。我记得在 SE 读到过这BorderLayout
是可行的方法,所以我尝试了。因此,本质上,我希望文本区域将其托管面板填充到边缘,并且滚动窗格也可见。
如何实现这一目标?上述 MWE 中最相关的代码可能是 MyTextPanel 类。
陪伴而非守候
相关分类