JavaSwing图形界面编程之JApplet(三)

时间:2023-01-26 16:44:59

JavaSwing图形界面编程之JApplet(三)

package three.day.applet;



import java.awt.BorderLayout;
import java.awt.Container;


import javax.swing.BorderFactory;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;




public class JAppletDemo03 extends JApplet implements ListSelectionListener
{
JList ls = null;
JLabel lb = null;
String[] s = {"小学","初中","高中","大学","研究生"};
    public void init()
    {
    Container ctp = this.getContentPane();
    ctp.setLayout(new BorderLayout());
    lb = new JLabel();
    ls = new JList(s);
    ls.setVisibleRowCount(2);
    ls.setBorder(BorderFactory.createTitledBorder("请选择"));
    ls.addListSelectionListener(this);
    ctp.add(lb,BorderLayout.NORTH);
    ctp.add(new JScrollPane(ls),BorderLayout.CENTER);
    System.out.println(Thread.currentThread().toString());
    }
@Override
public void valueChanged(ListSelectionEvent arg0) {
int m = 0;
String str = "选择的是:";
int[] index = ls.getSelectedIndices();
for(int i=0;i<index.length;i++)
{
m = index[i];
str += s[m]+" ";
}
lb.setText(str);
}


}