如何直接用java Swing写日期下拉列表框

如何直接用java Swing写日期下拉列表框,第1张

DateDropFrame extends JFrame

给year和month添加一个ItemListener来实时计算指定年和月中有多少天就可以了.

代码是用Swing写的,不过看你的图,不像是Swing界面.

/**

* Create the frame.

*/

public DateDropFrame() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

setBounds(100, 100, 450, 300)

contentPane = new JPanel()

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5))

contentPane.setLayout(new FlowLayout(FlowLayout.LEFT))

final JComboBox year = new JComboBox()

year.setModel(new DefaultComboBoxModel(getModel(start, end)))

contentPane.add(year)

final JComboBox month = new JComboBox()

month.setModel(new DefaultComboBoxModel(getModel(1, 12)))

contentPane.add(month)

final JComboBox day = new JComboBox()

contentPane.add(day)

year.addItemListener(new ItemListener() {

public void itemStateChanged(ItemEvent e) {

setDay(year, month, day)

}

})

month.addItemListener(new ItemListener() {

public void itemStateChanged(ItemEvent e) {

setDay(year, month, day)

}

})

setDay(year, month, day)

setContentPane(contentPane)

}

/**

* calculate days in select month &year

*/

private void setDay(JComboBox year, JComboBox month, JComboBox day) {

int y = Integer.parseInt((String) year.getSelectedItem())

int m = Integer.parseInt((String) month.getSelectedItem())

Calendar c = Calendar.getInstance()

c.set(Calendar.YEAR, y)

c.set(Calendar.MONTH, m - 1)

int days = c.getActualMaximum(Calendar.DAY_OF_MONTH)

day.setModel(new DefaultComboBoxModel(getModel(1, days)))

}

/**

* get String array [start, end]

*/

private String[] getModel(int start, int end) {

String[] m = new String[end - start + 1]

for (int i = 0i <m.lengthi++) {

m[i] = String.valueOf(i + start)

}

return m

}

public class RandomUtils {

    public static String randomId(){

        Random random = new Random()

        StringBuilder sb = new StringBuilder()

        for (int i = 0 i < 6 i++) {

            sb.append(random.nextInt(10))

        }

        return sb.toString()

    }

    public static int randomIntId(){

        Random random = new Random()

        StringBuilder sb = new StringBuilder()

        for (int i = 0 i < 6 i++) {

            sb.append(random.nextInt(10))

        }

        return Integer.parseInt(sb.toString())

    }

    public static String randomName(){

        String[] name1 = {"赵","钱","孙","李","周","吴","郑","王","张","刘"}

        String[] name2 = {"一","二","三","四","五","六","七","八","九","十"}

        Random random = new Random()

        StringBuilder sb = new StringBuilder()

        sb.append(name1[random.nextInt(10)])

        sb.append(name2[random.nextInt(10)])

        sb.append(name2[random.nextInt(10)])

        return sb.toString()

    }

    public static String randomSex(){

        Random random = new Random()

        return random.nextInt(4)/2==0?"男":"女"

    }

    public static int randomAge(int min, int max){

        Random random = new Random()

        return random.nextInt(min) + max-min

    }

    public static int randomScore(){

        Random random = new Random()

        return (int)(random.nextDouble()*100%100)

    }

    public static String randomAddress(){

        String[] address1 = {"商业","红叶","安康","河南","长海","黄河","桐柏","科学","瑞达","花园"}

        String[] address2 = {"东","南","西","北","中"}

        String[] address3 = {"街","路","大道","巷"}

        Random random = new Random()

        StringBuilder sb = new StringBuilder()

        sb.append(address1[random.nextInt(10)])

        sb.append(address2[random.nextInt(5)])

        sb.append(address3[random.nextInt(4)])

        sb.append(random.nextInt(1000)+1)

        sb.append("号")

        return sb.toString()

    }

} import javax.swing.*

import javax.swing.event.TreeSelectionEvent

import javax.swing.event.TreeSelectionListener

import javax.swing.table.DefaultTableModel

import javax.swing.tree.DefaultMutableTreeNode

import java.awt.*

import java.util.*

import java.util.List

public class StudentManage extends JFrame{

    public final static String[] CH_NUM = {"一","二","三","四","五","六","七","八","九","十"}

    public final static String ROOT = "班级管理"

    public final static int GRADE_NUM = 3

    public final static int CLASS_NUM = 3

    private Map<String, List<Student>> classes

    public StudentManage() {

        setTitle("学生管理系统")

        setSize(300, 300)

        setLayout(new BorderLayout())

        if(classes == null) classes = new HashMap<>()

        DefaultMutableTreeNode top = new DefaultMutableTreeNode(ROOT)

        DefaultMutableTreeNode[] gradeNodes = new DefaultMutableTreeNode[GRADE_NUM]

        for (int i = 0 i < GRADE_NUM i++) {

            gradeNodes[i] = new DefaultMutableTreeNode(CH_NUM[i] + "年级")

            for (int j = 0 j < CLASS_NUM j++) {

                gradeNodes[i].add(new DefaultMutableTreeNode(CH_NUM[j] + "班"))

            }

            top.add(gradeNodes[i])

        }

        final JTree tree = new JTree(top)

        String title[] = {"ID","姓名","性别","年龄","地址"}

        final DefaultTableModel model = new DefaultTableModel(title, 0)

        JTable table=new JTable(model)

        tree.addTreeSelectionListener(new TreeSelectionListener() {

            @Override

            public void valueChanged(TreeSelectionEvent e) {

                DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree .getLastSelectedPathComponent()

                if (node == null) return

                Object object = node.getUserObject()

                if (node.isLeaf()) {

                    List<Student> stdList = classes.get(getFullPath(node))

                    Object[][] data = new Object[stdList.size()][5]

                    for (int i = 0 i < stdList.size() i++) {

                        data[i][0] = stdList.get(i).getId()

                        data[i][1] = stdList.get(i).getName()

                        data[i][2] = stdList.get(i).getSex()

                        data[i][3] = stdList.get(i).getAge()

                        data[i][4] = stdList.get(i).getAddress()

                    }

                    while(model.getRowCount()>0){

                        model.removeRow(model.getRowCount()-1)

                    }

                    for (int i = 0 i < stdList.size() i++) {

                        model.addRow(data[i])

                    }

                    model.fireTableDataChanged()

                }

            }

        })

        add(tree, BorderLayout.WEST)

        add(new JScrollPane(table), BorderLayout.CENTER)

        setVisible(true)

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

    }

    private String getFullPath(DefaultMutableTreeNode defaultMutableTreeNode){

        StringBuilder sb = new StringBuilder()

        Object[] objs = defaultMutableTreeNode.getUserObjectPath()

        for (int i = 0 i < objs.length i++) {

            sb.append(objs[i])

        }

        return sb.toString().replace(ROOT,"")

    }

    public void init(){

        for (int i = 0 i < GRADE_NUM i++) {

            for (int j = 0 j < CLASS_NUM j++) {

                int n = new Random().nextInt(30)+1

                List<Student> stdList = new ArrayList<>(n)

                for (int k = 0 k < n k++) {

                    stdList.add(new Student(RandomUtils.randomIntId(),RandomUtils.randomName(),RandomUtils.randomSex(),

                            RandomUtils.randomAge(6,12),RandomUtils.randomAddress()))

                }

                classes.put(CH_NUM[i] + "年级"+ CH_NUM[j] +"班", stdList)

            }

        }

    }

    public static void main(String[] args) {

        StudentManage studentManage = new StudentManage()

        studentManage.init()

    }

}

class Student {

    private int id

    private String name

    private String sex

    private int age

    private String address

    public Student(int id, String name, String sex, int age, String address) {

        this.id = id

        this.name = name

        this.sex = sex

        this.age = age

        this.address = address

    }

    public Student() {

    }

    public int getId() {

        return id

    }

    public String getName() {

        return name

    }

    public String getSex() {

        return sex

    }

    public int getAge() {

        return age

    }

    public String getAddress() {

        return address

    }

    @Override

    public String toString() {

        return "Student{" +

                "id=" + id +

                ", name='" + name + '\'' +

                ", sex='" + sex + '\'' +

                ", age=" + age +

                ", address='" + address + '\'' +

                '}'

    }

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11555692.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存