Java 将JTextField设置为可包装的JLabel-不受欢迎的行为
我正在制作一个包含三个jTextArea的面板,每个jTextArea都是可包装JLabel的仿制品。 这里有一个示例代码:Java 将JTextField设置为可包装的JLabel-不受欢迎的行为,java,swing,layout,resize,Java,Swing,Layout,Resize,我正在制作一个包含三个jTextArea的面板,每个jTextArea都是可包装JLabel的仿制品。 这里有一个示例代码: public static String getLoremIpsumString() { return "Lorem ipsum dolor sit amet, consectetur adipisicing " + "elit, sed do eiusmod tempor incididunt ut labore et
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中每个组件的大小受三个约束 价值观最小尺寸、首选尺寸和最大尺寸。这些尺寸 控制组件在布局中调整大小的方式。这个 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);
}
是的,谢谢。我试试看。我想在这种情况下,我可以依赖默认设置。。。不是一个好方法;)