简体   繁体   English

根据 JFrame 的大小更改 JPanel 组件的大小

[英]Changing size of the components of JPanel depending on the size of the JFrame

In most of the GUI programs, when the user resizes it, the components of the program, such as text fields, buttons, etc. tend to increase or decrease their size depending on the decisions of the user.在大多数 GUI 程序中,当用户调整它的大小时,程序的组件(例如文本字段、按钮等)往往会根据用户的决定增加或减小它们的大小。 I'm trying to implement this idea into my GUI program.我正在尝试将这个想法实现到我的 GUI 程序中。 I'm a little bit lost about how I can do it.我对如何做到这一点有点迷茫。 By the way, I created my program without the usage/help of the Eclipse Swing or Netbeans' GUI.顺便说一下,我在没有使用 Eclipse Swing 或 Netbeans GUI 的情况下创建了我的程序。

import javax.swing.*;
import java.awt.*;
import java.io.IOException;

public class CodeReviewerFrame extends JFrame {
    EditorAreaPanel display = new EditorAreaPanel();
    // FileOptionsPanel fileOptionsPanel = new FileOptionsPanel( display );
    JPanel p = new JPanel();
    JPanel panel = new JPanel();

    public CodeReviewerFrame(String title) throws IOException {

        super(title);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(1500, 1000));

        setLayout(new BorderLayout());

        ImageIcon img = new ImageIcon("icon.png");
        setIconImage(img.getImage());

        p.setLayout(new BorderLayout());
        p.add(new HomeOptionsPanel(display), BorderLayout.LINE_START);
        p.add(new NewCommentPanel(display), BorderLayout.CENTER);
        p.add(new CommentOptionsPanel(display), BorderLayout.LINE_END);
        add(p, BorderLayout.PAGE_START);

        panel.setLayout(new BorderLayout());
        panel.add(new FileExplorerPanel(), BorderLayout.LINE_START);
        panel.add(new FileOptionsPanel(display), BorderLayout.CENTER);
        panel.add(new CommentShowPanel(display), BorderLayout.LINE_END);
        add(panel, BorderLayout.CENTER);

        pack();
        setResizable(true);
        setVisible(true);

        /**
         * Everything Under This is experimental
         */
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridx = 0;
        constraints.gridy = 0;
        //add ( fileOptionsPanel, constraints );
    }
}

There are components on each Panel that have been added, such as buttons in HomeOptionsPanel, huge JTextArea in the center of FileOptionsPanel plus four buttons up on the JTextArea, etc. Should I use new Layout type, or commands known as "repaint/revalidate," or implement changeListener?每个面板上都有已添加的组件,例如 HomeOptionsPanel 中的按钮、FileOptionsPanel 中心的巨大 JTextArea 以及 JTextArea 上的四个按钮等。我应该使用新的布局类型,还是称为“重绘/重新验证”的命令, " 或实施 changeListener? And should I only implement the code to the JFrame, or do it for each of the JPanels?我应该只对 JFrame 执行代码,还是对每个 JPanel 执行此代码?

The behaviour of your UI upon window resizing depends (also) on the Layout Manager you are using. window 调整大小时 UI 的行为(也)取决于您使用的布局管理器。

Some Layout Managers (like BorderLayout ) resize the components when the windows is resized, while others (like FlowLayout ) don't.一些布局管理器(如BorderLayout )会在 windows 调整大小时调整组件的大小,而其他布局管理器(如FlowLayout )则不会。

It is not clear what LM you are using inside your panels, but most likely your issue stands in there.目前尚不清楚您在面板中使用的是什么 LM,但很可能您的问题就在那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM