AWT常用基本组件

package com.one.component;

import javax.swing.*;
import java.awt.*;

import static javax.swing.Box.createHorizontalBox;

public class BasicComponentDemo {
    Frame frame = new Frame("这里测试基本组件");
//  topLeft添加一个文本域
    TextArea ta=new TextArea(5,20);
//  创建颜色下拉选框
    Choice colorChooser=new Choice();
//性别区域,创建两个复选框
    CheckboxGroup cbg=new CheckboxGroup();
    Checkbox man=new Checkbox("男",cbg,true);
    Checkbox woman=new Checkbox("女",cbg,false);

    Checkbox isMarried=new Checkbox("是否已婚");
//    topRingt区域添加一个list 50rows
    List colorList=new List(8);

//    下方添加一个单行文本域
    TextField tf=new TextField(30);
//    右侧放一个按钮
    Button ok=new Button("确认");
    public void init(){
//        组装界面
//        先组装上面,放置一个水平布局容器
        Box topBox=Box.createHorizontalBox();
//        topleft创建一个box
        Box topLeftColor=Box.createHorizontalBox();
        colorChooser.add("红色");
        colorChooser.add("绿色");
        colorChooser.add("蓝色");
        topLeftColor.add(colorChooser);
        topLeftColor.add(man);
        topLeftColor.add(woman);
        topLeftColor.add(isMarried);

        Box topLeft=Box.createVerticalBox();
        topLeft.add(ta);
        topLeft.add(topLeftColor);
//        将上面两个部分全部组装在一起
        topBox.add(topLeft);
        colorList.add("优秀");
        colorList.add("良好");
        colorList.add("及格");
        topBox.add(colorList);
//        组装下面的部分
        Box box=Box.createHorizontalBox();
        box.add(tf);
        box.add(ok);

        frame.add(topBox,BorderLayout.NORTH);
        frame.add(box,BorderLayout.SOUTH);

        frame.pack();
        frame.setVisible(true);



    }
    public static void main(String[] args) {
        new BasicComponentDemo().init();

    }
}

1、对话框Dialog

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

package com.one.component;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DialogDemo {
    public static void main(String[] args) {
        Frame frame = new Frame();
//        1、创建两个对话框对象,一个模式,一个非模式的
        Dialog d1 = new Dialog(frame,"模式对话框",true);
        Dialog d2 = new Dialog(frame,"非模式对话框",false);
//        2、通过setBounds方法设置Dialog的位置以及大小
        d1.setBounds(20,30,300,200);
        d2.setBounds(20,30,300,200);
//        3、创建两个按钮
        Button b1 = new Button("打开模式对话框");
        Button b2 = new Button("打开非模式对话框");
//        4、给这这两个按钮添加点击后的行为
        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
//             因为frame默认隐藏,Dialog也要设置可见
                d1.setVisible(true);
            }
        });
        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                d2.setVisible(true);
            }
        });
//        5、把按钮添加到frame中
        frame.add(b1);
        frame.add(b2,BorderLayout.SOUTH);




//      6、设置frame的最佳大小并可见
        frame.pack();
        frame.setVisible(true);

    }
}

在Dialog对话框中自定义显示内容

package com.one.component;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class FileDialogDemo {
    public static void main(String[] args) {
        Frame frame = new Frame("测试FileDialog");
//        1、创建两个FileDialog对象
        FileDialog f1 = new FileDialog(frame, "选择要打开的文件", FileDialog.LOAD);
        FileDialog f2 = 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) {
                f1.setVisible(true);//代码运行到这里时会阻塞
//                获取选择的路径及文件
                String directory = f1.getDirectory();
                String file = f1.getFile();
                System.out.println("打开文件的文件路径为:"+directory);
                System.out.println("打开文件的文件名称为:"+file);


            }
        });
        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                f2.setVisible(true);
                String directory = f2.getDirectory();
                String file = f2.getFile();
                System.out.println("保存的文件路径为:"+directory);
                System.out.println("保存的文件名称为:"+file);
            }
        });
//        4、把按钮添到frame中
        frame.add(b1,BorderLayout.NORTH);
        frame.add(b2);


        frame.pack();
        frame.setVisible(true);



    }
}

2、文件对话框FileDialog

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值