?2.让所有一级li元素左浮动并清除浮动。
?3.在一级li内创建二级ul列表,二级ul列表内建立二级li元素并清除默认样式。
?4.设置二级ul列表属性display:none让此元素不会被显示。
?5.在JS内动态设置一级li元素被点击时,让其下的二级ul列表属性切换为display:block让此元素显示。
Java程序:
import java.awt.*import java.awt.event.ItemEvent
import java.awt.event.ItemListener
import javax.swing.*
public class Main extends JFrame implements ItemListener {
JComboBox cmbProvince, cmbCity
JTextField txtProvince, txtCity
String[] provinces = {"请选择省份", "北京市", "上海市", "河南省"}
String[][] cities = {{"东城区", "西城区", "海淀区", "丰台区"}, {"浦东区", "徐汇区", "崇明县"}, {"郑州市", "洛阳市", "开封市"}}
public Main() {
super("请选择省份/城市")
this.setSize(350, 200)
this.setVisible(true)
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
this.setLayout(new FlowLayout())
init()
}
public void init() {
cmbProvince = new JComboBox(provinces)
cmbCity = new JComboBox(new String[]{"请选择城市"})
txtProvince = new JTextField(15)
txtCity = new JTextField(15)
this.add(cmbProvince)
this.add(txtProvince)
this.add(cmbCity)
this.add(txtCity)
cmbProvince.addItemListener(this)
cmbCity.addItemListener(this)
}
public static void main(String[] args) {
new Main()
}
@Override
public void itemStateChanged(ItemEvent arg0) {
if(arg0.getStateChange() != ItemEvent.SELECTED){
return
}
JComboBox cmb = (JComboBox) arg0.getSource()
int i
int index
if(cmb == cmbProvince) {
index = cmbProvince.getSelectedIndex()
if(index == 0) {
return
}
cmbCity.removeAllItems()
cmbCity.addItem("请选择城市")
for(i=0 i<cities[index-1].length i++) {
cmbCity.addItem(cities[index-1][i])
}
txtProvince.setText("您选择的省份是:" + cmbProvince.getSelectedItem().toString())
}
else if(cmb == cmbCity) {
index = cmbCity.getSelectedIndex()
if(index == 0) {
return
}
txtCity.setText("您选择的城市是:" + cmb.getSelectedItem().toString())
}
}
}
运行测试:
package testimport java.awt.event.ActionEvent
import java.awt.event.ActionListener
import javax.swing.DefaultListModel
import javax.swing.JButton
import javax.swing.JList
import javax.swing.WindowConstants
public class ListFrame extends javax.swing.JFrame {
private JList jList1
private JButton jButton1
private JButton jButton2
private JList jList2
private DefaultListModel listModel1
private DefaultListModel listModel2
private JButton jButton4
private JButton jButton3
{
//Set Look &Feel
try {
javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel")
} catch(Exception e) {
e.printStackTrace()
}
}
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
ListFrame inst = new ListFrame()
inst.setVisible(true)
}
public ListFrame() {
super()
initGUI()
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE)
getContentPane().setLayout(null)
{
listModel1 = new DefaultListModel()
listModel1.addElement("item 1")
listModel1.addElement("item 2")
listModel1.addElement("item 3")
listModel1.addElement("item 4")
listModel1.addElement("item 5")
listModel1.addElement("item 6")
jList1 = new JList(listModel1)
getContentPane().add(jList1)
jList1.setBounds(42, 28, 119, 203)
}
{
listModel2 = new DefaultListModel()
jList2 = new JList(listModel2)
getContentPane().add(jList2)
jList2.setBounds(238, 28, 119, 196)
}
{
jButton1 = new JButton()
getContentPane().add(jButton1)
jButton1.setText(">")
jButton1.setBounds(168, 84, 63, 28)
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton1ActionPerformed(evt)
}
})
}
{
jButton2 = new JButton()
getContentPane().add(jButton2)
jButton2.setText("<")
jButton2.setBounds(168, 133, 63, 28)
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton2ActionPerformed(evt)
}
})
}
{
jButton3 = new JButton()
getContentPane().add(jButton3)
jButton3.setText("添加")
jButton3.setBounds(168, 35, 63, 28)
jButton3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton3ActionPerformed(evt)
}
})
}
{
jButton4 = new JButton()
getContentPane().add(jButton4)
jButton4.setText("删除")
jButton4.setBounds(168, 189, 63, 28)
jButton4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton4ActionPerformed(evt)
}
})
}
pack()
setSize(400, 300)
setLocationRelativeTo(null)
} catch (Exception e) {
e.printStackTrace()
}
}
/** jList1移动到jList2*/
private void jButton1ActionPerformed(ActionEvent evt) {
if(jList1.getSelectedIndex()!=-1){
listModel2.addElement(jList1.getSelectedValue())
int i=jList1.getSelectedIndex()
listModel1.remove(i)
jList1.setSelectedIndex(i>0? i-1:0)
jList2.setSelectedIndex(listModel2.size()-1)
}
}
/** jList2移动到jList1*/
private void jButton2ActionPerformed(ActionEvent evt) {
if(jList2.getSelectedIndex()!=-1){
listModel1.addElement(jList2.getSelectedValue())
int i=jList2.getSelectedIndex()
listModel2.remove(i)
jList2.setSelectedIndex(i>0? i-1:0)
}
}
/** jList1增加项*/
private void jButton3ActionPerformed(ActionEvent evt) {
listModel1.addElement("new item")
}
/** jList1删除项*/
private void jButton4ActionPerformed(ActionEvent evt) {
if(jList1.getSelectedIndex()!=-1){
listModel1.remove(jList1.getSelectedIndex())
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)