import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class ChoiceDemo extends JFrame implements ListSelectionListener{
String str[]={"Java 语言","C语言","PowerBuilder","SQL Sever","JBuilder"};
JComboBox cb=new JComboBox(str);
JList list=new JList(str);
JPanel jp1=new JPanel();
JPanel jp2=new JPanel();
JLabel lb=new JLabel();
public ChoiceDemo(){
super("这是关于列表的例子");
Container c=getContentPane();
cb.insertItemAt("选项"+cb.getItemCount()+"个", 0);
cb.setEditable(true);
jp1.add(cb);
c.add(jp1, BorderLayout.CENTER);
list.setBackground(Color.yellow);
list.setSelectionBackground(Color.red);
list.setSelectionForeground(Color.white);
list.setSelectionMode(1);
jp2.add(lb);
jp2.add(list);
c.add(jp2, BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,200);
setVisible(true);
}
public void valueChanged(ListSelectionEvent e)
{
int[] index=list.getSelectedIndices();
for(int i=0;i<index.length;i++){
lb.setText("你选择的语言是:"+str[index[i]]+" ");
}
}
public static void main(String args[]){
new ChoiceDemo();
}
}
jlist 和jcomboBox
最新推荐文章于 2023-12-29 04:54:13 发布