今天主要講下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可以多選。