Java 调整框架大小后如何调整网格大小?
我想知道以下几点:我有一个MainWindow组件(它包含一个框架(JFrame))和几个其他JPanel。其中一个JPanel,假设gridPanel使用gridLayout作为LayoutManager。现在我的问题是,我想在调整窗口大小后进行调整(设置行的大小/设置列的大小)。有人能告诉我如何在调整帧大小后实现可以触发的操作,因为我不熟悉所涉及的听众 这应该是在最“标准”的编码实践中完成的。感谢您的回复和回答 如果您希望网格“填充”容器,或者填充JFrame,那么关键是使用适当的布局管理器来使用容器保存GridLayout。例如,如果将GridLayout using容器添加到另一个使用FlowLayout的容器中,则如果GridLayout using容器的容纳容器的大小发生变化,则该容器的大小不会发生变化。但是,如果将使用容器的GridLayout添加到使用BorderLayout的另一个容器及其BorderLayout.CENTER位置,则使用容器的GridLayout将随着使用父容器的BorderLayout的大小调整而调整大小 例如:Java 调整框架大小后如何调整网格大小?,java,swing,resize,frame,grid-layout,Java,Swing,Resize,Frame,Grid Layout,我想知道以下几点:我有一个MainWindow组件(它包含一个框架(JFrame))和几个其他JPanel。其中一个JPanel,假设gridPanel使用gridLayout作为LayoutManager。现在我的问题是,我想在调整窗口大小后进行调整(设置行的大小/设置列的大小)。有人能告诉我如何在调整帧大小后实现可以触发的操作,因为我不熟悉所涉及的听众 这应该是在最“标准”的编码实践中完成的。感谢您的回复和回答 如果您希望网格“填充”容器,或者填充JFrame,那么关键是使用适当的布局管理器
import java.awt.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class ExpandingGrid extends JPanel {
private static final int GAP = 5;
public ExpandingGrid() {
// create a BorderLayout-using JPanel
JPanel borderLayoutPanel = new JPanel(new BorderLayout());
borderLayoutPanel.setBorder(BorderFactory.createTitledBorder("BorderLayout Panel"));
borderLayoutPanel.add(createGridPanel(), BorderLayout.CENTER); // add a Grid to it
// create a FlowLayout-using JPanel
JPanel flowLayoutPanel = new JPanel(new FlowLayout());
flowLayoutPanel.setBorder(BorderFactory.createTitledBorder("FlowLayout Panel"));
flowLayoutPanel.add(createGridPanel()); // add a grid to it
// set up the main JPanel
setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
setLayout(new GridLayout(1, 0, GAP, 0)); // grid with 1 row
// and add the borderlayout and flowlayout using JPanels to it
add(borderLayoutPanel);
add(flowLayoutPanel);
}
// create a JPanel that holds a bunch of JLabels in a GridLayout
private JPanel createGridPanel() {
int rows = 5;
int cols = 5;
JPanel gridPanel = new JPanel(new GridLayout(rows, cols));
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
// create the JLabel that simply shows the row and column number
JLabel label = new JLabel(String.format("[%d, %d]", i, j),
SwingConstants.CENTER);
// give the JLabel a border
label.setBorder(BorderFactory.createEtchedBorder());
gridPanel.add(label); // add to the GridLayout using JPanel
}
}
return gridPanel;
}
private static void createAndShowUI() {
JFrame frame = new JFrame("ExpandingGrid");
frame.getContentPane().add(new ExpandingGrid());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
import java.awt.*;
导入javax.swing.*;
@抑制警告(“串行”)
公共类扩展网格扩展JPanel{
专用静态最终内部间隙=5;
公共扩展网格(){
//使用JPanel创建边框布局
JPanel borderLayoutPanel=newjpanel(newborderlayout());
borderLayoutPanel.setOrder(BorderFactory.CreateTiledBorder(“borderLayoutPanel”);
borderLayoutPanel.add(createGridPanel(),BorderLayout.CENTER);//向其中添加网格
//使用JPanel创建FlowLayout
JPanel flowLayoutPanel=新的JPanel(新的FlowLayout());
flowLayoutPanel.setBorder(BorderFactory.CreateTiledBorder(“flowLayoutPanel”);
flowLayoutPanel.add(createGridPanel());//向其中添加网格
//设置主JPanel
setBorder(BorderFactory.createEmptyByOrder(间隙,间隙,间隙));
setLayout(新的GridLayout(1,0,间隙,0));//带1行的网格
//并使用JPanels向其中添加borderlayout和flowlayout
添加(边框布局面板);
添加(flowLayoutPanel);
}
//创建一个JPanel,在GridLayout中保存一组JLabel
私有JPanel createGridPanel(){
int行=5;
int cols=5;
JPanel gridPanel=newjpanel(新网格布局(行、列));
对于(int i=0;i
此外,如果这没有帮助,那么您可能希望详细说明您的问题和邮政编码,最好是一个
这就是为什么我只想调整列
也许这正是您所需要的。我了解这一部分,但我需要以下内容:我想要一个简单的add命令,它可以将我的按钮放置在网格中。但是,当我的框架变大时,我需要更多的列,否则组件之间的空间会增加。这就是为什么我只想调整列。我还没有找到其他合适的经理,你可以推荐一个吗?(布局管理器的简单性是必需的,因为我不想有过多的技巧(比如使用网格袋布局)或其他东西;但是,如果网格袋布局足够(没有太多麻烦,我会尝试)@Anonymous:我建议您创建一个类似于我上面创建的,以更好地说明您的问题。@Anonymous:我的解决方案是针对固定数量的列和行(这是我认为您最初的帖子所要求的)。另外,请告诉我们您想用这个GUI做什么,为什么要拥有这些属性,因为也许有比您正在尝试的更好的解决方法,例如使用JTable。