JScrollPane jsp = new JScrollPane(list2);
import java.applet.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyWindow extends JFrame implements ListSelectionListener{
JList list1,list2;
String news[]={"人民日报","新民晚报","浙江日报","文汇报"};
String sports[]={"足球","排球","乒乓球","篮球"};
JTextArea text;
MyWindow(String s){
super(s);
Container con = getContentPane();
con.setBackground(Color.BLUE);
con.setLayout(new GridLayout(2,2));
con.setSize(200,500);
list1 = new JList(news);
list1.setVisibleRowCount(3);
list1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list1.addListSelectionListener(this);
list2 = new JList(sports);
list2.setVisibleRowCount(2);
list2.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
list2.addListSelectionListener(this);
con.add(list1);
con.add(list2);
text= new JTextArea(10,20);
con.add(text);
this.setVisible(true);
this.pack();
}
public void valueChanged(ListSelectionEvent e){// 每当选择值发生更改时调用
if(e.getSource()==list1){
text.setText(null);
Object listValue = ((JList) e.getSource()).getSelectedValue();
String seleName = listValue.toString();
for(int i=0;i<news.length;i++)
if(news[i].equals(seleName)){
text.append(seleName+ "被选中\n");
}
}
else if(e.getSource()==list2){
text.setText(null);
int tempList[] =list2.getSelectedIndices();
for(int i=0;i<tempList.length;i++)
text.append(sports[tempList[i]] + "被选中\n");
}
}
}
public class Example6_3 extends Applet{
MyWindow myWin = new MyWindow("列表示例");
}
public class Example6_4{
public static void main(String args[]){
ComboBoxDemo mycomboBoxGUI = new ComboBoxDemo();
}
}
class ComboBoxDemo extends JFrame implements ActionListener,ItemListener{
public static final int Width = 350;
public static final int Height = 150;
String proList[] = { "踢足球","打篮球","打排球" };
JTextField text;
JComboBox comboBox;
public ComboBoxDemo(){
setSize(Width,Height);
setTitle("组合框使用示意程序");
Container conPane = getContentPane();
conPane.setBackground(Color.BLUE);
conPane.setLayout(new FlowLayout());
comboBox = new JComboBox(proList);
comboBox.addActionListener(this);
combobox.addItemListener(this);
comboBox.setEditable(true);//响应键盘输入
conPane.add(comboBox);
text = new JTextField(10);
conPane.add(text);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==comboBox)
text.setText(comboBox.getSelectedItem().toString());
}
public void itemStateChanged(ItemEvent e){
if(e.getSource()==comboBox){
text.setText(comboBox.getSelectedItem().toString());
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有