Java图形化界面编程-3-AWT常用组件

Checkbox married = new Checkbox("是否已婚?", false);



//定义一个下拉选择框

Choice colorChooser = new Choice();



//定义一个列表选择框

List colorList = new List(6, true);



//定义一个5行,20列的多行文本域

TextArea ta = new TextArea(5, 20);



//定义一个50列的单行文本域

TextField tf = new TextField(50);



public void init() {

    //往下拉选择框中添加内容

    colorChooser.add("红色");

    colorChooser.add("绿色");

    colorChooser.add("蓝色");



    //往列表选择框中添加内容

    colorList.add("红色");

    colorList.add("绿色");

    colorList.add("蓝色");



    //创建一个装载按钮和文本框的Panel容器

    Panel bottom = new Panel();

    bottom.add(tf);

    bottom.add(ok);



    //把bottom添加到Frame的底部

    frame.add(bottom,BorderLayout.SOUTH);



    //创建一个Panel容器,装载下拉选择框,单选框和复选框

    Panel checkPanel = new Panel();

    checkPanel.add(colorChooser);

    checkPanel.add(male);

    checkPanel.add(female);

    checkPanel.add(married);



    //创建一个垂直排列的Box容器,装载 多行文本域和checkPanel

    Box topLeft = Box.createVerticalBox();

    topLeft.add(ta);

    topLeft.add(checkPanel);



    //创建一个水平排列的Box容器,装载topLeft和列表选择框

    Box top = Box.createHorizontalBox();

    top.add(topLeft);

    top.add(colorList);



    //将top添加到frame的中间区域

    frame.add(top);



    //设置frame最佳大小并可见

    frame.pack();

    frame.setVisible(true);

}



public static void main(String[] args) {



    new BasicComponentDemo().init();



}

}




### []( )2.5.2 对话框Dialog



#### []( )2.5.2.1 Dialog



Dialog 是 Window 类的子类,是 一个容器类,属于特殊组件 。 对话框是可以独立存在的顶级窗口, 因此用法与普通窗口的用法几乎完全一样,但是使用对话框需要注意下面两点:



*   对话框通常依赖于其他窗口,就是通常需要有一个父窗口;

*   对话框有非模式(non-modal)和模式(modal)两种,当某个模式对话框被打开后,该模式对话框总是位于它的父窗口之上,在模式对话框被关闭之前,父窗口无法获得焦点。



| 方法名称 | 方法功能 |

| --- | --- |

| Dialog(Frame owner, String title, boolean modal) | 创建一个对话框对象:  

owner:当前对话框的父窗口  

title:当前对话框的标题  

modal:当前对话框是否是模式对话框,true/false |



**案例1:**



​ 通过Frame、Button、Dialog实现下图效果:



​ ![在这里插入图片描述](https://img-blog.csdnimg.cn/f7c92c6785974e808830c09f9dd18fab.jpg#pic_center)



**演示代码1:**



import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class DialogDemo1 {

public static void main(String[] args) {

	//1.创建一个frame窗口

    Frame frame = new Frame("这里测试Dialog");

	

	//2.创建两个Dialog对象

	//模式对话框:不关闭对话框,无法对父窗口进行操作

	//非模式对话框:不关闭对话框,也可以对父窗口进行操作

    Dialog d1 = new Dialog(frame, "模式对话框", true);

    Dialog d2 = new Dialog(frame, "非模式对话框", false);



	//3.创建两个按钮Button

    Button b1 = new Button("打开模式对话框");

    Button b2 = new Button("打开非模式对话框");



    //4.通过setBounds()方法设置对话框的大小和位置

    d1.setBounds(20,30,300,400);

    d2.setBounds(20,30,300,400);



    //5.给b1和b2绑定监听事件

    b1.addActionListener(new ActionListener() {

        @Override

        public void actionPerformed(ActionEvent e) {

            d1.setVisible(true);

        }

    });

    b2.addActionListener(new ActionListener() {

        @Override

        public void actionPerformed(ActionEvent e) {

            d2.setVisible(true);

        }

    });



    //6.把按钮添加到frame中

    frame.add(b1);

    frame.add(b2,BorderLayout.SOUTH);



    //7.设置frame最佳大小并可见

    frame.pack();

    frame.setVisible(true);



}

}




在Dialog对话框中,可以根据需求,自定义内容



**案例:**



​ 点击按钮,弹出一个模式对话框,其内容如下:



​ ![在这里插入图片描述](https://img-blog.csdnimg.cn/ec73456dc0cd493882a01cd395f16b14.jpg#pic_center)



**演示代码:**



public class DialogDemo2 {

public static void main(String[] args) {



    Frame frame = new Frame("这里测试Dialog");



    Dialog d1 = new Dialog(frame, "模式对话框", true);



    //创建一个Box容器,往对话框中添加内容,垂直添加

    Box vBox = Box.createVerticalBox();



    vBox.add(new TextField(15));

    vBox.add(new JButton("确认"));



	//把Box容器放到Dialog容器中

    d1.add(vBox);



    Button b1 = new Button("打开模式对话框");



    //设置对话框的大小和位置

    d1.setBounds(20,30,200,100);



    //给b1绑定监听事件

    b1.addActionListener(new ActionListener() {

        @Override

        public void actionPerformed(ActionEvent e) {

            d1.setVisible(true);

        }

    });



    //把按钮添加到frame中

    frame.add(b1);



    //设置frame最佳大小并可见

    frame.pack();

    frame.setVisible(true);



}

}




#### []( )2.5.2.1 FileDialog



Dialog 类还有 一个子类 : FileDialog ,它代表一个文件对话框,用于打开或者保存 文件,需要注意的是FileDialog无法指定模态或者非模态,这是因为 FileDialog 依赖于运行平台的实现,如果运行平台的文件对话框是模态的,那么 FileDialog 也是模态的;否则就是非模态的 。



| 方法名称 | 方法功能 |

| --- | --- |

| FileDialog(Frame parent, String title, int mode) | 创建一个文件对话框:  

parent:指定父窗口  

title:对话框标题  

mode:文件对话框类型,如果指定为FileDialog.load,用于打开文件,如果指定为FileDialog.SAVE,用于保存文件 |

| String getDirectory() | 获取被打开或保存文件的绝对路径 |

| String getFile() | 获取被打开或保存文件的文件名 |



**案例2:**



​ 使用 Frame、Button和FileDialog完成下图效果:



​![在这里插入图片描述](https://img-blog.csdnimg.cn/ece27908ed19434683f152a8e457d16c.jpg#pic_center)



**演示代码2:**



import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class FileDialogTest {

public static void main(String[] args) {



    Frame frame = new Frame("这里测试FileDialog");

	//1.创建两个FileDialog对象

    FileDialog d1 = new FileDialog(frame, "选择需要加载的文件", FileDialog.LOAD);

    FileDialog d2 = new FileDialog(frame, "选择需要保存的文件", FileDialog.SAVE);

	//2.创建两个按钮

    Button b1 = new Button("打开文件");

    Button b2 = new Button("保存文件");



    //3.给按钮添加事件

    b1.addActionListener(new ActionListener() {

        @Override

        public void actionPerformed(ActionEvent e) {

            d1.setVisible(true);

            //打印用户选择的文件路径和名称

            System.out.println("用户选择的文件路径:"+d1.getDirectory());

            System.out.println("用户选择的文件名称:"+d1.getFile());

        }

    });



    System.out.println("-------------------------------");

    b2.addActionListener(new ActionListener() {

        @Override

        public void actionPerformed(ActionEvent e) {

            d2.setVisible(true);

            //打印用户选择的文件路径和名称

            System.out.println("用户选择的文件路径:"+d2.getDirectory());

            System.out.println("用户选择的文件名称:"+d2.getFile());

        }

    });



    //4.添加按钮到frame中



    frame.add(b1);

    frame.add(b2,BorderLayout.SOUTH);



    //5.设置frame最佳大小并可见

    frame.pack();

    frame.setVisible(true);

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值