Встроенный WrapPanel
не позволит вам выровнять его содержимое - только себя. Вот методика, которая позволяет установить HorizontalContentAlignment
:
using System;
using System.Windows;
using System.Windows.Controls;
public class AlignableWrapPanel : Panel
{
public HorizontalAlignment HorizontalContentAlignment
{
get { return (HorizontalAlignment)GetValue(HorizontalContentAlignmentProperty); }
set { SetValue(HorizontalContentAlignmentProperty, value); }
}
public static readonly DependencyProperty HorizontalContentAlignmentProperty =
DependencyProperty.Register("HorizontalContentAlignment", typeof(HorizontalAlignment), typeof(AlignableWrapPanel), new FrameworkPropertyMetadata(HorizontalAlignment.Left, FrameworkPropertyMetadataOptions.AffectsArrange));
protected override Size MeasureOverride(Size constraint)
{
Size curLineSize = new Size();
Size panelSize = new Size();
UIElementCollection children = base.InternalChildren;
for (int i = 0; i < children.Count; i++)
{
UIElement child = children[i] as UIElement;
// Flow passes its own constraint to children
child.Measure(constraint);
Size sz = child.DesiredSize;
if (curLineSize.Width + sz.Width > constraint.Width) //need to switch to another line
{
panelSize.Width = Math.Max(curLineSize.Width, panelSize.Width);
panelSize.Height += curLineSize.Height;
curLineSize = sz;
if (sz.Width > constraint.Width) // if the element is wider then the constraint - give it a separate line
{
panelSize.Width = Math.Max(sz.Width, panelSize.Width);
panelSize.Height += sz.Height;
curLineSize = new Size();
}
}
else //continue to accumulate a line
{
curLineSize.Width += sz.Width;
curLineSize.Height = Math.Max(sz.Height, curLineSize.Height);
}
}
// the last line size, if any need to be added
panelSize.Width = Math.Max(curLineSize.Width, panelSize.Width);
panelSize.Height += curLineSize.Height;
return panelSize;
}
protected override Size ArrangeOverride(Size arrangeBounds)
{
int firstInLine = 0;
Size curLineSize = new Size();
double accumulatedHeight = 0;
UIElementCollection children = this.InternalChildren;
for (int i = 0; i < children.Count; i++)
{
Size sz = children[i].DesiredSize;
if (curLineSize.Width + sz.Width > arrangeBounds.Width) //need to switch to another line
{
ArrangeLine(accumulatedHeight, curLineSize, arrangeBounds.Width, firstInLine, i);
accumulatedHeight += curLineSize.Height;
curLineSize = sz;
if (sz.Width > arrangeBounds.Width) //the element is wider then the constraint - give it a separate line
{
ArrangeLine(accumulatedHeight, sz, arrangeBounds.Width, i, ++i);
accumulatedHeight += sz.Height;
curLineSize = new Size();
}
firstInLine = i;
}
else //continue to accumulate a line
{
curLineSize.Width += sz.Width;
curLineSize.Height = Math.Max(sz.Height, curLineSize.Height);
}
}
if (firstInLine < children.Count)
ArrangeLine(accumulatedHeight, curLineSize, arrangeBounds.Width, firstInLine, children.Count);
return arrangeBounds;
}
private void ArrangeLine(double y, Size lineSize, double boundsWidth, int start, int end)
{
double x = 0;
if (this.HorizontalContentAlignment == HorizontalAlignment.Center)
{
x = (boundsWidth - lineSize.Width) / 2;
}
else if (this.HorizontalContentAlignment == HorizontalAlignment.Right)
{
x = (boundsWidth - lineSize.Width);
}
UIElementCollection children = InternalChildren;
for (int i = start; i < end; i++)
{
UIElement child = children[i];
child.Arrange(new Rect(x, y, child.DesiredSize.Width, lineSize.Height));
x += child.DesiredSize.Width;
}
}
}
Вы можете использовать java.io.Console
, чтобы представить пользователю интерактивную командную строку. Вы можете сделать все это с помощью System.in
самостоятельно, но вам придется реализовать такие вещи, как отслеживание времени ввода или readPassword
и т. Д.
Потому что это код, который уже написан для вас ... нет необходимости изобретать колесо. Скорее всего, вы не добьетесь лучшего, чем оно есть.
См. java.io.Console наконец-то здесь!
Одна из самых популярных функций запросов на J2SE в последнее время была просьба улучшить консоль поддержать и предоставить возможность войти пароли с отключенным эхом. Разработчики знают эту функцию 4050435 поскольку он прятался в топ-25 Список RFE на некоторое время.
java.io.Console работает только при запуске программа на Java из командной строки без перенаправления STDIN / STDOUT.
Основное преимущество, которое я вижу с помощью Console над System.in, заключается в том, что у вас есть метод readPassword (), который не будет отображать символы, введенные пользователем (я не удалось найти способ сделать это с помощью System.in).
У вас также есть readLine (), которая выдаст приглашение и прочитает одну строку. Вам не нужно создавать свой собственный LineNumberReader.
Но если вы хотите, чтобы ваша Java-программа могла читать из STDIN, когда она перенаправляется из файла или канала, вам все равно придется использовать System.in.
Еще один трюк, который, я уверен, вы не получите с Консолью - я создал свои собственные потоки ввода и вывода и заменил ими System.in/out. Моя реализация потока, добавленного к файлу журнала, а также отображаемого на экране.
Когда я включил "Debug Info" моего бедняги, я мог даже сказать мне, из какой программы / строки пришел sysout ( Однако он был медленным. Он создал исключение и проверил соответствующую запись в стеке, поэтому по умолчанию он был отключен)