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.
click_handler = new MouseAdapter() { public void mousePressed( MouseEvent e ) { reference_position = e.getPoint(); // Start of the drag } };
setDragable(true); }
/** Turn dragability on or off. */ public void setDragable( boolean on ) { if( on ) { title.addMouseMotionListener ( movement_handler ); title.addMouseListener ( click_handler ); } else { title.removeMouseMotionListener ( movement_handler ); title.removeMouseListener ( click_handler ); } }
日期對話框
剩下的都是些微不足道的包裝。Date_selector_dialog 修飾者把包裝的Date_selector放在彈出框中。它從Titled_date_selector中復制了一些代碼用來顯示對話框標題欄(當前日期和月份)這里根本就沒有考慮導航條問題。因為Date_selector已經包含了導航條等等。。 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