组件

对话框组件Dialog:界面效果和Frame类似,是一个拥有边框和最大化、最小化、关闭按钮的顶级窗口,其中也可以添加其他的组件或容器,但不同之处在于对话框组件主要用于接收用户某种形式的简单输入,起到确认操作或警告/提示的作用。

Dialog组件的默认布局管理器是BorderLayout类型,且默认初始化为不可见的,需要使用setVisible(true)方法使之显示出来

Dialog组件虽然是顶级窗口,但必须依赖一个其他的窗口而不能单独存在,所依赖的窗口称为对话框的所有者(Owner),通常是Frame或其他Dialog

当其所有者窗口最小化时,Dialog也会自动隐藏为对用户不可见,当所有者窗口被还原时,Dialog重新变为可见。

通过建立Dialog的子类来建立一个对话框类,然后这个类的一个实例,即该类的一个对象,就是一个对话框。

构造方法
public Dialod(Frame f,String s);
public Dialod(Frame f,String s,boolean b);

对话框分为无模式对话框(Modeless Dialog)和有模式对话框(Modal Dialog)两种:

有模式对话框将阻断输入到其所在应用程序中其他所有窗体的内容,例如MS Word中的文件对话框。

无模式对话框显示与否都不影响所在应用程序其他窗体的操作,通常用于提供非必须的辅助性功能,如Word查找/替换对话框。

常用方法
public String getTitle();
public void setTitle();
public void setModal(Boolean model);
public void setSize(int width,int height);
public void setVisible(boolean b);

文件对话框FileDialog
文件对话框java.awt.FileDialog,继承了Dialog类,但属于有模式对话框,用于在打开和保存文件时指定文件的路径和文件名。

构造方法
public FileDialog(Frame f,String s, int mode);

常用方法
public String getDirectory();
public String getFile();

需注意,文件对话框仅仅提供了一个文件操作的界面,要真正实现对文件的操作应使用文件的输入输出流(方法loadFile()saveFile()saveFileAs()中都用到了文件的输入/输出流的操作)。

典型Swing组件

1 JFrame

继承并扩充啦java.AwtFrame类主要包括DO_NOTHING_ON_CLOSE:默认窗口关闭操作为“误操作即淡季窗口的“关闭”HIDE_ON_CLOSE:默认窗口关闭操作作为“隐藏窗口”,即点击窗口的关闭按钮时隐藏当前的窗口。DISPOEE_ON_CLOSE:默认窗口关闭操作为“销毁操作”

EXIT_ON_CLOSE:默认的为关闭窗口为退出程序

package com.hesi;

import java.awt.*;

import java.awt.event.*;

public class TestDialog extends Frameimplements ActionListener

{

Labelinfo;

DialogquitDialog;

DialogloginDialog;

TextFieldtf_name;

TextFieldtf_psw;

Buttonlogin,regist,help,exit;

publicTestDialog()

{

super("注册窗口");

login=newButton("登录");

regist=newButton("注册");

help=newButton("帮助");

exit=newButton("退出");

Panelp=new Panel();

p.setLayout(newGridLayout(1,4));

p.add(login);

p.add(regist);

p.add(help);

p.add(exit);

info=newLabel("您尚未登录");

add(p,BorderLayout.NORTH);

add(info,BorderLayout.CENTER);

login.addActionListener(this);

exit.addActionListener(this);

addWindowListener(newWindowAdapter()

{

publicvoid windowClosing(WindowEvent e)

{

quit();

}

});

loginDialog=this.createLoginDialog(this);

quitDialog=this.createQuitDialog(this);

setSize(200,150);

setLocation(450,200);

setVisible(true);

}

publicDialog createQuitDialog(Frame f)

{

Dialogd=new Dialog(f,"确认退出对话框",true);

Labelnote=new Label("您确定要退出程序吗?");

Panelp=new Panel();

p.setLayout(newGridLayout(1,2));

Buttonconfirm=new Button("确定");

Buttoncancel=new Button("取消");

confirm.setActionCommand("confirmQuit");

cancel.setActionCommand("cancelQuit");

confirm.addActionListener(this);

cancel.addActionListener(this);

p.add(confirm);

p.add(cancel);

d.setSize(200,100);

d.setLocation(400,200);

d.add(note,BorderLayout.CENTER);

d.add(p,BorderLayout.SOUTH);

returnd;

}

publicDialog createLoginDialog(Frame f)

{

Dialogd=new Dialog(f,"登录对话框",true);

Labelnote=new Label("请输入注册信息");

Panelpa=new Panel();

pa.setLayout(newGridLayout(2,1));

pa.add(newLabel("用户名"));

pa.add(newLabel("密码"));

Panelpc=new Panel();

pc.setLayout(newGridLayout(2,1));

tf_name=newTextField();

tf_psw=newTextField();

tf_psw.setEchoChar('*');

pc.add(tf_name);

pc.add(tf_psw);

Panelpb=new Panel();

pb.setLayout(newGridLayout(1,2));

Buttonsubmit=new Button("提交");

Buttoncancel=new Button("取消");

submit.setActionCommand("submitLogin");

cancel.setActionCommand("cancelLogin");

submit.addActionListener(this);

cancel.addActionListener(this);

pb.add(submit);

pb.add(cancel);

d.add(note,BorderLayout.NORTH);

d.add(pa,BorderLayout.WEST);

d.add(pc,BorderLayout.CENTER);

d.add(pb,BorderLayout.SOUTH);

d.setSize(200,110);

d.setLocation(400,200);

returnd;

}

publicvoid actionPerformed(ActionEvent e)

{

Strings=e.getActionCommand();

if(s.equals("登录"))

{

loginDialog.setVisible(true);

}

elseif(s.equals("退出"))

{

this.quit();

}

elseif(s.equals("confirmQuit"))

{

System.exit(0);

}

elseif(s.equals("cancelQuit"))

{

quitDialog.setVisible(false);

}

elseif(s.equals("submitLogin"))

{

Stringname=tf_name.getText();

Stringpassword=(tf_psw).getText();

if(name.equals("Scott")&&password.equals("1134"))

{

info.setText("欢迎您:"+name+"用户");

}else

{

info.setText("验证失败,错误的用户/密码!");

}

loginDialog.setVisible(false);

}

elseif(s.equals("cancelLogin"))

{

loginDialog.setVisible(false);

}

}

publicvoid quit()

{

quitDialog.setVisible(true);

}

publicstatic void main(String[] args)

{

newTestDialog();

}

}

你可能感兴趣的:(组件)