将 java 个组件放在 gridbaglayout 上方并在同一布局中并排放置
Placing java components on gridbaglayout above and side by side in same layout
我有一个 JSlider 和 JTextfield 数组,我想在下面放置一个 JTextfield,然后是一个 JSlider,向左移动并再次放置下一个 JTextfield,Jslider。
setLayout(new GridBagLayout());
JTextField[] textField = new JTextField[NUM_CHANNELS + 1];
JSlider[] sliders = makeSliders(NUM_CHANNELS + 1);
for (int i = 0; i < NUM_CHANNELS + 1; i++) {
textField[i] = new JTextField();
textField[i].setText("Channel " + (i + 1));
add(textField[i]);
add(sliders[i]);
}
通过以上,我连续得到了 JTextField - JSlider ...。
添加组件时需要添加GridBagConstraints
GridBagConstraints gbc = new GridBagConstraints();
for (int i = 0; i < NUM_CHANNELS + 1; i++) {
gbc.gridy = 0;
textField[i] = new JTextField();
textField[i].setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
textField[i].setText("Channel " + (i + 1));
add(textField[i], gbc);
gbc.gridy = 1;
add(sliders[i], gbc);
gbc.gridx++;
}
我有一个 JSlider 和 JTextfield 数组,我想在下面放置一个 JTextfield,然后是一个 JSlider,向左移动并再次放置下一个 JTextfield,Jslider。
setLayout(new GridBagLayout());
JTextField[] textField = new JTextField[NUM_CHANNELS + 1];
JSlider[] sliders = makeSliders(NUM_CHANNELS + 1);
for (int i = 0; i < NUM_CHANNELS + 1; i++) {
textField[i] = new JTextField();
textField[i].setText("Channel " + (i + 1));
add(textField[i]);
add(sliders[i]);
}
通过以上,我连续得到了 JTextField - JSlider ...。
添加组件时需要添加GridBagConstraints
GridBagConstraints gbc = new GridBagConstraints();
for (int i = 0; i < NUM_CHANNELS + 1; i++) {
gbc.gridy = 0;
textField[i] = new JTextField();
textField[i].setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
textField[i].setText("Channel " + (i + 1));
add(textField[i], gbc);
gbc.gridy = 1;
add(sliders[i], gbc);
gbc.gridx++;
}