今天主要讲下JSpinner和JList。JSpinner用的不多,一般都用滚动条来代替,但当值要精确时,用滚动条会经常滚不到自己要的值,这也是很尴尬的,这时JSpinner就派上用场了。
其实JSpinner没什么花样,主要构造时要传个SpinnerModel,这个类有3个子类
SpinnerNumberModel用于设置数值型的JSpinner
SpinnerDateModel用于实现时间控件的微调
SpinnerListModel用于传入List或数组之间的微调,eg
- public class TestJSpinner
- {
- final int SPINNER_NUM = 6;
- JFrame mainWin = new JFrame("微调控制器示范");
- Box spinnerBox = new Box(BoxLayout.Y_AXIS);
- JSpinner[] spinners = new JSpinner[SPINNER_NUM];
- JLabel[] valLabels = new JLabel[SPINNER_NUM];
- JButton okBn = new JButton("确定");
- public void init()
- {
- for (int i = 0 ; i < SPINNER_NUM ; i++ )
- {
- valLabels[i] = new JLabel();
- }
- //-----------普通Spinner-----------
- spinners[0] = new JSpinner();
- addSpinner(spinners[0], "普通" , valLabels[0]);
- //-----------指定最小值、最大值、步长的Spinner-----------
- //创建一个SpinnerNumberModel对象,指定最小值、最大值和步长
- SpinnerNumberModel numModel = new SpinnerNumberModel(3.4,
- -1.1, 4.3, 0.1);
- spinners[1] = new JSpinner(numModel);
- addSpinner(spinners[1], "数 值 范 围" , valLabels[1]);
- //-----------使用SpinnerListModel的Spinner------------
- String[] books = new String[]
- {
- "轻量级J2EE企业应用实战",
- "Struts2权威指南",
- "基于J2EE的Ajax宝典"
- };
- //使用字符串数组创建SpinnerListModel对象
- SpinnerListModel bookModel = new SpinnerListModel(books);
- //使用SpinnerListModel对象创建JSpinner对象
- spinners[2] = new JSpinner(bookModel);
- addSpinner(spinners[2], "字符串序列值" , valLabels[2]);
- //-----------使用序列值是ImageIcon的Spinner------------
- ArrayList<ImageIcon> icons = new ArrayList<ImageIcon>();
- icons.add(new ImageIcon("a.gif"));
- icons.add(new ImageIcon("b.gif"));
- //使用ImageIcon数组创建SpinnerListModel对象
- SpinnerListModel iconModel = new SpinnerListModel(icons);
- //使用SpinnerListModel对象创建JSpinner对象
- spinners[3] = new JSpinner(iconModel);
- addSpinner(spinners[3], "图标序列值" , valLabels[3]);
- //-----------使用SpinnerDateModel的Spinner------------
- //分别获取起始时间、结束时间、初时时间
- Calendar cal = Calendar.getInstance();
- Date init = cal.getTime();
- cal.add(Calendar.DAY_OF_MONTH , -3);
- Date start = cal.getTime();
- cal.add(Calendar.DAY_OF_MONTH , 8);
- Date end = cal.getTime();
- //创建一个SpinnerDateModel对象,指定最小时间、最大时间和初始时间
- SpinnerDateModel dateModel = new SpinnerDateModel(init ,
- start , end , Calendar.HOUR_OF_DAY);
- //以SpinnerDateModel对象创建JSpinner
- spinners[4] = new JSpinner(dateModel);
- addSpinner(spinners[4], "时 间 范 围" , valLabels[4]);
- //-----------使用DateEditor来格式化Spinner------------
- dateModel = new SpinnerDateModel();
- spinners[5] = new JSpinner(dateModel);
- //创建一个JSpinner.DateEditor对象,用于对指定Spinner进行格式化
- JSpinner.DateEditor editor = new JSpinner.DateEditor(spinners[5],
- "公元yyyy年MM月dd日 HH时");
- //设置使用JSpinner.DateEditor对象进行格式化
- spinners[5].setEditor(editor);
- addSpinner(spinners[5], "使用DateEditor" , valLabels[5]);
- //为“确定”按钮添加一个事件监听器
- okBn.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent evt)
- {
- //取出每个微调控制器的值,并将该值用后面的Label标签显示出来。
- for (int i = 0 ; i < SPINNER_NUM ; i++)
- {
- valLabels[i].setText(spinners[i].getValue().toString());
- }
- }
- });
- JPanel bnPanel = new JPanel();
- bnPanel.add(okBn);
- mainWin.add(spinnerBox, BorderLayout.CENTER);
- mainWin.add(bnPanel, BorderLayout.SOUTH);
- mainWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- mainWin.pack();
- mainWin.setVisible(true);
- }
- //定义一个方法,用于将滑动条添加到容器中
- public void addSpinner(JSpinner spinner, String description
- ,JLabel valLabel)
- {
- Box box = new Box(BoxLayout.X_AXIS);
- JLabel desc = new JLabel(description + ":");
- desc.setPreferredSize(new Dimension(100 , 30));
- box.add(desc);
- box.add(spinner);
- valLabel.setPreferredSize(new Dimension(180 , 30));
- box.add(valLabel);
- spinnerBox.add(box);
- }
- public static void main(String[] args)
- {
- new TestJSpinner().init();
- }
- }
相比前面的Jspinner来说,JList和JComboBox就用的多些,eg
- public class TestList
- {
- private JFrame mainWin = new JFrame("测试列表框");
- String[] books = new String[]
- {
- "Spring2.0宝典",
- "轻量级J2EE企业应用实战",
- "基于J2EE的Ajax宝典",
- "Struts2权威指南",
- "ROR敏捷开发最佳实践"
- };
- JList bookList = new JList(books);
- JComboBox bookSelector;
- //定义布局选择按钮所在的面板
- JPanel layoutPanel = new JPanel();
- ButtonGroup layoutGroup = new ButtonGroup();
- //定义选择模式按钮所在的面板
- JPanel selectModePanel = new JPanel();
- ButtonGroup selectModeGroup = new ButtonGroup();
- JTextArea favoriate = new JTextArea(4 , 40);
- public void init()
- {
- //JList的可视高度可同时显示三个列表项
- bookList.setVisibleRowCount(3);
- //默认选中第三项到第五项(第一项的索引是0)
- bookList.setSelectionInterval(2, 4);
- addLayoutButton("纵向滚动", JList.VERTICAL);
- addLayoutButton("纵向换行", JList.VERTICAL_WRAP);
- addLayoutButton("横向换行", JList.HORIZONTAL_WRAP);
- addSelectModelButton("无限制", ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
- addSelectModelButton("单选", ListSelectionModel.SINGLE_SELECTION);
- addSelectModelButton("单范围", ListSelectionModel.SINGLE_INTERVAL_SELECTION);
- Box listBox = new Box(BoxLayout.Y_AXIS);
- //将JList组件放在JScrollPane中,再将该JScrollPane添加到listBox容器中
- listBox.add(new JScrollPane(bookList));
- //添加布局选择按钮面板、选择模式按钮面板
- listBox.add(layoutPanel);
- listBox.add(selectModePanel);
- //为JList添加事件监听器
- bookList.addListSelectionListener(new ListSelectionListener()
- {
- public void valueChanged(ListSelectionEvent e)
- {
- //获取用户所选择的所有图书
- Object[] books = bookList.getSelectedValues();
- favoriate.setText("");
- for (Object book : books )
- {
- favoriate.append(book.toString() + "/n");
- }
- }
- });
- Vector<String> bookCollection = new Vector<String>();
- bookCollection.add("Spring2.0宝典");
- bookCollection.add("轻量级J2EE企业应用实战");
- bookCollection.add("基于J2EE的Ajax宝典");
- bookCollection.add("Struts2权威指南");
- bookCollection.add("ROR敏捷开发最佳实践");
- //用一个Vector对象来创建一个JComboBox对象
- bookSelector = new JComboBox(bookCollection);
- //为JComboBox添加事件监听器
- bookSelector.addItemListener(new ItemListener()
- {
- public void itemStateChanged(ItemEvent e)
- {
- //获取JComboBox所选中的项
- Object book = bookSelector.getSelectedItem();
- favoriate.setText(book.toString());
- }
- });
- //设置可以直接编辑
- bookSelector.setEditable(true);
- //设置下拉列表框的可视高度可同时显示4个列表项
- bookSelector.setMaximumRowCount(4);
- JPanel p = new JPanel();
- p.add(bookSelector);
- Box box = new Box(BoxLayout.X_AXIS);
- box.add(listBox);
- box.add(p);
- mainWin.add(box);
- JPanel favoriatePanel = new JPanel();
- favoriatePanel.setLayout(new BorderLayout());
- favoriatePanel.add(new JScrollPane(favoriate));
- favoriatePanel.add(new JLabel("您喜欢的图书:") , BorderLayout.NORTH);
- mainWin.add(favoriatePanel , BorderLayout.SOUTH);
- mainWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- mainWin.pack();
- mainWin.setVisible(true);
- }
- private void addLayoutButton(String label, final int orientation)
- {
- layoutPanel.setBorder(new TitledBorder(new EtchedBorder(), "确定选项布局"));
- JRadioButton button = new JRadioButton(label);
- //把该单选按钮添加到layoutPanel面板中
- layoutPanel.add(button);
- //默认选中第一个按钮
- if (layoutGroup.getButtonCount() == 0)
- button.setSelected(true);
- layoutGroup.add(button);
- button.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent event)
- {
- //改变列表框里列表项的布局方向
- bookList.setLayoutOrientation(orientation);
- }
- });
- }
- private void addSelectModelButton(String label, final int selectModel)
- {
- selectModePanel.setBorder(new TitledBorder(new EtchedBorder(), "确定选择模式"));
- JRadioButton button = new JRadioButton(label);
- //把该单选按钮添加到selectModePanel面板中
- selectModePanel.add(button);
- //默认选中第一个按钮
- if (selectModeGroup.getButtonCount() == 0)
- button.setSelected(true);
- selectModeGroup.add(button);
- button.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent event)
- {
- //改变列表框里的选择模式
- bookList.setSelectionMode(selectModel);
- }
- });
- }
- public static void main(String[] args)
- {
- new TestList().init();
- }
- }
JList和JComboBox除了样子上的区别,就是JComboBox只支持单选,而JList可以多选。
原文链接:http://blog.csdn.net/terryzero/article/details/3820172
【编辑推荐】