我正在尝试手动编写Swing(是的,我知道是疯狂的),出于某种原因,我在这里创建此部分面板:
private JPanel createColorSliderPanel() {
JPanel colorSliderPanel = new JPanel(new GridLayout(3, 1, 0, 5));
JPanel redSliderPanel = new JPanel(new BorderLayout());
JPanel greenSliderPanel = new JPanel(new BorderLayout());
JPanel blueSliderPanel = new JPanel(new BorderLayout());
GridLayout grid = (GridLayout) colorSliderPanel.getLayout();
redSlider = new JSlider();
greenSlider = new JSlider();
blueSlider = new JSlider();
redField = new JTextField();
greenField = new JTextField();
blueField = new JTextField();
redField.setEditable(false);
greenField.setEditable(false);
blueField.setEditable(false);
JLabel redLabel = new JLabel("Red");
JLabel greenLabel = new JLabel("Green");
JLabel blueLabel = new JLabel("Blue");
redLabel.setPreferredSize(new Dimension(64, 16));
redField.setPreferredSize(new Dimension(32, 16));
redSliderPanel.add(redLabel, BorderLayout.WEST);
redSliderPanel.add(redSlider, BorderLayout.CENTER);
redSliderPanel.add(redField, BorderLayout.EAST);
greenLabel.setPreferredSize(new Dimension(64, 16));
greenField.setPreferredSize(new Dimension(32, 16));
greenSliderPanel.add(redLabel, BorderLayout.WEST);
greenSliderPanel.add(redSlider, BorderLayout.CENTER);
greenSliderPanel.add(redField, BorderLayout.EAST);
blueLabel.setPreferredSize(new Dimension(64, 16));
blueField.setPreferredSize(new Dimension(32, 16));
blueSliderPanel.add(redLabel, BorderLayout.WEST);
blueSliderPanel.add(redSlider, BorderLayout.CENTER);
blueSliderPanel.add(redField, BorderLayout.EAST);
colorSliderPanel.add(redSliderPanel);
colorSliderPanel.add(greenSliderPanel);
colorSliderPanel.add(blueSliderPanel);
return colorSliderPanel;
}
不能按预期工作:
应该将我制作的三个面板堆叠在一起。我将其添加到我的JFrame中是这样的:
JPanel sliderPanel = createColorSliderPanel();
...
contentPane.add(sliderPanel, BorderLayout.SOUTH);
任何提示为什么显示不正确?
最佳答案
问题是,您只将redSlider/Field/Label
添加到所有其他面板中
redSliderPanel.add(redLabel, BorderLayout.WEST);
redSliderPanel.add(redSlider, BorderLayout.CENTER);
redSliderPanel.add(redField, BorderLayout.EAST);
greenSliderPanel.add(redLabel, BorderLayout.WEST);
greenSliderPanel.add(redSlider, BorderLayout.CENTER);
greenSliderPanel.add(redField, BorderLayout.EAST);
blueSliderPanel.add(redLabel, BorderLayout.WEST);
blueSliderPanel.add(redSlider, BorderLayout.CENTER);
blueSliderPanel.add(redField, BorderLayout.EAST);
这样做将从先前添加的容器中删除
redSlider
,redField
和redLabel
,这意味着它只会出现在blueSliderPanel
上。不要使用
setPreferred/Minimum/MaximumSize
,您将无法控制组件用来衡量其所需大小的度量标准。为组件提供大小调整提示,以便他们可以做出更好的决策。redField = new JTextField(4);
greenField = new JTextField(4);
blueField = new JTextField(4);
然后不要先将字段添加到子面板
然后,不要先将字段添加到单独的面板中,但是,您可能会发现诸如
GridBagLayout
这样的选项是更好的选择...JPanel colorSliderPanel = new JPanel(new GridBagLayout());
redSlider = new JSlider();
greenSlider = new JSlider();
blueSlider = new JSlider();
redField = new JTextField(4);
greenField = new JTextField(4);
blueField = new JTextField(4);
redField.setEditable(false);
greenField.setEditable(false);
blueField.setEditable(false);
JLabel redLabel = new JLabel("Red");
JLabel greenLabel = new JLabel("Green");
JLabel blueLabel = new JLabel("Blue");
addTo(colorSliderPanel, redLabel, redSlider, redField, 0);
addTo(colorSliderPanel, greenLabel, greenSlider, greenField, 1);
addTo(colorSliderPanel, blueLabel, blueSlider, blueField, 2);
还有
addTo
方法...protected void addTo(JPanel parent, JLabel label, JSlider slider, JTextField field, int gridY) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.gridx = 0;
gbc.gridy = gridY;
parent.add(label, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx++;
gbc.weightx = 1;
parent.add(slider, gbc);
gbc.gridx++;
gbc.weightx = 0;
parent.add(field, gbc);
}