[英]Changing size of the components of JPanel depending on the size of the JFrame
在大多數 GUI 程序中,當用戶調整它的大小時,程序的組件(例如文本字段、按鈕等)往往會根據用戶的決定增加或減小它們的大小。 我正在嘗試將這個想法實現到我的 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 );
}
}
每個面板上都有已添加的組件,例如 HomeOptionsPanel 中的按鈕、FileOptionsPanel 中心的巨大 JTextArea 以及 JTextArea 上的四個按鈕等。我應該使用新的布局類型,還是稱為“重繪/重新驗證”的命令, " 或實施 changeListener? 我應該只對 JFrame 執行代碼,還是對每個 JPanel 執行此代碼?
window 調整大小時 UI 的行為(也)取決於您使用的布局管理器。
一些布局管理器(如BorderLayout
)會在 windows 調整大小時調整組件的大小,而其他布局管理器(如FlowLayout
)則不會。
目前尚不清楚您在面板中使用的是什么 LM,但很可能您的問題就在那里。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.