• Java Swing实战(三)文本组件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);
        }
    
    }
    

    效果如图:

  • 相关阅读:
    Mysql update from
    抽象类
    表自链接递归查询死循环
    复制订阅服务器和 AlwaysOn 可用性组 (SQL Server)
    C#找出接口的所有实现类并遍历执行这些类的公共方法
    Cors Http 访问控制
    返回参数去掉xml格式,以纯json格式返回(转)
    混布技术提升资源利用率
    fair scheduler配置
    ambari安装
  • 原文地址:https://www.cnblogs.com/pianistedward/p/10140649.html
Copyright © 2020-2023  润新知