[在大多数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上方的四个按钮,等等。我应该使用新的Layout类型还是称为“ repaint /重新验证”或实施changeListener?我应该只对JFrame实现代码,还是对每个JPanels进行编码?
[在大多数GUI程序中,当用户调整大小时,程序的组件(例如文本字段,按钮等)会根据用户的决定而增大或减小其大小。 ...
您的UI在调整窗口大小时的行为也取决于您使用的布局管理器。