JList

package JList;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.*;
import java.util.List;

import javax.swing.*;

public class JListDemo extends JFrame implements ActionListener{
    
    private static final long serialVersionUID = 1L;
    private JList _list;//list
    private JLabel _label;//显示选中item
    private JButton _button;//获取选中item
    private JTextField _textField;//输入框
    private DefaultListModel _listModel;
    
    private JPanel _containerPanel;
    private JPanel _panel02;
    private JPanel _panel03;
    private JPanel _panel04;
    public JListDemo(String title){
        super(title);
        this.setLayout(new GridLayout(1,2));
        this.setBounds(200,300,300,200);
        
        //添加子组件
        this.createComponents();
        
        //窗口事件监听
        this.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e) {
                System.exit(1);
            }
        });
        
        this.pack();
    }
    
    private void createComponents (){
        //1.添加list(用scrollPane封装)
        _listModel = new DefaultListModel();
        _list = new JList ();
        _list.setBorder(BorderFactory.createTitledBorder("你身在哪个国家?"));
        JScrollPane scrollPane = new JScrollPane (_list);
        
        //2.创建容器面板
        _containerPanel = new JPanel();
        _containerPanel.setLayout(new GridLayout(3,1,5,5));
        
        //3.添加以上两个组件
        this.add(scrollPane);
        this.add(_containerPanel);
        
        //4.配置容器面板
        this.configContainerPanel();
    }
    
    /**配置容器面板*/
    private void configContainerPanel(){
        
        _panel02 = new JPanel();
        _panel02.setLayout(new GridLayout(2,1));
        
        _panel03 = new JPanel();
        _panel03.setLayout(new GridLayout(2,1));
        
        _panel04 = new JPanel();
        _panel04.setLayout(new GridLayout(2,1));
        
        _containerPanel.add(_panel02);
        _containerPanel.add(_panel03);
        _containerPanel.add(_panel04);
        
        this.configPanel02();
        this.configPanel03();
        this.configPanel04();
    }
    
    /**配置子面板02*/
    private void configPanel02(){
        
        //1.添加显示的label
        _label = new JLabel ("显示结果");
        _label.setHorizontalAlignment(SwingConstants.CENTER);
        
        //2.添加按钮
        _button = new JButton("获取");
        //->添加监听
        _button.addActionListener(this);
                
        //3.添加面板装label&&button
        _panel02.add(_label);
        _panel02.add(_button);
    }
    
    /**配置子面板03*/
    private void configPanel03(){
        _textField = new JTextField("输入内容");
        _textField.setHorizontalAlignment(SwingConstants.CENTER);
        JButton button = new JButton("添加");
        button.addActionListener(this);
        _panel03.add(_textField);
        _panel03.add(button);
    }
    
    /**配置子面板04*/
    private void configPanel04(){   
        JButton delete = new JButton("删除");
        delete.addActionListener(this);
        
        JButton clear = new JButton("清空");
        clear.addActionListener(this);
        
        _panel04.add(delete);
        _panel04.add(clear);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (false == e.getSource() instanceof JButton){
            return;
        }
        JButton btn = (JButton)e.getSource();
        
        switch(btn.getText()){
        case "获取":
            StringBuffer strBuffer = new StringBuffer();
            List values=_list.getSelectedValuesList();
            for (int i=0;i
JList_第1张图片
JList.gif

你可能感兴趣的:(JList)