带有图像的 JCheckBox
JCheckBox with Image
我怎样才能拥有带有图像的复选框组?
我使用:
for (String testata : listaTestate) {
JCheckBox checkbox = new JCheckBox();
ImageIcon imgTestata = new ImageIcon("src/img/"+testata+".png");
checkbox.setName(testata);
checkbox.setBackground(new Color(194, 169, 221));
checkbox.setSelected(true);
testate.add(checkbox);
JLabel label = new JLabel(imgTestata);
label.setBackground(new Color(194, 169, 221));
label.setPreferredSize(new Dimension(500, 50));
testate.add(label);
}
但是 ImageIcon 和 JCheckBox 之间有很多 space。
space 发生是因为您在循环中创建的每个标签实例上调用 .setPreferredSize(new Dimension(500, 50));
,这会产生宽标签,因此将其删除,结果会很好。
在不了解布局管理器的情况下,很难做到 100%,但如果我这样做,我可能会使用 GridBagLayout
...
testate.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridy = 0;
gbc.insets = new Insets(4, 4, 4, 4);
gbc.anchor = GridBagConstraints.WEST;
for (String testata : listaTestate) {
gbc.gridx = 0;
JCheckBox checkbox = new JCheckBox();
ImageIcon imgTestata = new ImageIcon(getClass().getResource("/img/"+testata+".png"));
checkbox.setName(testata);
checkbox.setBackground(new Color(194, 169, 221));
checkbox.setSelected(true);
testate.add(checkbox, gbc);
gbc.gridx++;
JLabel label = new JLabel(imgTestata);
label.setBackground(new Color(194, 169, 221));
testate.add(label, gbc);
gbc.gridy++;
}
您可能还想通读一下:
- Retrieving Resources
- Location-Independent Access to Resources
- Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?
- Reading/Loading an Image
- Laying Out Components Within a Container
- How to Use GridBagLayout
我怎样才能拥有带有图像的复选框组?
我使用:
for (String testata : listaTestate) {
JCheckBox checkbox = new JCheckBox();
ImageIcon imgTestata = new ImageIcon("src/img/"+testata+".png");
checkbox.setName(testata);
checkbox.setBackground(new Color(194, 169, 221));
checkbox.setSelected(true);
testate.add(checkbox);
JLabel label = new JLabel(imgTestata);
label.setBackground(new Color(194, 169, 221));
label.setPreferredSize(new Dimension(500, 50));
testate.add(label);
}
但是 ImageIcon 和 JCheckBox 之间有很多 space。
space 发生是因为您在循环中创建的每个标签实例上调用 .setPreferredSize(new Dimension(500, 50));
,这会产生宽标签,因此将其删除,结果会很好。
在不了解布局管理器的情况下,很难做到 100%,但如果我这样做,我可能会使用 GridBagLayout
...
testate.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridy = 0;
gbc.insets = new Insets(4, 4, 4, 4);
gbc.anchor = GridBagConstraints.WEST;
for (String testata : listaTestate) {
gbc.gridx = 0;
JCheckBox checkbox = new JCheckBox();
ImageIcon imgTestata = new ImageIcon(getClass().getResource("/img/"+testata+".png"));
checkbox.setName(testata);
checkbox.setBackground(new Color(194, 169, 221));
checkbox.setSelected(true);
testate.add(checkbox, gbc);
gbc.gridx++;
JLabel label = new JLabel(imgTestata);
label.setBackground(new Color(194, 169, 221));
testate.add(label, gbc);
gbc.gridy++;
}
您可能还想通读一下:
- Retrieving Resources
- Location-Independent Access to Resources
- Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?
- Reading/Loading an Image
- Laying Out Components Within a Container
- How to Use GridBagLayout