为什么不直接创建一个包含标签和复选框的新类并将其添加到您的表格中?
import java.awt.*;
import javax.swing.*;
public class LabelWithCheckBox extends JPanel{
public LabelWithCheckBox(String text){
setLayout(new GridBagLayout());
JLabel jLabel = new JLabel(text);
JCheckBox checkBox = new JCheckBox();
add(jLabel, new GridBagConstraints(
0, 0, 1, 1, 1.0, 1.0,
GridBagConstraints.WEST, GridBagConstraints.NONE,
new Insets(0, 0, 0, 0),
0, 0
));
add(checkBox, new GridBagConstraints(
1, 0, 1, 1, 1.0, 1.0,
GridBagConstraints.WEST, GridBagConstraints.NONE,
new Insets(0, 0, 0, 0),
0, 0
));
}
public static void main(String args[]){
JFrame frame = new JFrame();
frame.add(new LabelWithCheckBox("Label text"));
frame.setVisible(true);
frame.setSize(100, 50);
}
}