代码:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
class JList_test extends JFrame
{
List l = new List(4,false);
List l1 = new List(4,false);
JTextArea t=new JTextArea(4,20);
JScrollPane jsp=new JScrollPane(t);
JButton b=new JButton("添加");
JButton b1=new JButton("删除");
JPanel p0=new JPanel();
JPanel p=new JPanel();
JPanel p1=new JPanel();
JList_test()
{
super("选修课");
p0.add(jsp);
l.addItem("java程序设计");
l.addItem("TCP/IP");
l.addItem("SQL");
l.addItem("数据结构");
l.addItem("操作系统");
l.addItem("组装与维护");
p.add(l);
p1.add(l1);
add(p0);
add(p);
add(p1);
add(b);
add(b1);
p0.setBorder(BorderFactory.createTitledBorder("课程选择记录"));
p.setBorder(BorderFactory.createTitledBorder("可选课程"));
p1.setBorder(BorderFactory.createTitledBorder("已选课程"));
setLayout(new FlowLayout());
setSize(330,300);
setVisible(true);
setResizable(false);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((d.width-350)/2,(d.height-300)/2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class Bb implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String name=((JButton)e.getSource()).getText();
if(name.equals("添加"))
{
t.append("曾经选择过的课程:"+l.getSelectedItem()+"\n");
l1.addItem(l.getSelectedItem());
l.remove(l.getSelectedItem());
}
if(name.equals("删除"))
{
t.append("曾经去掉的课程:"+l1.getSelectedItem()+"\n");
l.addItem(l1.getSelectedItem());
l1.remove(l1.getSelectedItem());
}
}
}
void init()
{
Bb test=new Bb();
b.addActionListener(test);
b1.addActionListener(test);
}
public static void main(String args[])
{
JList_test jt=new JList_test();
jt.init();
}
}
运行截图:
|