?? testlist.java
字號:
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
/**
* 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 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();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -