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.JFrame; |
|
public class Jpaneldemo extends JFrame { |
private List list = new List( 50 , true ); |
private Label le = new Label( "姓 名:" ); |
private TextField tx = new TextField(); |
private Label le2 = new Label( "电话号码:" ); |
private TextField te= new TextField(); |
private Button bt = new Button( "添加" ); |
private Button bt2 = new Button( "删除" ); |
public Jpaneldemo() { |
setTitle( "电话簿" ); |
this .le.setBounds( 20 , 10 , 50 , 20 ); |
this .tx.setBounds( 80 , 10 , 100 , 20 ); |
this .le2.setBounds( 200 , 10 , 60 , 20 ); |
this .te.setBounds( 260 , 10 , 100 , 20 ); |
this .bt.setBounds( 380 , 10 , 50 , 20 ); |
this .bt2.setBounds( 450 , 10 , 50 , 20 ); |
this .list.setBounds( 10 , 50 , 500 , 400 ); |
this .list.add( "姓名" + " " + "电话号码" ); |
this .list.add( "张三" + " " + "123456789" , 1 ); |
this .list.add( "刘11" + " " + "1111111111" , 2 ); |
this .list.setMultipleMode( false ); |
this .setLayout( null ); |
this .add( this .list); |
this .add( this .le2); |
this .add( this .te); |
this .add( this .le); |
this .add( this .tx); |
this .add( this .bt); |
this .add( this .bt2); |
this .setSize( 540 , 400 ); |
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.bt.addActionListener( new ActionListener() { |
|
public void actionPerformed(ActionEvent e) { |
String name = j1.tx.getText(); |
String phone = j1.te.getText(); |
j1.list.add(name+ " " +phone); |
j1.tx.setText( null ); |
j1.te.setText( null ); |
} |
}); |
j1.bt2.addActionListener( new ActionListener() { |
|
public void actionPerformed(ActionEvent e) { |
// TODO Auto-generated method stub |
int a = j1.list.getSelectedIndex(); |
j1.list.delItem(a); |
} |
}); |
} |
} |