我正在尝试用手写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);
有什么理由不能正常显示?
答案 0 :(得分:2)
问题是,您只需将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);
}