弹出对话框
对话框包装器有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.
*/
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 );
}
}
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.
*/
public date_selector_dialog( dialog parent, date_selector to_wrap )
{ super(parent);
selector = to_wrap;
init();
}
/** 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); }
}
ealy ,java 爱好者,matrix jsp翻译小组成员,可以点击http://www.matrix.org.cn/user_view.asp?username=ealy查看她的个人信息
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器