弹出对话框
对话框包装器有2个类组成:第一个是popup_dialog,继承与jdialog实现小的如前所示的框架。
生成常规对话框的主要难点是所有的装饰(框架与标题栏)的消失问题,因此如果你需要生成自己的标题栏,下面提供了具体实现的大部分代码:
标题栏是包含一个显示标题的标签和一个按钮(关闭按钮,如导航箭头一样加载图象资源)。
代码如下:
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();
setundecorated( true );
jpanel contents = new jpanel();
contents.setborder( borderfactory.createlineborder(color.black,1) );
contents.setlayout(new borderlayout());
contents.add(header, borderlayout.north);
contents.add(content_pane, borderlayout.center);
contents.setbackground( color.white );
setcontentpane( contents ); // , borderlayout.center );
setlocation(100,100);
}
private jbutton create_close_button()
{
url image = getclass().getclassloader().getresource(
"images/8px.red.x.gif");
jbutton b = (image!=null) ? new jbutton( new imageicon(image) )
: new jbutton( " x " )
border outer = borderfactory.createlineborder(close_box_color,1);
border inner = borderfactory.createemptyborder(2,2,2,2);
b.setborder( borderfactory.createcompoundborder(outer,inner) );
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.
current_window_location.translate(delta_x, delta_y);
setlocation(current_window_location);
}
};
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
闽公网安备 35060202000074号