public class Popup_dialog extends JDialog { private Color TITLE_BAR_COLOR = com.holub.ui.Colors.LIGHT_YELLOW; private Color CLOSE_BOX_COLOR = com.holub.ui.Colors.DARK_RED;
private JLabel title = new JLabel("xxxxxxxxxxxxxx"); { title.setHorizontalAlignment(SwingConstants.CENTER); title.setOpaque( false ); title.setFont( title.getFont().deriveFont(Font.BOLD) ); }
private JPanel header = new JPanel(); { header.setBackground( TITLE_BAR_COLOR ); header.setLayout( new BorderLayout() ); header.setBorder( BorderFactory.createEmptyBorder(2,2,2,2) ); header.add( title , BorderLayout.CENTER ); header.add( create_close_button() , BorderLayout.EAST ); }
private JPanel content_pane = new JPanel(); { content_pane.setLayout( new BorderLayout() ); }
public Popup_dialog( Frame owner ){ super(owner); setModal(true); } public Popup_dialog( Dialog owner){ super(owner); setModal(true); }
/* Code common to all constructors. */ { init_dragable();
b.setOpaque( false ); b.addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent e) { Popup_dialog.this.setVisible(false); Popup_dialog.this.dispose(); } } );
b.setFocusable( false ); return b; }
/** Set the dialog title to the indicated text. */ public void setTitle( String text ){ title.setText( text ); }
/** Add your widgets to the window returned by this method, in * a manner similar to a JFrame. Do not modify the Popup_dialog * itself. The returned container is a {@link JPanel JPanel} * with a preinstalled {@link BorderLayout}. * By default, it's colored dialog-box gray. * @return the content pane. */
public Container getContentPane(){ return content_pane; } 有点意思实现拖拉功能的代码。主要的思路是设置2个监听器。扑捉按纽的鼠标监听器与使用变量reference_position存储鼠标点击在标题标签中的上层窗口位置。 当鼠标移动时,鼠标移动监听器将被激活。问题是和拖动联系一起的事件一般和屏幕联系在一起而不是和上层窗口联系一起。拉句柄在当前鼠标位置移动对话框到原始的reference_position位置。就象从原始位置拖动窗体到现在鼠标位置一样。 下面是代码:
private Point reference_position = new Point(0,0); private MouseMotionListener movement_handler; private MouseListener click_handler;
private void init_dragable() { movement_handler = new MouseMotionAdapter() { public void mouseDragged( MouseEvent e ) { // The reference position is the (window-relative) // cursor position when the click occurred. The // current_mouse_position is mouse position // now, and the deltas represent the distance // moved.
Point current_mouse_position = e.getPoint(); Point current_window_location = getLocation();
int delta_x=current_mouse_position.x - reference_position.x; int delta_y=current_mouse_position.y - reference_position.y;
// Move the window over by the computed delta. This move // effectively shifts the window-relative, current mouse // position back to the original reference position.
public class Date_selector_dialog extends Popup_dialog implements Date_selector { private Date_selector selector = new Date_selector_panel();
/** Creates a dialog box with the indicated parent that holds * a standard {@link Date_selector_panel Date_selector_panel} * (as created using the no-arg constructor). */ public Date_selector_dialog( Frame parent ) { super(parent); selector = new Navigable_date_selector( new Date_selector_panel() ); init(); }
/* Like {@link #Date_selector_dialog(Frame), * but for a {@link Dialog} parent. */ public Date_selector_dialog( Dialog parent ) { super(parent); selector = new Navigable_date_selector( new Date_selector_panel() ); init(); }
/** Creates a dialog box with the indicated parent that holds * the indicated Date_selector. * Note that the current month and year display in the * dialog-box title bar, so there's no need to display them in * the selector too. */ public Date_selector_dialog( Frame parent, Date_selector to_wrap ) { super(parent); selector = to_wrap; init(); }
/* Like {@link #Date_selector_dialog(Frame,Date_selector), * but for a {@link Dialog} parent. */
/** Code common to all constructors. */ private void init() { getContentPane().add( (Container)selector, BorderLayout.CENTER ); selector.addActionListener ( new ActionListener() { public void actionPerformed( ActionEvent event ) { if( event.getID() == Date_selector.CHANGE_ACTION ) { setTitle( event.getActionCommand() ); } else { setVisible(false); dispose(); } } } ); ((Container)selector).setVisible(true); pack(); }
/** For use when you pop up a dialog using * setVisible(true) rather than {@link #select}. * @return the selected date or null if the dialog was closed * without selecting anything. */ public Date get_selected_date() { return selector.get_selected_date(); }
/** Get the current date. The dialog stays in existence * until the user closes it or selects a date, so this * method can be used to see what month the user has * scrolled to. * @return the date currently displayed on the calendar. */ public Date get_current_date() { return selector.get_current_date(); }
/** Add an action listener for both. * {@link Date_selector#CHANGE_ACTION} and * {@link Date_selector#SELECT_ACTION} action events. */ public void addActionListener(ActionListener l) { selector.addActionListener(l); }
/** Remove a previously added listener. */ public void removeActionListener(ActionListener l) { selector.removeActionListener(l); }
/** Pops up the chooser and blocks until the user selects * a date. * @return the selected date or null if the dialog was closed * without selecting anything. */ public Date select() { setVisible(true); return selector.get_selected_date(); }
public void roll(int f, boolean up) { selector.roll(f,up); } public int get(int f) { return selector.get(f); } }