[java]代码库
import java.awt.Button;
import java.awt.Label;
import java.awt.List;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
public class Jpaneldemo extends JFrame {
private List list = new List(50,true);
private Label name1 = new Label("姓 名:");
private TextField namet = new TextField();
private Label tel = new Label("性 别:");
private JRadioButton jBM = new JRadioButton("男");
private JRadioButton jBS= new JRadioButton("女");
private String [] sele = {"请选择系别","计算机科学与技术","生物医学工程","眼视光"};
JComboBox jC = new JComboBox(sele);
private Button addt = new Button("添加");
private Button del = new Button("删除");
public Jpaneldemo() {
setTitle("学生信息输入窗口");
this.name1.setBounds(240, 20, 50, 20);
this.namet.setBounds(290,20,80,20);
this.list.setBounds(10, 20, 200, 180);
this.tel.setBounds(240, 60, 50, 20);
this.jBM.setBounds(290, 60, 50, 20);
this.jBS.setBounds(340, 60, 50, 20);
this.jC.setBounds(240, 90, 130, 20);
this.addt.setBounds(240, 120, 50, 40);
this.del.setBounds(320,120, 50, 40);
ButtonGroup bg = new ButtonGroup();
bg.add(jBM);
bg.add(jBS);
this.list.add("姓名"+" "+"性别"+" "+"系别");
this.list.add("张三"+" "+"男"+" "+"计算机科学与技术",1);
this.list.add("刘33"+" "+"男"+" "+"计算机科学与技术",2);
this.list.setMultipleMode(false);
this.setLayout(null);
this.add(this.list);
this.add(this.tel);
this.add(this.jBM);
this.add(this.jBS);
this.add(this.name1);
this.add(this.namet);
this.add(this.addt);
this.add(this.del);
this.add(this.jC);
this.setSize(400, 280);
this.setLocation(100, 100);
this.setVisible(true);
}
public static void main(String[] args) {
final Jpaneldemo j1 = new Jpaneldemo();
j1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j1.addt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = j1.namet.getText();
String sex = null;
if(j1.jBM.isSelected()){
sex=j1.jBM.getText();
}else if(j1.jBS.isSelected()){
sex=j1.jBS.getText();
}
String sb =(String)j1.jC.getSelectedItem();
j1.list.add(name+" "+sex+" "+sb);
j1.namet.setText(null);
}
});
j1.del.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int a = j1.list.getSelectedIndex();
j1.list.delItem(a);
}
});
}
}