Java Swing編程:微調(diào)控制器和列表框
今天主要講下JSpinner和JList。JSpinner用的不多,一般都用滾動條來代替,但當值要精確時,用滾動條會經(jīng)常滾不到自己要的值,這也是很尷尬的,這時JSpinner就派上用場了。
其實JSpinner沒什么花樣,主要構(gòu)造時要傳個SpinnerModel,這個類有3個子類
SpinnerNumberModel用于設(shè)置數(shù)值型的JSpinner
SpinnerDateModel用于實現(xiàn)時間控件的微調(diào)
SpinnerListModel用于傳入List或數(shù)組之間的微調(diào),eg
- public class TestJSpinner
- {
- final int SPINNER_NUM = 6;
- JFrame mainWin = new JFrame("微調(diào)控制器示范");
- 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-----------
- //創(chuàng)建一個SpinnerNumberModel對象,指定最小值、最大值和步長
- SpinnerNumberModel numModel = new SpinnerNumberModel(3.4,
- -1.1, 4.3, 0.1);
- spinners[1] = new JSpinner(numModel);
- addSpinner(spinners[1], "數(shù) 值 范 圍" , valLabels[1]);
- //-----------使用SpinnerListModel的Spinner------------
- String[] books = new String[]
- {
- "輕量級J2EE企業(yè)應(yīng)用實戰(zhàn)",
- "Struts2權(quán)威指南",
- "基于J2EE的Ajax寶典"
- };
- //使用字符串數(shù)組創(chuàng)建SpinnerListModel對象
- SpinnerListModel bookModel = new SpinnerListModel(books);
- //使用SpinnerListModel對象創(chuàng)建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數(shù)組創(chuàng)建SpinnerListModel對象
- SpinnerListModel iconModel = new SpinnerListModel(icons);
- //使用SpinnerListModel對象創(chuàng)建JSpinner對象
- spinners[3] = new JSpinner(iconModel);
- addSpinner(spinners[3], "圖標序列值" , valLabels[3]);
- //-----------使用SpinnerDateModel的Spinner------------
- //分別獲取起始時間、結(jié)束時間、初時時間
- 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();
- //創(chuàng)建一個SpinnerDateModel對象,指定最小時間、最大時間和初始時間
- SpinnerDateModel dateModel = new SpinnerDateModel(init ,
- start , end , Calendar.HOUR_OF_DAY);
- //以SpinnerDateModel對象創(chuàng)建JSpinner
- spinners[4] = new JSpinner(dateModel);
- addSpinner(spinners[4], "時 間 范 圍" , valLabels[4]);
- //-----------使用DateEditor來格式化Spinner------------
- dateModel = new SpinnerDateModel();
- spinners[5] = new JSpinner(dateModel);
- //創(chuàng)建一個JSpinner.DateEditor對象,用于對指定Spinner進行格式化
- JSpinner.DateEditor editor = new JSpinner.DateEditor(spinners[5],
- "公元yyyy年MM月dd日 HH時");
- //設(shè)置使用JSpinner.DateEditor對象進行格式化
- spinners[5].setEditor(editor);
- addSpinner(spinners[5], "使用DateEditor" , valLabels[5]);
- //為“確定”按鈕添加一個事件監(jiān)聽器
- okBn.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent evt)
- {
- //取出每個微調(diào)控制器的值,并將該值用后面的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企業(yè)應(yīng)用實戰(zhàn)",
- "基于J2EE的Ajax寶典",
- "Struts2權(quán)威指南",
- "ROR敏捷開發(fā)最佳實踐"
- };
- 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添加事件監(jiān)聽器
- 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企業(yè)應(yīng)用實戰(zhàn)");
- bookCollection.add("基于J2EE的Ajax寶典");
- bookCollection.add("Struts2權(quán)威指南");
- bookCollection.add("ROR敏捷開發(fā)最佳實踐");
- //用一個Vector對象來創(chuàng)建一個JComboBox對象
- bookSelector = new JComboBox(bookCollection);
- //為JComboBox添加事件監(jiān)聽器
- bookSelector.addItemListener(new ItemListener()
- {
- public void itemStateChanged(ItemEvent e)
- {
- //獲取JComboBox所選中的項
- Object book = bookSelector.getSelectedItem();
- favoriate.setText(book.toString());
- }
- });
- //設(shè)置可以直接編輯
- bookSelector.setEditable(true);
- //設(shè)置下拉列表框的可視高度可同時顯示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除了樣子上的區(qū)別,就是JComboBox只支持單選,而JList可以多選。
原文鏈接:http://blog.csdn.net/terryzero/article/details/3820172
【編輯推薦】