查看: 856|回复: 2
|
Dialog Box (java) 的问题!!!
[复制链接]
|
|
我的dialog box不能close....我试用过hide()和dispose() 的方法不能
code:
//link declare as global variable
public void connect()
{
link = new Dialog(this,"Connect",true);
TextField url = new TextField("http://computing.unn.ac.uk/staff/cgpb2/web/Clients.html",40);
Button bttGo = new Button("GO");
Button cancel = new Button("CANCEL");
Label lblurl = new Label("Url :");
Panel west = new Panel(new BorderLayout());
Panel east = new Panel(new BorderLayout());
west.add("West",lblurl);
west.add("Center",url);
east.add("West",bttGo);
link.setLayout(new BorderLayout());
link.add("East",east);
link.add("West",west);
link.resize(380,60);
link.show();
bttGo.addActionListener
(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
link.hide();
}
}
);
} |
|
|
|
|
|
|
|
发表于 27-12-2005 07:05 PM
|
显示全部楼层
原帖由 momo99t 于 27-12-2005 01:27 AM 发表
我的dialog box不能close....我试用过hide()和dispose() 的方法不能
code:
//link declare as global variable
public void connect()
{
link = new Dialog(this,"Connect",true);
...
你好!不能close是因为你没有加addWindowListener。 Dialog 的default close bahavior 是不会帮你dispose你的Dialog。
你只须加以下statement就行了。
- connectDialog.addWindowListener(new WindowAdapter(){
- public void windowClosing(WindowEvent e) {
- connectDialog.dispose();
- }
- });
复制代码
还有,你的link.hide()应该改用setVisible(false),因为link.hide()已经deprecated 了.如果你要release connectDialog 的 resource的话, 你应改用connectDialog.dispose();
如果你有的选择的话,我建议你用swing, 因为awt比较旧,很basic,但不能说被弃用,因为swing 是built on top of it。
怕你不懂我说什么东东,唯有给个例子吧。
- import java.awt.BorderLayout;
- import java.awt.Button;
- import java.awt.Dialog;
- import java.awt.Dimension;
- import java.awt.Frame;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.awt.event.WindowAdapter;
- import java.awt.event.WindowEvent;
- public class MyDialog extends Frame{
- private Button openConnectDialog;
-
- public MyDialog (){
- super("MyDialog");
- initComponents();
- }
-
- private void initComponents() {
- openConnectDialog = new Button("Open Connect Dialog");
- openConnectDialog .addMouseListener(new MouseAdapter(){
- public void mouseClicked(MouseEvent e) {
- handleOpenConnectDialog();
- }
- });
-
- this.addWindowListener(new WindowAdapter(){
- public void windowClosing(WindowEvent e) {
- close();
- }
- });
- this.setLayout(new BorderLayout());
- this.add(openConnectDialog,BorderLayout.NORTH);
- this.setSize(new Dimension(500,500));
- }
- protected void handleOpenConnectDialog() {
- final Dialog connectDialog= new Dialog(this,"Connect Dialog", true);
-
- connectDialog.addWindowListener(new WindowAdapter(){
- public void windowClosing(WindowEvent e) {
- connectDialog.dispose();
- }
- });
-
- Button killMe = new Button("Kill Me") ;
- killMe.addMouseListener(new MouseAdapter(){
- public void mouseClicked(MouseEvent e) {
- connectDialog.dispose();
- }
- });
- connectDialog.setLayout(new BorderLayout());
- connectDialog.add(killMe,BorderLayout.NORTH);
- connectDialog.setSize(new Dimension(200,300));
-
- connectDialog.setVisible(true);
- }
-
- protected void close() {
- this.dispose();
- }
- public static void main(String[] args) {
- MyDialog dialog = new MyDialog();
-
- dialog.setVisible(true);
- }
- }
复制代码
有点乱,因为是偷偷用办公时间写,但帮你一点,是吧? |
|
|
|
|
|
|
|
楼主 |
发表于 29-12-2005 02:25 AM
|
显示全部楼层
|
|
|
|
|
|
| |
本周最热论坛帖子
|