java在线聊天项目 swt可视化窗口Design 重新设计好友列表窗口 增加菜单栏

近年来的类型,查询时只有需要年和月,不欲日,因此待对旧的DatePicker进行修改,查询了网上的情节,最终由同首帖子里看到了增长附加属性的主意,地址是http://stackoverflow.com/questions/1798513/wpf-toolkit-datepicker-month-year-only

增的菜谱栏作用图如下:

原文是为此了少单近乎,其中一个凡为着让DatePicker下的Calendar只展示时间,不出示日,另一个类似是为让DatePicker格式化为yyyy-MM格式,但是于文章被得以看来,有人提出了,用格式化类进行格式化时,DatePicker控件会闪动一下,当然不影响下。如果不思量使文章中关系的类似进行格式化,也堪为此自家及一样篇文章被的道进行格式化,不会见产出闪动的状。

图片 1

单独展示时间之类(2016-12-20改动,可以就显示年)

eclipse 中调整及 swt的design视图下

图片 2图片 3

控件区域选择Menu Controls

public class DatePickerCalendar
    {

        public static bool GetIsYear(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsYearProperty);
        }

        public static void SetIsYear(DependencyObject obj, bool value)
        {
            obj.SetValue(IsYearProperty, value);
        }

        // Using a DependencyProperty as the backing store for IsYear.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsYearProperty =
            DependencyProperty.RegisterAttached("IsYear", typeof(bool), typeof(DatePickerCalendar), new PropertyMetadata(false, new PropertyChangedCallback(OnIsMonthYearChanged)));



        public static readonly DependencyProperty IsMonthYearProperty =
        DependencyProperty.RegisterAttached("IsMonthYear", typeof(bool), typeof(DatePickerCalendar),
                                            new PropertyMetadata(OnIsMonthYearChanged));

        public static bool GetIsMonthYear(DependencyObject dobj)
        {
            return (bool)dobj.GetValue(IsMonthYearProperty);
        }

        public static void SetIsMonthYear(DependencyObject dobj, bool value)
        {
            dobj.SetValue(IsMonthYearProperty, value);
        }

        private static void OnIsMonthYearChanged(DependencyObject dobj, DependencyPropertyChangedEventArgs e)
        {
            var datePicker = (DatePicker)dobj;

            Application.Current.Dispatcher
                .BeginInvoke(DispatcherPriority.Loaded,
                             new Action<DatePicker, DependencyPropertyChangedEventArgs>(SetCalendarEventHandlers),
                             datePicker, e);
        }

        private static void SetCalendarEventHandlers(DatePicker datePicker, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue == e.OldValue)
                return;

            if ((bool)e.NewValue)
            {
                datePicker.CalendarOpened += DatePickerOnCalendarOpened;
                datePicker.CalendarClosed += DatePickerOnCalendarClosed;
            }
            else
            {
                datePicker.CalendarOpened -= DatePickerOnCalendarOpened;
                datePicker.CalendarClosed -= DatePickerOnCalendarClosed;
            }
        }

        private static void DatePickerOnCalendarOpened(object sender, RoutedEventArgs routedEventArgs)
        {
            var calendar = GetDatePickerCalendar(sender);
            if (GetIsYear(sender as DatePicker))
            {
                calendar.DisplayMode = CalendarMode.Decade;
            }
            else
            {
                calendar.DisplayMode = CalendarMode.Year;
            }

            calendar.DisplayModeChanged += CalendarOnDisplayModeChanged;
        }

        private static void DatePickerOnCalendarClosed(object sender, RoutedEventArgs routedEventArgs)
        {
            var datePicker = (DatePicker)sender;
            var calendar = GetDatePickerCalendar(sender);
            datePicker.SelectedDate = calendar.SelectedDate;

            calendar.DisplayModeChanged -= CalendarOnDisplayModeChanged;
        }

        private static void CalendarOnDisplayModeChanged(object sender, CalendarModeChangedEventArgs e)
        {
            var calendar = (Calendar)sender;
            var datePicker = GetCalendarsDatePicker(calendar);

            bool mode = (GetIsYear(datePicker) && calendar.DisplayMode != CalendarMode.Year) || (GetIsMonthYear(datePicker) && calendar.DisplayMode != CalendarMode.Month);

            if (mode)
                return;

            calendar.SelectedDate = GetSelectedCalendarDate(calendar.DisplayDate);

            datePicker.IsDropDownOpen = false;
        }

        private static Calendar GetDatePickerCalendar(object sender)
        {
            var datePicker = (DatePicker)sender;
            var popup = (Popup)datePicker.Template.FindName("PART_Popup", datePicker);
            return ((Calendar)popup.Child);
        }

        private static DatePicker GetCalendarsDatePicker(FrameworkElement child)
        {
            var parent = (FrameworkElement)child.Parent;
            if (parent.Name == "PART_Root")
                return (DatePicker)parent.TemplatedParent;
            return GetCalendarsDatePicker(parent);
        }

        private static DateTime? GetSelectedCalendarDate(DateTime? selectedDate)
        {
            if (!selectedDate.HasValue)
                return null;
            return new DateTime(selectedDate.Value.Year, selectedDate.Value.Month, 1);
        }
    }

用Menu Bar拖动到窗口标题栏

DatePickerCalendar

将Cascaded Menu拖动到Menu Bar

调用方式

梯次将大半个Menu Item加入到New SubMenu

<DatePicker Width="200" Height="30" local:DatePickerCalendar.IsMonthYear="True"/>

Separator是分隔符

来得力量

为得再放入Cascaded Menu级联菜单

图片 4图片 5图片 6


由图上就会观看左右请勿均等,左侧的凡补加附加属性之,点击后直接显示月,后侧没因此之则显得到日,虽然都格式化为了yyyy-MM,但是Calendar如果显示到日的讲话,用户体验不好。

 

========================================================================================================================

知音列表窗口的代码如下:

2015年8月4日 记:

package com.swift.frame;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class FriendsDialog extends JDialog {

    public static void main(String args[]) {

        JFrame.setDefaultLookAndFeelDecorated(true);
        JDialog.setDefaultLookAndFeelDecorated(true);

        try {
            UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        } catch (InstantiationException e1) {
            e1.printStackTrace();
        } catch (IllegalAccessException e1) {
            e1.printStackTrace();
        } catch (UnsupportedLookAndFeelException e1) {
            e1.printStackTrace();
        }

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    FriendsDialog dialog = new FriendsDialog();
                    dialog.addWindowListener(new WindowAdapter() {
                        public void windowClosing(WindowEvent e) {
                            System.exit(0);
                        }
                    });
                    dialog.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public FriendsDialog() {
        super();
        setTitle("飞燕聊天");
        setBounds(100, 100, 247, 581);

        final JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        getContentPane().add(panel, BorderLayout.NORTH);

        final JLabel label = new JLabel(new ImageIcon("Images/logo.jpg"));
        label.setText("New JLabel");
        panel.add(label, BorderLayout.WEST);
        label.setPreferredSize(new Dimension(74,74));

        final JPanel panel_1 = new JPanel();
        panel_1.setLayout(new BorderLayout());
        panel.add(panel_1, BorderLayout.CENTER);

        final JLabel advancingSwiftLabel = new JLabel();
        advancingSwiftLabel.setText("Advancing Swift");
        panel_1.add(advancingSwiftLabel, BorderLayout.CENTER);

        final JLabel neverWasterLabel = new JLabel();
        neverWasterLabel.setText("Never waste time any more");
        panel_1.add(neverWasterLabel, BorderLayout.SOUTH);

        final JPanel panel_2 = new JPanel();
        panel_2.setLayout(new BorderLayout());
        getContentPane().add(panel_2, BorderLayout.SOUTH);

        final JPanel panel_3 = new JPanel();
        final FlowLayout flowLayout = new FlowLayout();
        flowLayout.setAlignment(FlowLayout.LEFT);
        panel_3.setLayout(flowLayout);
        panel_2.add(panel_3);

        final JButton button = new JButton();
        panel_3.add(button);
        button.setHorizontalTextPosition(SwingConstants.LEFT);
        button.setHorizontalAlignment(SwingConstants.LEFT);
        button.setText("设置");

        final JButton button_1 = new JButton();
        panel_3.add(button_1);
        button_1.setText("查找");

        final JPanel panel_4 = new JPanel();
        panel_2.add(panel_4, BorderLayout.EAST);

        final JButton button_2 = new JButton();
        panel_4.add(button_2);
        button_2.setText("退出");

        final JTabbedPane tabbedPane = new JTabbedPane();
        getContentPane().add(tabbedPane, BorderLayout.CENTER);

        final JPanel panel_5 = new JPanel();
        tabbedPane.addTab("好友列表", null, panel_5, null);

        final JPanel panel_6 = new JPanel();
        tabbedPane.addTab("群聊", null, panel_6, null);

        final JPanel panel_7 = new JPanel();
        tabbedPane.addTab("聊天记录", null, panel_7, null);

        final JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        final JMenu menu = new JMenu();
        menu.setText("操作");
        menuBar.add(menu);

        final JMenuItem newItemMenuItem = new JMenuItem();
        newItemMenuItem.setText("设置");
        menu.add(newItemMenuItem);

        final JMenuItem newItemMenuItem_1 = new JMenuItem();
        newItemMenuItem_1.setText("空间");
        menu.add(newItemMenuItem_1);

        final JMenuItem newItemMenuItem_2 = new JMenuItem();
        newItemMenuItem_2.setText("邮箱");
        menu.add(newItemMenuItem_2);

        final JMenu menu_1 = new JMenu();
        menu_1.setText("会员");
        menu.add(menu_1);

        final JMenuItem newItemMenuItem_3 = new JMenuItem();
        newItemMenuItem_3.setText("会员官网");
        menu_1.add(newItemMenuItem_3);

        final JMenuItem newItemMenuItem_4 = new JMenuItem();
        newItemMenuItem_4.setText("会员专区");
        menu_1.add(newItemMenuItem_4);

        menu.addSeparator();

        final JMenu menu_2 = new JMenu();
        menu_2.setText("安全");
        menu.add(menu_2);

        final JMenuItem newItemMenuItem_5 = new JMenuItem();
        newItemMenuItem_5.setText("紧急冻结");
        menu_2.add(newItemMenuItem_5);

        final JMenuItem newItemMenuItem_6 = new JMenuItem();
        newItemMenuItem_6.setText("密码保护");
        menu_2.add(newItemMenuItem_6);

        final JMenuItem newItemMenuItem_7 = new JMenuItem();
        newItemMenuItem_7.setText("退出");
        menu.add(newItemMenuItem_7);
        final FlowLayout flowLayout_1 = new FlowLayout();
        flowLayout_1.setAlignment(FlowLayout.RIGHT);
    }

    /**
     * WindowBuilder generated method.<br>
     * Please don't remove this method or its invocations.<br>
     * It used by WindowBuilder to associate the {@link javax.swing.JPopupMenu} with parent.
     */
    private static void addPopup(Component component, final JPopupMenu popup) {
        component.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                if (e.isPopupTrigger())
                    showMenu(e);
            }
            public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger())
                    showMenu(e);
            }
            private void showMenu(MouseEvent e) {
                popup.show(e.getComponent(), e.getX(), e.getY());
            }
        });
    }
}

今在列蒙发觉一个题材,就是行使自上平等篇帖子中之老三句话进行DatePicker时间的格式化显示,但是由用底凡Thread,所以是当线程里进行格式化的,因此,影响至了同事开的另外模块,因为大部分还是格式化为yyyy-MM-dd,但是由自身之一个页面是格式化为了yyyy-MM,而里边的一个同事并从未于外的构造函数里下三词格式化他的DatePicker,从而导致他其余的辰测算起问题,因此当承保原有效力不换的情状下,需要显示yyyy-MM,就需用到本帖子中之另一个接近。

 

DatePickerDateFormat

图片 7图片 8

public class DatePickerDateFormat
    {
        public static readonly DependencyProperty DateFormatProperty =
DependencyProperty.RegisterAttached("DateFormat", typeof(string), typeof(DatePickerDateFormat),
                                    new PropertyMetadata(OnDateFormatChanged));

        public static string GetDateFormat(DependencyObject dobj)
        {
            return (string)dobj.GetValue(DateFormatProperty);
        }

        public static void SetDateFormat(DependencyObject dobj, string value)
        {
            dobj.SetValue(DateFormatProperty, value);
        }

        private static void OnDateFormatChanged(DependencyObject dobj, DependencyPropertyChangedEventArgs e)
        {
            var datePicker = (DatePicker)dobj;

            Application.Current.Dispatcher.BeginInvoke(
                DispatcherPriority.Loaded, new Action<DatePicker>(ApplyDateFormat), datePicker);
        }

        private static void ApplyDateFormat(DatePicker datePicker)
        {
            var binding = new Binding("SelectedDate")
            {
                RelativeSource = new RelativeSource { AncestorType = typeof(DatePicker) },
                Converter = new DatePickerDateTimeConverter(),
                ConverterParameter = new Tuple<DatePicker, string>(datePicker, GetDateFormat(datePicker))
            };
            var textBox = GetTemplateTextBox(datePicker);
            textBox.SetBinding(TextBox.TextProperty, binding);

            textBox.PreviewKeyDown -= TextBoxOnPreviewKeyDown;
            textBox.PreviewKeyDown += TextBoxOnPreviewKeyDown;

            datePicker.CalendarOpened -= DatePickerOnCalendarOpened;
            datePicker.CalendarOpened += DatePickerOnCalendarOpened;
        }

        private static TextBox GetTemplateTextBox(Control control)
        {
            control.ApplyTemplate();
            return (TextBox)control.Template.FindName("PART_TextBox", control);
        }

        private static void TextBoxOnPreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key != Key.Return)
                return;

            /* DatePicker subscribes to its TextBox's KeyDown event to set its SelectedDate if Key.Return was
             * pressed. When this happens its text will be the result of its internal date parsing until it
             * loses focus or another date is selected. A workaround is to stop the KeyDown event bubbling up
             * and handling setting the DatePicker.SelectedDate. */

            e.Handled = true;

            var textBox = (TextBox)sender;
            var datePicker = (DatePicker)textBox.TemplatedParent;
            var dateStr = textBox.Text;
            var formatStr = GetDateFormat(datePicker);
            datePicker.SelectedDate = DatePickerDateTimeConverter.StringToDateTime(datePicker, formatStr, dateStr);
        }

        private static void DatePickerOnCalendarOpened(object sender, RoutedEventArgs e)
        {
            /* When DatePicker's TextBox is not focused and its Calendar is opened by clicking its calendar button
             * its text will be the result of its internal date parsing until its TextBox is focused and another
             * date is selected. A workaround is to set this string when it is opened. */

            var datePicker = (DatePicker)sender;
            var textBox = GetTemplateTextBox(datePicker);
            var formatStr = GetDateFormat(datePicker);
            textBox.Text = DatePickerDateTimeConverter.DateTimeToString(formatStr, datePicker.SelectedDate);
        }

        private class DatePickerDateTimeConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                var formatStr = ((Tuple<DatePicker, string>)parameter).Item2;
                var selectedDate = (DateTime?)value;
                return DateTimeToString(formatStr, selectedDate);
            }

            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                var tupleParam = ((Tuple<DatePicker, string>)parameter);
                var dateStr = (string)value;
                return StringToDateTime(tupleParam.Item1, tupleParam.Item2, dateStr);
            }

            public static string DateTimeToString(string formatStr, DateTime? selectedDate)
            {
                return selectedDate.HasValue ? selectedDate.Value.ToString(formatStr) : null;
            }

            public static DateTime? StringToDateTime(DatePicker datePicker, string formatStr, string dateStr)
            {
                DateTime date;
                var canParse = DateTime.TryParseExact(dateStr, formatStr, CultureInfo.CurrentCulture,
                                                      DateTimeStyles.None, out date);

                if (!canParse)
                    canParse = DateTime.TryParse(dateStr, CultureInfo.CurrentCulture, DateTimeStyles.None, out date);

                return canParse ? date : datePicker.SelectedDate;
            }
        }
    }

DatePickerDateFormat

毕竟的调用方法 

<DatePicker conver:DatePickerCalendar.IsMonthYear="True" conver:DatePickerDateFormat.DateFormat="yyyy-MM" />

 ========================================================================================================================

2016年12月20日

修改原来的代码,增加IsYear附加属性,用法及IsMonthYear一样,设置也True则仅仅显示年份