首页 > 编程语言 > 详细

java Swing 面板 选项卡展示demo

时间:2020-07-13 19:31:16      阅读:18      评论:0      收藏:0      [点我收藏+]

标签:editable   ram   err   new   sys   rac   

 

选项卡1:WeimingSyncApplets

package com.adao.swing;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

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("数据库类型", 30, 20, 80, 25));
        String dbs[] = {"mysql", "oracle", "sqlserver"};
        dbPanel.add(buildJComboBox("mysql", "mysql", dbs, 0, 100, 20, 165, 25));

        // 文本框
        dbPanel.add(buildJLabel("用户名", 30, 50, 80, 25));
        JTextField dbUserName = buildJTextField("dbUserName", "dbUserName", 20, 100, 50, 165, 25);
        dbPanel.add(dbUserName);

        // 密码
        dbPanel.add(buildJLabel("密码", 30, 80, 80, 25));
        JPasswordField dbPassWord = buildJPasswordField("dbPassWord", "dbPassWord", 20, 100, 80, 165, 25);
        dbPanel.add(dbPassWord);

        // 添加按钮,并为按钮绑定事件监听
        JButton saveButton = buildJButton("保存", 185, 230, 80, 25);
        addActionListener(saveButton);
        dbPanel.add(saveButton);

        return tabbedPane;
    }

    private static void addActionListener(JButton saveButton) {
        // 为按钮绑定监听器
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 对话框
                JOptionPane.showMessageDialog(null, "保存成功!");
            }
        });
    }

    private static JButton buildJButton(String name, int x, int y, int width, int height) {
        JButton button = new JButton(name);
        button.setBounds(x, y, width, height);
        return button;
    }

    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);
    }

}

选项卡2:WeimingSyncApplets2

package com.adao.swing;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class WeimingSyncApplets2 {
    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);

        // 创建面板容器
        JPanel tablePanel = new JPanel();
        // 设置BorderLayout布局方式
        tablePanel.setLayout(new BorderLayout());
        // 创建表格
        JTable table = configTable();
        // 使用普通的中间容器添加表格时,表头 和 内容 需要分开添加
        tablePanel.add(table.getTableHeader(), BorderLayout.NORTH);
        tablePanel.add(table, BorderLayout.CENTER);
        // 将容器放入taskPanel面板
        taskPanel.add(tablePanel);

        return tabbedPane;
    }

    private static JTable configTable() {
        // 创建 table
        JTable table = getTable();
        // 获取 model
        DefaultTableModel model = getTableModel();
        table.setModel(model);
        for (int i = 0; i < model.getRowCount(); i++) {
            // 获得表格的值
            String taskName = (String) model.getValueAt(i, 0);
            // 设置表格的值
            model.setValueAt(taskName + "执行时间:" + i + "分钟后", i, 2);
        }
        int[] columnWidth = {135, 165, 350, 150, 150, 100};
        for (int i = 0; i < columnWidth.length; i++) {
            // 设置表格各栏各行的尺寸
            table.getColumnModel().getColumn(i).setPreferredWidth(columnWidth[i]);
        }
        return table;
    }

    private static JTable getTable() {
        JTable table = new JTable() {
            @Override
            public void updateUI() {
                // 刷新
                super.updateUI();
                // 表格行高
                setRowHeight(36);
            }

            @Override
            public boolean isCellEditable(int row, int column) {
                // 第3列、第4列设置可以编辑,其余列不可编辑
                if (column != 4 && column != 5) {
                    return false;
                }
                return true;
            }
        };
        // 设置表头不可移动
        table.getTableHeader().setReorderingAllowed(false);
        // 一次只能选择一项
        table.setSelectionMode(1);
        return table;
    }

    private static DefaultTableModel getTableModel() {
        int rowCount = 4;
        Object[] columnNames = {"任务名称", "执行规则", "任务执行时间", "状态", "操作", "SQL"};
        Object[][] rowData = new Object[rowCount][columnNames.length];
        // 向表格中填充数据
        for (int i = 0; i < rowCount; i++) {
            for (int j = 0; j < columnNames.length; j++) {
                switch (j) {
                    case 0:
                        rowData[i][j] = "任务" + i;
                        break;
                    case 1:
                        rowData[i][j] = "0 0 0/" + i + " * * ? ";
                        break;
                    default:
                        rowData[i][j] = null;
                }
            }
        }
        return new DefaultTableModel(rowData, columnNames);
    }


    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 面板 选项卡展示demo

标签:editable   ram   err   new   sys   rac   

原文:https://www.cnblogs.com/adao21/p/13295051.html

(0)
(0)
   
举报
评论 一句话评论(0
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号