欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java Swing实战(三)文本组件JTextField和密码组件JPasswordField

程序员文章站 2024-01-17 10:34:58
接下来添加文本组件JTextField和密码组件JPasswordField。 效果如图: ......

接下来添加文本组件jtextfield和密码组件jpasswordfield。

/**
 * @author: lishuai
 * @date: 2018/11/26 13:51
 */
public class weimingsyncapplets {
    public static void main(string[] args) {
        // 面板组件
        jpanel taskpanel = new jpanel();
        jpanel dbpanel = new jpanel();
        jtabbedpane tabbedpane = buildjtabbedpane(taskpanel, dbpanel);
        buildframe(tabbedpane);
    }

    private static jtabbedpane buildjtabbedpane(jpanel taskpanel, jpanel dbpanel) {
        // 选项卡面板
        jtabbedpane tabbedpane = new jtabbedpane();
        // 通过borderfactory来设置边框的特性
        tabbedpane.setborder(borderfactory.createemptyborder(10, 10, 10, 10));
        tabbedpane.add("执行任务", taskpanel);
        tabbedpane.add("数据源配置", dbpanel);

        // 下拉框
        dbpanel.add(buildjlabel("数据库类型", 10, 20, 80, 25));
        string dbs[] = {"mysql", "oracle", "sqlserver"};
        dbpanel.add(buildjcombobox("mysql", "mysql", dbs, 0, 100, 20, 165, 25));

        // 文本框
        dbpanel.add(buildjlabel("用户名", 10, 50, 80, 25));
        jtextfield dbusername = buildjtextfield("dbusername", "dbusername", 20, 100, 50, 165, 25);
        dbpanel.add(dbusername);

        // 密码
        dbpanel.add(buildjlabel("密码", 10, 80, 80, 25));
        jpasswordfield dbpassword = buildjpasswordfield("dbpassword", "dbpassword", 20, 100, 80, 165, 25);
        dbpanel.add(dbpassword);

        return tabbedpane;
    }

    private static jpasswordfield buildjpasswordfield(string defaultvalue, string name, int columns, int x, int y, int width, int height) {
        jpasswordfield jpasswordfield = new jpasswordfield(columns);
        jpasswordfield.settext(defaultvalue);
        jpasswordfield.setname(name);
        jpasswordfield.setbounds(x, y, width, height);
        return jpasswordfield;
    }

    private static jtextfield buildjtextfield(string defaultvalue, string name, int columns, int x, int y, int width, int height) {
        jtextfield text = new jtextfield(columns);
        text.settext(defaultvalue);
        text.setname(name);
        text.setbounds(x, y, width, height);
        return text;
    }

    private static jcombobox buildjcombobox(object selecteditem, string name, string[] elements, int selectedindex, int x, int y, int width, int height) {
        defaultcomboboxmodel codetypemodel = new defaultcomboboxmodel();
        // elements 下拉框中的选项
        for (string element : elements) {
            codetypemodel.addelement(element);
        }
        jcombobox codetypebox = new jcombobox(codetypemodel);
        codetypebox.setname(name);
        // 默认选中的下拉框选项
        codetypebox.setselecteditem(selecteditem);
//        codetypebox.setselecteditem(selectedindex);
        codetypebox.setbounds(x, y, width, height);
        // 添加下拉框事件监听器
        codetypebox.additemlistener(new itemlistener() {
            @override
            public void itemstatechanged(itemevent e) {
                if (e.getstatechange() == itemevent.selected) {
                    // 选择的下拉框选项
                    system.out.println(e.getitem());
                }
            }
        });
        return codetypebox;
    }

    private static jlabel buildjlabel(string name, int x, int y, int width, int height) {
        jlabel label = new jlabel(name);
        label.setbounds(x, y, width, height);
        return label;
    }

    private static void buildframe(jcomponent component) {
        // 窗体容器
        jframe frame = new jframe("数据同步工具");
        frame.add(component);
        //  jframe.exit_on_close  退出
        //  jframe.hide_on_close  最小化隐藏
        frame.setdefaultcloseoperation(jframe.hide_on_close);
        // 设置布局
        frame.getcontentpane().setlayout(new borderlayout());
        frame.getcontentpane().add(borderlayout.center, component);
        // 设置窗口最小尺寸
        frame.setminimumsize(new dimension(1060, 560));
        // 调整此窗口的大小,以适合其子组件的首选大小和布局
        frame.pack();
        // 设置窗口相对于指定组件的位置
        frame.setlocationrelativeto(null);
        frame.setvisible(true);
        // 设置窗口尺寸是否固定不变
        frame.setresizable(true);
    }

}

效果如图:
Java Swing实战(三)文本组件JTextField和密码组件JPasswordField