问题描述
我需要在更改窗口大小时,右下角的文本字段也会更改其大小。现在大小始终相同。
我使用的布局:
-
3 x
BorderLayout
(红色)- 一个用于整个 GUI,每个用于 GUI 主 GUI 面板的PAGE_START
和PAGE_END
约束。 -
在
PAGE_START
中使用的面板中,2 xFlowLayout
(绿色),一个在LINE_START
,另一个在LINE_END
。 (1) -
在面板中的
PAGE_END
,2 xGridLayout
(蓝色),第一个是 3 x 3、其他单列。
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class MyJFrame extends JFrame {
JPanel pan1 = new JPanel(); // top_left
JPanel pan2 = new JPanel(); // top_right
JPanel tPan = new JPanel(); // top
JPanel pan4 = new JPanel(); // bottom_left
JPanel pan5 = new JPanel(); // bottom_right
JPanel bPan = new JPanel(); // bottom
JPanel mPan = new JPanel(); // main
JButton jButton1 = new JButton("FR");
JButton jButton2 = new JButton("FG");
JButton jButton3 = new JButton("FB");
JButton jButton4 = new JButton("A");
JButton jButton5 = new JButton("B");
JButton jButton6 = new JButton("C");
JTextArea textArea = new JTextArea();
public MyJFrame(){
setTitle("Simple Swing App");
setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
textArea.setFont(new Font("Ubuntu Mono",Font.PLAIN,22));
textArea.setEnabled(false);
textArea.setText(" Obszar tekstowy typu jTextArea\n\n");
textArea.setdisabledTextColor(Color.RED);
JScrollPane scrollPane = new JScrollPane(textArea);
jButton1.setBackground(Color.red);
jButton2.setBackground(Color.green);
jButton3.setBackground(Color.blue);
jButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textArea.setdisabledTextColor(Color.red);
mPan.updateUI();
}
});
jButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textArea.setdisabledTextColor(Color.green);
mPan.updateUI();
}
});
jButton3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textArea.setdisabledTextColor(Color.blue);
mPan.updateUI();
}
});
pan1.setLayout(new FlowLayout());
pan2.setLayout(new FlowLayout());
tPan.setLayout(new BorderLayout());
pan4.setLayout(new GridLayout(3,3,2,2));
pan5.setLayout(new GridLayout(3,1,2));
bPan.setLayout(new BorderLayout());
mPan.setLayout(new BorderLayout(2,2));
bPan.setBorder(BorderFactory.createEmptyBorder(4,4,4));
for (int i=1; i<10; i++) {
JButton jButton = new JButton(i+"");
pan4.add(jButton);
}
for (int i=1; i<4; i++){
JTextField jTextField = new JTextField(" Pole tekstowe " + i + " typu jTextField ");
jTextField.setBackground(Color.WHITE);
jTextField.setBorder(BorderFactory.createLineBorder(Color.CYAN));
jTextField.addKeyListener(new KeyAdapter() {
@Override
public void keyreleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER){
textArea.append(jTextField.getText() + "\n\n");
mPan.updateUI();
}
}
});
pan5.add(jTextField);
}
pan1.add(jButton1);
pan1.add(jButton2);
pan1.add(jButton3);
pan2.add(jButton4);
pan2.add(jButton5);
pan2.add(jButton6);
tPan.add(pan1,BorderLayout.LINE_START);
tPan.add(pan2,BorderLayout.LINE_END);
bPan.add(pan4,BorderLayout.WEST);
bPan.add(pan5,BorderLayout.EAST);
mPan.add(tPan,BorderLayout.PAGE_START);
mPan.add(scrollPane);
mPan.add(bPan,BorderLayout.PAGE_END);
add(mPan);
setResizable(true);
setSize(600,400);
setLocationRelativeto(null);
setVisible(true);
}
}
解决方法
解决这个问题的诀窍是了解 BorderLayout
如何为其中的区域分配额外的空间。例如,假设这是初始大小。
我们可以忽略绿色区域,在问题的 GUI 中不相关。但请注意中间“行”中的红色/蓝色区域。
当用户将 GUI 拖得更大时,红色区域(LINE_START
和 LINE_END
)将被分配任何额外的可用高度,而蓝色区域将获得任何额外的高度或宽度.
GUI 将文本字段放在 LINE_END
中。要让它们获得任何额外的宽度,它们需要在 CENTER
中。
但是等等,现在文本字段不会在中心的第一个红色区域和蓝色区域之间有任何“空白”。有多种方法可以解决此问题:
- 在构建时为边框布局分配额外的水平空间。类似于
new BorderLayout(0,100)
。 - 向
LINE_START
或CENTER
组件添加EmptyBorder
。对于中心组件,它可能看起来像这样new EmptyBorder(0,100,0)
。
我会使用第二个,但两者都应该创建所需的效果。