Java程序设计 图形用户界面 【十一】列表框JList
列表框JList
JList常用方法
方法 | 作用 |
---|---|
public JList(ListModel dataModel) | 根据ListModel构造JList |
public JList(Object[] listData) | 根据对象数组构造JList |
public JList(Vector<?> listData) | 根据一个Vector构造JList |
public void setSelectionMode(int selectionMode) | 设置选择模式,是多选还是单选 |
public ListModel getModel() | 返回列表框的列表模型 |
public int[] getSelectedIndices() | 返回所选择的全部数组 |
可以通过ListSelectionModel接口完成列表框是多选还是单选
ListSelectionModel定义的常量
常量 | 作用 |
---|---|
static final int MULTIPLE_INTERVAL_SELECTION | 一次选择一个或多个连续的索引范围 |
static final int SINGLE_INTERVAL_SELECTION | 一次选择一个连续范围的值 |
static final int SINGLE_SELECTION | 一次选择一个值 |
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;
class MyList{
private JFrame frame = new JFrame("一");
private Container cont = frame.getContentPane();
private JList list1 = null;
private JList list2 = null;
public MyList(){
this.frame.setLayout(new GridLayout(1,3));
String n[] = {"A","B","C","D","E","F"};
Vector<String> v = new Vector<String>();
v.add("A");
v.add("B");
v.add("C");
this.list1 = new JList(n);
this.list2 = new JList(v);
list1.setBorder(BorderFactory.createTitledBorder("请选择"));
list2.setBorder(BorderFactory.createTitledBorder("请选择"));
list1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
list2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
cont.add(this.list1);
cont.add(this.list2);
this.frame.setSize(300,200);
this.frame.setVisible(true);
this.frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(1);
}
});
}
}
public class Hello {
public static void main(String[] args) {
new MyList();
}
}
第一个JList通过字符串数组设置列表的内容,并通过setSelectionMode()方法设置一次性可以选择多个选项
第二个JList通过Vector设置列表的内容,并通过setSelectionMode()方法将其设置成每次只能选择一个选项
使用ListModel构造JList
ListModel是一个专门用于创建JList列表内容操作的接口
方法 | 作用 |
---|---|
void addListDataListener(ListDataListener l) | 加入数据改变事件的监听 |
void removeListDataListener(ListDataListener l) | 加入数据删除时的监听 |
Object getElementAt(int index) | 返回指定索引处的内容 |
int getSize() | 返回列表长度 |
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
class MyListModel extends AbstractListModel{
private String n[]={"A","B","C","D"};
@Override
public int getSize() {
return this.n.length;
}
public Object getElementAt(int ind){
if(ind<this.n.length){
return this.n[ind];
}else {
return null;
}
}
}
class MyList{
private JFrame frame = new JFrame("一");
private Container cont = frame.getContentPane();
private JList list1 =null;
public MyList(){
this.list1 = new JList(new MyListModel());
list1.setBorder(BorderFactory.createTitledBorder("请选择"));
list1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
cont.add(this.list1);
this.frame.setSize(300,200);
this.frame.setVisible(true);
this.frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(1);
}
});
}
}
public class Hello {
public static void main(String[] args) {
new MyList();
}
}
JList事件处理
使用ListSelectionListener的监听接口实现对JList中所选项的监听
ListSelectionListener接口定义的方法
方法 | 作用 |
---|---|
void valueChanged(ListSelectionEvent e) | 当值发生改变时调用 |
ListSelectionEvent的常用方法
方法 | 作用 |
---|---|
public int getFirsIndex() | 返回选择的第1个选项的索引值 |
public int getLastIndex() | 返回选择的最后1个选项的索引值 |
对JList进行监听
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
class MyListModel extends AbstractListModel{
private String n[]={"A","B","C","D"};
@Override
public int getSize() {
return this.n.length;
}
public Object getElementAt(int ind){
if(ind<this.n.length){
return this.n[ind];
}else {
return null;
}
}
}
class MyList implements ListSelectionListener {
private JFrame frame = new JFrame("一");
private Container cont = frame.getContentPane();
private JList list1 =null;
public MyList(){
this.list1 = new JList(new MyListModel());
list1.setBorder(BorderFactory.createTitledBorder("请选择"));
list1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
list1.addListSelectionListener(this);
cont.add(this.list1);
this.frame.setSize(300,200);
this.frame.setVisible(true);
this.frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(1);
}
});
}
@Override
public void valueChanged(ListSelectionEvent e) {
int temp[] = list1.getSelectedIndices();
System.out.print("选的的内容是:");
for(int i:temp){
System.out.print(list1.getModel().getElementAt(i)+",");
}
System.out.println();
}
}
public class Hello {
public static void main(String[] args) {
new MyList();
}
}
到此这篇关于Java 入门图形用户界面设计之列表框JList的文章就介绍到这了,更多相关Java 列表框JList内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!