我有一个 [JFrame][1] 的课程,但没有显示任何内容。但是,如果我删除 setLayout 临时面板会显示,但如果我添加其他组件,它们不会显示。我现在添加了面板
public class GenerateQuestions extends JFrame {
List<ButtonGroup> btngp;
GenerateQuestions() {
setSize(400, 400);
btngp = new LinkedList<ButtonGroup>();
String[] contents =
{
"Test1", "Test2", "Test3", "Test4"
};
SpringLayout z = new SpringLayout();
setLayout(z);
JPanel temp = new JPanel();
SpringLayout sl = new SpringLayout();
temp.setLayout(sl);
JLabel x = new JLabel("This is a test question generator for online assesment ");
temp.add(x);
sl.putConstraint(SpringLayout.NORTH, x, 5, SpringLayout.NORTH, temp);
sl.putConstraint(SpringLayout.WEST, x, 5, SpringLayout.WEST, temp);
ButtonGroup btn = new ButtonGroup();
int counterNorth = 15;
int counterWest = 15;
for (int i = 0; i < contents.length; i++) {
JRadioButton t = new JRadioButton(contents[i]);
btn.add(t);
temp.add(t);
sl.putConstraint(SpringLayout.NORTH, t, counterNorth, SpringLayout.NORTH, x);
sl.putConstraint(SpringLayout.WEST, t, counterWest, SpringLayout.WEST, temp);
if (i % 2 == 0) {
counterWest += 75;
} else {
counterWest -= 75;
counterNorth += 25;
}
}
btngp.add(btn);
z.putConstraint(SpringLayout.NORTH, temp, 10, SpringLayout.NORTH, this);
z.putConstraint(SpringLayout.WEST, temp, 10, SpringLayout.WEST, this);
setVisible(true);
getContentPane().add(temp);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new GenerateQuestions();
}
}