多平台统一管理软件接口,如何实现多平台统一管理软件接口
401
2022-08-28
Java 入门图形用户界面设计之列表框JList
目录列表框JList使用ListModel构造JListJList事件处理
java程序设计 图形用户界面 【十一】列表框JList
列表框JList
JList常用方法
方法作用public JList(ListModel dataModel)根据ListModel构造JListpublic JList(Object[] listData)根据对象数组构造JListpublic JList(Vector> listData)根据一个Vector构造JListpublic 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
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 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 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.fraELCKgonTtNme.setVisible(true); this.frame.addWindowListener(new WindowAdapter() { @ELCKgonTtNOverride 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(); } }
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 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.fraELCKgonTtNme.setVisible(true); this.frame.addWindowListener(new WindowAdapter() { @ELCKgonTtNOverride 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(); } }
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.fraELCKgonTtNme.setVisible(true);
this.frame.addWindowListener(new WindowAdapter() {
@ELCKgonTtNOverride
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();
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~