我有一个JPanel
,其中GridBagLayout
作为布局管理器,我正在尝试获得这种安排:
忽略边框多余的深蓝色空间。
我总共有5列和3行,并且所有组件都将setPreferredSize()
设置为确切的值,以完全适合JPanel
,而GridBagLayout
的大小也是首选(170 x 115)。 / p>
问题是JPanel
似乎在最后一列宽度上增加了30像素,这是因为仅在setMinimumSize()
(总共200)的宽度上增加了30像素,这些组件才能正确显示,像这样:
但由于多余的空间而使最后一列分开。
它会增加30像素,因为在JPanel的宽度上添加29像素会得到以下结果:
根据我的经验,这表明可用空间太小,无法显示所有组件,然后使用JFrame
。
我不知道那30像素来自哪里,请问有人可以告诉我如何使组件合适吗?
下面显示了代码,当前给出了此结果:
忽略JPanel
的多余黑色空间。
您可以在第34行中更改import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
public class Test{
public static void main (String[] args){
JFrame f;
SizeProperties p;
f = new JFrame();
p = new SizeProperties();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setPreferredSize(new Dimension(250,200));
f.getContentPane().setBackground(Color.BLACK);
f.setLayout(new FlowLayout());
f.add(p);
f.pack();
f.setVisible(true);
}
}
final class SizeProperties extends JPanel{
private GridBagConstraints c;
private PropertiesLabel xL,yL,wL,hL;
private PropertiesField xF,yF,wF,hF;
private ProportionToggleButton ptb;
SizeProperties(){
setBackground(new Color(18,101,142));
setPreferredSize(new Dimension(170,115));//Change width here
setLayout(new GridBagLayout());
xL = new PropertiesLabel("x:",25,25);
xF = new PropertiesField();
yL = new PropertiesLabel("y:",25,25);
yF = new PropertiesField();
wL = new PropertiesLabel("Width:",80,25);
wF = new PropertiesField();
hL = new PropertiesLabel("Height:",80,25);
hF = new PropertiesField();
ptb = new ProportionToggleButton();
c = new GridBagConstraints(0,0,1,1,0,0,GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(10,10,10,0),0,0);
add(xL,c);
c = new GridBagConstraints(1,0,1,1,0,0,GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(10,0,10,10),0,0);
add(xF,c);
c = new GridBagConstraints(2,0,1,1,0,0,GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(10,0,10,0),0,0);
add(yL,c);
c = new GridBagConstraints(3,0,2,1,0,0,GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(10,0,10,10),0,0);
add(yF,c);
c = new GridBagConstraints(0,1,2,1,0,0,GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(0,10,10,0),0,0);
add(wL,c);
c = new GridBagConstraints(2,1,2,1,0,0,GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(0,0,10,10),0,0);
add(wF,c);
c = new GridBagConstraints(0,2,2,1,0,0,GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(0,10,10,0),0,0);
add(hL,c);
c = new GridBagConstraints(2,2,2,1,0,0,GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(0,0,10,10),0,0);
add(hF,c);
c = new GridBagConstraints(4,1,1,2,0,0,GridBagConstraints.CENTER,GridBagConstraints.NONE,new Insets(0,0,10,10),0,0);
add(ptb,c);
}
}
final class PropertiesLabel extends JLabel{
PropertiesLabel(String label,int w,int h){
setText(label);
setPreferredSize(new Dimension(w,h));
setBackground(Color.BLACK);
setOpaque(true);
setForeground(Color.WHITE);
setFont(new Font("SansSerif",Font.PLAIN,14));
setHorizontalAlignment(SwingConstants.CENTER);
setVerticalAlignment(SwingConstants.CENTER);
}
}
final class PropertiesField extends JTextField{
private int validNumber = 0;
PropertiesField(){
setPreferredSize(new Dimension(45,25));
setBackground(new Color(202,226,255));
setForeground(Color.BLACK);
setFont(new Font("SansSerif",Font.PLAIN,14));
setHorizontalAlignment(JTextField.CENTER);
setText("999");
}
}
final class ProportionToggleButton extends JToggleButton{
ProportionToggleButton(){
setPreferredSize(new Dimension(15,60));
}
}
的宽度。
CREATE TABLE articles(id INTEGER PRIMARY KEY, categories TEXT);
INSERT INTO articles VALUES(1, '123,13,43'), (2, '1,3,15'),
(3, '9,17,44,18,3'), (4, ''), (5, NULL);
WITH split(id, category, str) AS (
SELECT id, '', categories||',' FROM articles
UNION ALL SELECT id,
substr(str, 0, instr(str, ',')),
substr(str, instr(str, ',')+1)
FROM split WHERE str
) SELECT id, category FROM split WHERE category ORDER BY id;
谢谢。
答案 0 :(得分:1)
我总共有5列
不是我所看到的。您不能仅将组件分配给列。实际上,您需要在一行中包含5个组件才能创建5列的网格。
根据图片,第一行有4列,最后两行有3列。
因此,根据您的图片,您需要重新考虑设计。我看到的是:
此外,您不应设置组件的首选大小。每个组件将确定自己的首选大小。然后,GridBagLayout将根据行/列中的组件确定每个单元格的大小。