第五章
5.1 Swing列表
JList
创建代码:
String [] fruit={“apple”,”pear”,”orange”,”banana”}
JList myJList=new JList(fruit);
构造器:
JList()
JList(Object [] listData)
JList(ListModel dataModel)
JList(Vector listData)
常见方法:
getSelecttionForegroud()
setSelectionForeGroud(Color selectionForegroud)
getSelectionBackgroud()
setSelectionBackgroud(Color selectionBackgroud)
getModel()
setModel(ListModel model)
setListData(Object[] listData)
setListData(Vector listData)
setSelectionMode(int selectionMode)
getSelectionMode()
getMaxSelectionIndex()
getMinSelectionIndex()
isSelectedeIndex(int index)
isSelectionEmpty()
clearSelection()
getSelectedValues()
在NetBeans中使用
代码和运行图如下
private void jTextFieldCountryNameActionPerformed(java.awt.event.ActionEvent evt) {
// TODO 将在此处添加您的处理代码:
String countryName=this.jTextFieldCountryName.getText().trim();
if(countryName !=null &&! countryName.equals("")){
this.imageVector.add(countryName);
this.jLabelActionMessage.setText("你将"+countryName+"添加到列表中");
this.jListCountryList.setListData(this.imageVector);
}else{this.jLabelActionMessage.setText("请先输入内容再添加");
}
}
private void jButtonDeleteActionPerformed(java.awt.event.ActionEvent evt) {
// TODO 将在此处添加您的处理代码:
String countryName=(String)this.jListCountryList.getSelectedValue();
if(countryName !=null){
this.imageVector.remove(countryName);
this.jListCountryList.setListData(imageVector);
this.jLabelActionMessage.setText("你将"+countryName+"从列表中删除");
}else{
this.jLabelActionMessage.setText("请先选择一个选项");
}
}
private void jButtonAddActionPerformed(java.awt.event.ActionEvent evt) {
// TODO 将在此处添加您的处理代码:
String countryName=this.jTextFieldCountryName.getText().trim();
if(countryName !=null &&! countryName.equals("")){
this.imageVector.add(countryName);
this.jLabelActionMessage.setText("你将"+countryName+"添加到列表中");
this.jListCountryList.setListData(this.imageVector);
}else{
this.jLabelActionMessage.setText("请先输入内容再添加");
}
}
private void jListCountryListValueChanged(javax.swing.event.ListSelectionEvent evt) {
// TODO 将在此处添加您的处理代码:
String choosedCountry=(String)this.jListCountryList.getSelectedValue();
this.jLabelActionMessage.setText("你从列表中选择"+choosedCountry+"选项");
}
Swing组合框(JComboBox)
构造器:
JComboBox()
JComboBox(Object[] items)
JComboBox(Vector items)
JComboBox(ComboBoxModel model)
常用方法:
setModel(ComboBoxModel aModel)
getModel()
setSelectedItem(Object anObject)
getSelectedItem()
setSelectedIndex(int anIndex)
getSelectedIndex()
addItem(Object anObject)
removeItem(Object anObject)
removeItemAt(int anIndex)
removeAllItems()
showPopup()
hidePopup()
setPopupVisible(Boolean v)
isPopupVisible()
getItemCount()
getItemAt(int index)
在NetBeans中使用
代码和运行图如下
//先添加一个JComboBox,然后选择model属性,删除原来的项,添加新的项。
private void jComboBoxLoverItemStateChanged(java.awt.event.ItemEvent evt) {
// TODO 将在此处添加您的处理代码:
String favor=(String)this.jComboBoxLover.getSelectedItem();
this.jLabel1.setText("你喜欢"+favor);
}
Swing组合框(JComboBox)
构造器:
JComboBox()
JComboBox(Object[] items)
JComboBox(Vector items)
JComboBox(ComboBoxModel model)
常用方法:
setModel(ComboBoxModel aModel)
getModel()
setSelectedItem(Object anObject)
getSelectedItem()
setSelectedIndex(int anIndex)
getSelectedIndex()
addItem(Object anObject)
removeItem(Object anObject)
removeItemAt(int anIndex)
removeAllItems()
showPopup()
hidePopup()
setPopupVisible(Boolean v)
isPopupVisible()
getItemCount()
getItemAt(int index)
在NetBeans中使用
代码和运行图如下
//先添加一个JComboBox,然后选择model属性,删除原来的项,添加新的项。
private void jComboBoxLoverItemStateChanged(java.awt.event.ItemEvent evt) {
// TODO 将在此处添加您的处理代码:
String favor=(String)this.jComboBoxLover.getSelectedItem();
this.jLabel1.setText("你喜欢"+favor);
}
Swing分割窗口(JSplitPane)
用于将一个窗口分割成两个部分。
构造器
JSplitPane()
JSplitPane(int newOrientation)
JSplitPane( int newOrientation,Boolean newContinuousLayout)
常用方法:
setDividerSize(int newSize)
getDividerSize()
setLeftComponent(Component comp)
Component getLeftComponent()