这是一个神秘的问题。我有一个JSlider
,JLabel
和JTextField
. 我希望它们都相邻对齐。我的问题是JTextField
's (valueField) 上移了。当我打印出组件之间的 y 坐标值时,它们都是相同的。这是我的代码JLabel
和JTextField
:
public SliderLabelPanel(String labelStr, String unitLabelStr)
//PRE: labelStr is initialized
//POST: a default SliderLabelPanel is created
{
//this.setLayout(new FlowLayout());
slider = new JSlider();
slider.setMajorTickSpacing(10);
slider.setMaximum(100);
slider.setMinimum(0);
slider.setPaintTicks(true);
label = new JLabel(labelStr);
valueField = new JTextField();
unitLabel = new JLabel(unitLabelStr);
this.add(slider);
this.add(label);
this.add(valueField);
this.add(unitLabel);
//printFields();
}
public void update()
//POST: this SliderLabelPanel is updated (size of the components)
{
this.slider.setBounds(0, slider.getY(), (int)(Global.sliderWidth * getWidth()), (int)(Global.labelHeight * getHeight()));
this.label.setBounds(slider.getX() + slider.getWidth(), slider.getY(),
Global.LABEL_WIDTH, (int)(Global.labelHeight * getHeight()));
this.valueField.setBounds(label.getX() + label.getWidth(), slider.getY(),
(int)(Global.fieldWidth * getWidth()), Global.FIELD_HEIGHT);
//valueField.getx
this.unitLabel.setBounds(valueField.getX() + valueField.getWidth(), slider.getY(),
Global.LABEL_WIDTH, (int)(Global.labelHeight * getHeight()));
printFields();
}
public void paintComponent(Graphics g)
//PRE: g is initialized
//POST: the components of this SliderLabelPanel are painted to g
{
super.paintComponent(g);
}
将JSlider
与 对齐JLabel
,但是JTextField
将向上移动。也请给我建议,我的想法不多了。感谢所有的帮助!