微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

简单 JAVA 摆动窗口中可调整大小的面板尺寸

如何解决简单 JAVA 摆动窗口中可调整大小的面板尺寸

我有一个 Java Swing 应用,如下图所示:

enter image description here

我需要在更改窗口大小时,右下角的文本字段也会更改其大小。现在大小始终相同。

我使用的布局:

  • 3 x BorderLayout(红色)- 一个用于整个 GUI,每个用于 GUI 主 GUI 面板的 PAGE_STARTPAGE_END 约束。

  • PAGE_START 中使用的面板中,2 x FlowLayout(绿色),一个LINE_START,另一个LINE_END。 (1)

  • 在面板中的 PAGE_END,2 x GridLayout(蓝色),第一个是 3 x 3、其他单列。

enter image description here

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 如何为其中的区域分配额外的空间。例如,假设这是初始大小。

enter image description here

我们可以忽略绿色区域,在问题的 GUI 中不相关。但请注意中间“行”中的红色/蓝色区域。

enter image description here

当用户将 GUI 拖得更大时,红色区域(LINE_STARTLINE_END)将被分配任何额外的可用高度,而蓝色区域将获得任何额外的高度或宽度.

GUI 将文本字段放在 LINE_END 中。要让它们获得任何额外的宽度,它们需要CENTER 中。

但是等等,现在文本字段不会在中心的第一个红色区域和蓝色区域之间有任何“空白”。有多种方法可以解决此问题:

  1. 在构建时为边框布局分配额外的水平空间。类似于 new BorderLayout(0,100)
  2. LINE_STARTCENTER 组件添加 EmptyBorder。对于中心组件,它可能看起来像这样 new EmptyBorder(0,100,0)

我会使用第二个,但两者都应该创建所需的效果。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。