[java]代码库
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.util.List;
import javax.swing.JList;
import javax.swing.border.*;
import java.util.Vector;
//JFrame(窗口)<JMenuBar(菜单条)<JMenu(菜单)<JMenuItem(菜单里的项目)
//JFrame(窗口)<JScrollPane(滚动条)<JTextArea(文件编辑面板)<JPopupMenu(右键菜单)
//JFrame(窗口)<JScrollPane(滚动条)<JLabel(标签)
//JFrame(窗口)<JPanel(板面)<JTextField(文件编辑条)
//JFrame(窗口)<JPanel(板面)<JButton(按钮)
//JFrame(窗口)<JPanel(板面)<JRadioButton(单选按钮,用ButtonGroup组合)
//JFrame(窗口)<JPanel(板面)<JCheckBox(复选框)
//JFrame(窗口)<JPanel(板面)<JComboBox(下拉选择框)
//JFrame(窗口)<Box(布局管理器)
public class ListTest
{
private JFrame mainWin = new JFrame("测试列表框");
String[] books = new String[]{"轻量级Java EE企业级应用实战",
"疯狂Java讲义","疯狂Ajax讲义","疯狂Android讲义",
"经典Java EE企业级应用实战"};
//用一个 字符串数组来创建一个JList对象
JList<String> bookList = new JList<>(books);//----------------------这样加一打开就能看见
JComboBox<String> 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的可视高度可同时显示3个列表项
bookList.setVisibleRowCount(3);
//默认选中第三项和第五项(第一项的索引是0)
bookList.setSelectionInterval(2,4);
addLayoutButton("纵向滚动",JList.VERTICAL);
addLayoutButton("纵向换行",JList.VERTICAL_WRAP);
addLayoutButton("横向换行",JList.VERTICAL_WRAP);
addSelectModelButton("无限制",ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
addSelectModelButton("单选",ListSelectionModel.SINGLE_SELECTION);
addSelectModelButton("单范围",ListSelectionModel.SINGLE_INTERVAL_SELECTION);
Box listBox = new Box(BoxLayout.Y_AXIS);//-----------------------------------------小Box垂直排列
//将JList组件放在JScrollPane中,再将该JScrollPane添加到ListBox容器中
listBox.add(new JScrollPane(bookList));
//添加布局选择按钮面板,选择模式按钮面板。
//上面6个都通过下面的两个方法,加到了layoutPanel、selectModePanel中
//于是layoutPanel、selectModePanel再加到Box中
listBox.add(layoutPanel);//------------------------------------------有顺序,先加的就在上面
listBox.add(selectModePanel);
//为JList添加事件监听器
bookList.addListSelectionListener(new ListSelectionListener(){//--JList的事件监听器addListSelectionListener
public void valueChanged(ListSelectionEvent e){
//获取用户所选择的所有图书
List<String> books = bookList.getSelectedValuesList();
favoriate.setText("");//清空
for(String book : books){
favoriate.append(book+"\n");//显示在编辑板上
}
}
});
Vector<String> bookCollection = new Vector<>();//-------------------------Vector在右边JComboBox中
bookCollection.add("轻量级Java EE企业级应用实战");
bookCollection.add("疯狂Java讲义");
bookCollection.add("疯狂Ajax讲义");
bookCollection.add("疯狂Android讲义");
bookCollection.add("经典Java EE企业级应用实战");
//用一个Vector对象来创建一个JComboBox对象
bookSelector = new JComboBox<>(bookCollection);//-------------------------Vector在右边JComboBox中
//为JComboBox添加事件监听器
bookSelector.addItemListener(new ItemListener(){//-------------JComboBox的事件监听器addItemListener
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();//2个Box,3个JPanel。一个小Box加了2个JPanel,大Box加了小Box和1个JPanel
p.add(bookSelector);
Box box = new Box(BoxLayout.X_AXIS);//---------------------------------大Box水平排列
box.add(listBox);//-----------------------------------Box加入Box中,大Box水平排列,小Box垂直排列
box.add(p);
mainWin.add(box);
JPanel favoriatePanel = new JPanel();//----------------第4个JPanel加了JTextArea,后直接加到窗口中
favoriatePanel.setLayout(new BorderLayout());//这里布局是因为下面加了两个组件,JTextArea和JLabel
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 ae){
//改变列表框里列表项 的布局方向
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);//----------------------------单选按钮不加到ButtonGroup里就不是单选的了
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
//改变列表框里列表项选择模式
bookList.setSelectionMode(selectModel);
}
});
}
public static void main(String[] args){
new ListTest().init();
}
}
[代码运行效果截图]