package itheima008; import javax.swing.*; import javax.swing.border.Border; import javax.swing.border.EtchedBorder; import javax.swing.border.TitledBorder; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.util.List; import java.util.Vector; public class JlistDemo2 { public static void main(String[] args) { new JlistDemo2().init(); } String[] book = {"java实战教学1", "java实战教学2", "java实战似懂非懂教学3", "java实战似懂非懂教学3","jav我a实战adsa四大教学5", "java实战教学6", "java实战教学7","java实战教学8", "java实战教学9"}; JFrame jf=new JFrame("测试JList,Jcombobox"); JPanel jPanel1=new JPanel(); JPanel jPanel2=new JPanel(); ButtonGroup buttonGroup1=new ButtonGroup(); ButtonGroup buttonGroup2=new ButtonGroup(); JList<String> jList; JComboBox<String> jComboBox; JTextArea jTextArea=new JTextArea(4,40); private void init() { //组装视图 jList=new JList<>(book); button1("纵向滚动", JList.VERTICAL); button1("纵向换行", JList.VERTICAL_WRAP); button1("横向换行", JList.HORIZONTAL_WRAP); button2("无限制", ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); button2("单选", ListSelectionModel.SINGLE_SELECTION); button2("单范围", ListSelectionModel.SINGLE_INTERVAL_SELECTION); jList.setVisibleRowCount(3); jList.setSelectionInterval(2,4); jList.addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { List<String> selectedValuesList = jList.getSelectedValuesList(); jTextArea.setText(""); for (String s : selectedValuesList) { jTextArea.append(s+"\n"); } } }); //组装左边 Box box=Box.createVerticalBox(); box.add(new JScrollPane(jList)); box.add(jPanel1); box.add(jPanel2); Vector<String> vector=new Vector<>(); jComboBox=new JComboBox<>(book); JPanel jPanel=new JPanel(); jComboBox.setEditable(true); jComboBox.setMaximumRowCount(4); jPanel.add(jComboBox); jComboBox.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { Object selectedItem = jComboBox.getSelectedItem(); jTextArea.setText(selectedItem.toString()); } }); Box box2=Box.createHorizontalBox(); box2.add(box); box2.add(jPanel); JLabel jLabel=new JLabel("最喜欢的图书"); JPanel jPanel3=new JPanel(); jPanel3.setLayout(new BorderLayout()); jPanel3.add(jLabel, BorderLayout.NORTH); jPanel3.add(new JScrollPane(jTextArea)); Box box3=Box.createVerticalBox(); box3.add(box2); box3.add(jPanel3); jf.add(box3); jf.pack(); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setVisible(true); } public void button1(String name, int layoutorient) { jPanel1.setBorder(new TitledBorder(new EtchedBorder(), "确定选项布局")); JRadioButton jRadioButton = new JRadioButton(name); jPanel1.add(jRadioButton); if (buttonGroup1.getButtonCount() == 0) { jRadioButton.setSelected(true); } buttonGroup1.add(jRadioButton); jRadioButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jList.setLayoutOrientation(layoutorient); } }); } public void button2(String name, int selectedMode) { jPanel2.setBorder(new TitledBorder(new EtchedBorder(), "确定选择模式")); JRadioButton jRadioButton = new JRadioButton(name); jPanel2.add(jRadioButton); if (buttonGroup2.getButtonCount() == 0) { jRadioButton.setSelected(true); } buttonGroup2.add(jRadioButton); jRadioButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { jList.setSelectionMode(selectedMode); } }); } }