我有以下例子。我想要的是面板的内容与面板的顶部对齐,但它在面板的中心对齐。我认为使用GridBagLayout作为管理器并将锚设置为NORTH应该可以做到这一点,显然它没有。
试图澄清几点:
另外,如果框架的高度太小,textArea突然只调整到一行,我希望逐渐减小它的大小。
<DatePicker x:Name="DatePicker" HorizontalAlignment="Left" Margin="363,12,0,0" VerticalAlignment="Top" Width="164" SelectedDate="{x:Static sys:DateTime.Now}" FirstDayOfWeek="Monday" Text="4/27/2016">
<DatePicker.Resources>
<Style TargetType="{x:Type DatePickerTextBox}">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<TextBox x:Name="PART_TextBox"
Text="{Binding Path=SelectedDate, RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}, StringFormat={}{0:yyy-MM-dd}}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DatePicker.Resources>
</DatePicker>
答案 0 :(得分:2)
我有类似的方法,包括大小限制
public LayoutTest()
{
setSize(50, 200);
setLocation(MouseInfo.getPointerInfo().getLocation());
final JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JButton okButton = new JButton("Save");
JPanel wrapper = new JPanel();
wrapper.add(okButton);
JTextArea textArea = new JTextArea(5,1);
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
textArea.setEditable(true);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.getVerticalScrollBar().setUnitIncrement(10);
scrollPane.setMaximumSize(new Dimension(Short.MAX_VALUE, 150));
panel.add(scrollPane);
panel.add(wrapper);
panel.add(Box.createVerticalGlue());
panel.setBorder(BorderFactory.createLineBorder(Color.red));
add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
答案 1 :(得分:2)
基本上就像......
gbc.anchor = GridBagConstraints.NORTH;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
应该
有关详细信息,请查看How to Use GridBagLayout