gpt4 book ai didi

java - 如何更改BorderLayout中Jpanels的宽度大小?

转载 作者:行者123 更新时间:2023-12-01 20:11:56 25 4
gpt4 key购买 nike

我有主 JPanel,它是 Borderlayout,添加了 4 个 JPANELS:北(绿)、西(红)、中心(灰)、南(蓝)。我想减小 WEST(Red) Jpanel 的宽度尺寸,或增大 Center(Grey) Jpanel 的宽度尺寸。

屏幕截图:

/image/at5GA.png

这是我的代码:

    frame = new JFrame("FreshPos baza podataka");
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

// Main paneel
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBorder( BorderFactory.createEmptyBorder(10,10,10,10) );
frame.getContentPane().add(panel);

//West panel;
JPanel panelWest = new JPanel(new GridLayout(14,0,0,2));
panelWest.setPreferredSize(new Dimension(300, 300));
panelWest.setBorder( BorderFactory.createEmptyBorder(100,0,0,0) );
panel.add(panelWest, BorderLayout.WEST);
panelWest.setBackground(Color.red);

for (int i = 0; i < MAX_TABLES; i++) {
buttonsTables[i] = new JButton(tables[i]);
buttonsTables[i].setMaximumSize(new Dimension(Integer.MAX_VALUE, buttonsTables[i].getMinimumSize().height));
panelWest.add(buttonsTables[i]);
panelWest.add(Box.createVerticalStrut(10));
}

//South panel;
JPanel southPanel = new JPanel(); // Donji layout za dugmice
southPanel.setBorder( BorderFactory.createEmptyBorder(20,0,0,0) );
panel.add(southPanel, BorderLayout.SOUTH);
southPanel.setBackground(Color.BLUE);

JButton buttonDodaj = new JButton("Dodaj");
southPanel.add(buttonDodaj);
JButton buttonIzmeni = new JButton("Izmeni");
southPanel.add(buttonIzmeni);
JButton butonObrisi = new JButton("Obrisi");
southPanel.add(butonObrisi);

//North panel;
JPanel northPanel = new JPanel(); // Donji layout za dugmice
northPanel.setBorder( BorderFactory.createEmptyBorder(0,10,0,0) );
panel.add(northPanel, BorderLayout.NORTH);
northPanel.setBackground(Color.green);

JButton buttonImport = new JButton("Importuj fajl");
buttonImport.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
importActionPerformed(evt);
}
});
northPanel.add(buttonImport, BorderLayout.WEST);

JButton ButtonRecord = new JButton("Snimi fajl");
northPanel.add(ButtonRecord, BorderLayout.WEST);

// Central panel
JPanel centerPanel = new JPanel(new BorderLayout());
centerPanel.setBackground(Color.GRAY);
panel.add(centerPanel, BorderLayout.CENTER);

最佳答案

I want to reduce width size of WEST(Red) Jpanel

panelWest.setBorder( BorderFactory.createEmptyBorder(100,0,0,0) );

那么为什么你的边框宽度这么大?

边框用于组件周围的“额外”空间。

因此面板的宽度是按钮的宽度加上边框的宽度。

编辑:

panelWest.setPreferredSize(new Dimension(300, 300));

不要硬编码首选尺寸。布局管理器将根据上述逻辑计算尺寸。删除该声明。

编辑2:

// buttonsTables[i].setMaximumSize(new Dimension(Integer.MAX_VALUE, buttonsTables[i].getMinimumSize().height));

摆脱任何试图控制组件大小的逻辑。使用布局管理器的目的是让布局管理器进行尺寸计算。

因此,对于按钮面板,您需要嵌套面板以防止按钮占用所有空间。

你可以这样做:

JPanel wrapper = new JPanel();
wrapper.add(buttonsPanel);
...
//panel.add(panelWest, BorderLayout.WEST);
panel.add(wrapper, BorderLayout.WEST);

默认情况下,JPanel 使用 FlowLayout,它将尊重添加到其中的任何组件的首选大小。

另一个选项是将 GridBagLayout 与包装面板一起使用。默认情况下,面板将显示在可用空间的“中心”。因此它将垂直居中,并且您不需要 EmptyBorder。

关于java - 如何更改BorderLayout中Jpanels的宽度大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46572625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com