package 列表框控件演示;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
public class MyFrame extends JFrame implements ActionListener {
//下面是DefaultListModel的私有变量
private DefaultListModel sourceModel;
private DefaultListModel destModel;
//创建两个JList类型的变量
private JList source; //尚未初始化
private JList dest = new JList();//初始化
//创建两个按钮对象
private JButton addButton = new JButton(">>");
private JButton removeButton = new JButton("<<");
//有参数的构造函数
public MyFrame(String title) {
super(title);//把此字符串传递给父类的构造函数显示在窗体的标题栏
//好在SWing API的设计者想出了用于事件处理的适配器Adapter类,从而省却实现众多接口方法的麻烦,以下是匿名内部类
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){//实现此方法,以此来优雅的终止程序
System.exit(0);
}
});
//创建DufaultListModel类的一个实例
sourceModel = new DefaultListModel();
//向创建的数据模型中添加几个String类型的选项
sourceModel.addElement("Banana");
sourceModel.addElement("Apple");
sourceModel.addElement("Orange");
sourceModel.addElement("Mango");
sourceModel.addElement("Pineapple");
sourceModel.addElement("Kiwi");
sourceModel.addElement("Strawberry");
sourceModel.addElement("Peach");
//创建一个JList实例然后将先前创建的数据模型作为参数传递给JList的构造函数
source = new JList(sourceModel);//这种先前没有实例化的
//为源列表框设置选择模式:单选模式
source.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//
source.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.DARK_GRAY,5),""+"Shop",0,0,null,Color.RED));
//设置初始化列表框中选中的第一个选项
source.setSelectedIndex(0);
source.setSelectionBackground(Color.BLACK);
source.setSelectionForeground(Color.WHITE);
//创建另一个列表框的数据模型
destModel = new DefaultListModel();
dest.setModel(destModel);//这种是先前创建好了实例对象的
dest.setSelectionBackground(Color.BLACK);
dest.setSelectionForeground(Color.WHITE);
dest.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.DARK_GRAY,5),""+"Fruit Basket",0,0,null,Color.RED));
//提示:通过创建数据模型,允许在程序运行时替换列表框中的内容
//建立GUI:用户界面的创建
//使用面板的目的是用作放置组件的容器
JPanel panel = new JPanel();
//为面板设置布局管理器为网格布局4行1列
panel.setLayout(new GridLayout(4,1,20,20));
panel.add(new JLabel());//第1行为空的即标签
panel.add(addButton);
panel.add(removeButton);
panel.add(new JLabel());//最后一行为空的即用标签来显示在面板上
//为窗口设置布局管理器为网格布局1行3列
this.setLayout(new GridLayout(1,3,20,20));
add(source);
add(panel);
add(dest);
//注册事件监听器:这是最后需要完成的一件事情,就是为按钮添加事件监听器
addButton.addActionListener(this);
removeButton.addActionListener(this);
}
//以下是按钮的事件处理代码
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource().equals(addButton))
{
if(source.getSelectedValue()!=null)//调用JList类的getSelectedValue()方法
{
String str=(String)source.getSelectedValue();
if(str!=null)
{
destModel.addElement(str);
dest.setSelectedIndex(0);
sourceModel.removeElement(str);
source.setSelectedIndex(0);
}
}
}
if(e.getSource().equals(removeButton))
{
if(dest.getSelectedValue()!=null)
{
String str=(String)dest.getSelectedValue();
if(str!=null)
{
sourceModel.addElement(str);
source.setSelectedIndex(0);
destModel.removeElement(str);
dest.setSelectedIndex(0);
}
}
}
}
}
下面是测试的类:
package 列表框控件演示;
public class ListDemoApp {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyFrame frame = new MyFrame("list Demo");
frame.setBounds(20,50,400,300);//(x,y,width,heigth)
//frame.setSize(400,300);(width,heigth)
frame.setVisible(true);
}
}