与GridBagLayout有点混淆。
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.event.*;
public class MihatteFrame extends JFrame {
private JTextArea area;
private JTextField textField;
private Button b;
final static boolean shouldFill = true;
public MihatteFrame() {
setTitle("見張ってしながら...");
setSize(500,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if (shouldFill) {
c.fill = GridBagConstraints.HORIZONTAL;
}
area = new JTextArea(5, 15);
area.setEditable(false);
area.setCursor(null);
area.setOpaque(false);
area.setFocusable(false);
area.setLineWrap(true);
area.setWrapStyleWord(true);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0;
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.WEST;
this.add(area, c);
textField = new JTextField(20);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1;
c.gridx = 2;
c.gridy = 0;
c.anchor = GridBagConstraints.CENTER;
this.add(textField);
//textField.addActionListener(this);
b = new Button("Proceed");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0;
c.gridx = 2;
c.gridy = 0;
this.add(b);
setVisible(true);
}
public void displayText(String text) {
area.setText(text);
//textField.setText(text)
}
}
我理解基础知识及其运作良好,它正在整理调整大小(特别是缩小尺寸)的问题。
当文本区域权重为0且文本字段权重为1时,调整大小没有问题,但文本区域位于中心,而不是左上方,尽管有锚定。
当文本区域权重大于0并且文本字段权重相关(例如0.2 / 0.8)时,一切看起来都像一个bug,并且升级很好,但缩小尺寸,文本字段减少到1个字符宽。
我不介意使用框架调整文本区域的大小,但是我希望它在框架变小时返回到原始大小,而不是将文本区域移动到继续按钮。
我已经读过,我可能需要将它放在JPanel边框布局中 - 我不确定这在代码中会是什么样子,所以如果有人能告诉我在哪种情况下会发生这种情况,那将不胜感激。
所以问题:
谢谢! (请注意,在执行actionlistener的过程中,如果它看起来半完整,那就是原因)。
答案 0 :(得分:2)
将ipadx用于JTextField
。我还建议将JTextArea
放在JScrollPane
。
您对JTextField
和JButton
两次使用c.gridx = 2。
最好发布SSCCE让我们检查一下是什么问题。