?? testlistmodel.java
字號:
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.math.BigDecimal;
/**
* Description:
* <br/>Copyright (C), 2005-2008, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class TestListModel
{
private JFrame mainWin = new JFrame("測試ListModel");
//根據NumberListModel對象來創建一個JList對象
private JList numScopeList = new JList(
new NumberListModel(1 , 21 , 2));
//根據NumberComboBoxModel對象來創建JComboBox對象
private JComboBox numScopeSelector = new JComboBox(
new NumberComboBoxModel(0.1 , 1.2 , 0.1));
private JTextField showVal = new JTextField(10);
public void init()
{
//JList的可視高度可同時顯示4個列表項
numScopeList.setVisibleRowCount(4);
//默認選中第三項到第五項(第一項的索引是0)
numScopeList.setSelectionInterval(2, 4);
//設置每個列表項具有指定的高度和寬度。
numScopeList.setFixedCellHeight(30);
numScopeList.setFixedCellWidth(90);
//為numScopeList添加監聽器
numScopeList.addListSelectionListener(new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent e)
{
//獲取用戶所選中的所有數字
Object[] nums = numScopeList.getSelectedValues();
showVal.setText("");
//把用戶選中的數字添加到單行文本框中
for (Object num : nums )
{
showVal.setText(showVal.getText() + num.toString() + " , ");
}
}
});
//設置列表項的可視高度可顯示5個列表項
numScopeSelector.setMaximumRowCount(5);
Box box = new Box(BoxLayout.X_AXIS);
box.add(new JScrollPane(numScopeList));
JPanel p = new JPanel();
p.add(numScopeSelector);
box.add(p);
//為numScopeSelector添加監聽器
numScopeSelector.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
//獲取JComboBox所選中的數字
Object num = numScopeSelector.getSelectedItem();
showVal.setText(num.toString());
}
});
JPanel bottom = new JPanel();
bottom.add(new JLabel("您選擇的值是:"));
bottom.add(showVal);
mainWin.add(box);
mainWin.add(bottom , BorderLayout.SOUTH);
mainWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWin.pack();
mainWin.setVisible(true);
}
public static void main(String[] args)
{
new TestListModel().init();
}
}
class NumberListModel extends AbstractListModel
{
protected BigDecimal start;
protected BigDecimal end;
protected BigDecimal step;
public NumberListModel(double start , double end , double step)
{
this.start = BigDecimal.valueOf(start);
this.end = BigDecimal.valueOf(end);
this.step = BigDecimal.valueOf(step);
}
//返回列表項的個數
public int getSize()
{
return (int)Math.floor(end.subtract(start).divide(step)
.doubleValue()) + 1;
}
//返回指定索引處的列表項
public Object getElementAt(int index)
{
return BigDecimal.valueOf(index).multiply(step).add(start);
}
}
class NumberComboBoxModel extends NumberListModel
implements ComboBoxModel
{
//用于保存用戶選中項的索引
private int selectId = 0;
public NumberComboBoxModel(double start , double end , double step)
{
super(start , end , step);
}
//設置選中“選擇項”
public void setSelectedItem(Object anItem)
{
if (anItem instanceof BigDecimal)
{
BigDecimal target = (BigDecimal)anItem;
//根據選中的值來修改選中項的索引
selectId = target.subtract(super.start).divide(step).intValue();
}
}
//獲取“選擇項”的值
public Object getSelectedItem()
{
//根據選中項的索引來取得選中項
return BigDecimal.valueOf(selectId).multiply(step).add(start);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -