日期:2014-05-20  浏览次数:20756 次

GUI事件处理问题
想实现组件间切换,可是没实现。。。
部分代码如下
import javax.swing.*;
import javax.swing.border.*;

import java.awt.*;
import java.awt.event.*;
public class BorrowerIofoPanel extends JPanel implements ActionListener{
private JPanel infoPanel;
private JLabel name1,name2;
private JLabel position1,position2;
private JLabel id1,id2;
private JLabel mail1,mail2;
private JLabel maxBorrowNum1,maxBorrowNum2;
private JLabel maxBorrowTime1,maxBorrowTime2;
private JLabel maxContinueBorrowTime1,maxContinueBorrowTime2;
private JButton change_bt;
private Borrower borrower;
 
private ChangeKeyPanel changeKeyPanel;

public BorrowerIofoPanel(){
infoPanel=new JPanel();
borrower=new Borrower("1","dg","123456" );
name1=new JLabel("姓名");
name2=new JLabel(borrower.getBorrowerName());
position1=new JLabel("身份");
position2=new JLabel("本科生");
id1=new JLabel("编号");
id2=new JLabel(borrower.getBorrowerNum());
mail1=new JLabel("邮箱");
mail2=new JLabel("453636@qq.com");
maxBorrowNum1=new JLabel("最大借阅册数");
maxBorrowNum2=new JLabel("borrower.maxBorrowedNumber");
maxBorrowTime1=new JLabel("最长借阅时间");
maxBorrowTime2=new JLabel("31天");
maxContinueBorrowTime1=new JLabel("最大续借次数");
maxContinueBorrowTime2=new JLabel("borrower.maxContinueBorrowTime");
change_bt=new JButton("修改密码");
 
changeKeyPanel=new ChangeKeyPanel();
 
infoPanel.setLayout(new GridLayout(8,2));
infoPanel.setBorder(new TitledBorder(borrower.getBorrowerName()+":你好!" ));
changeKeyPanel.setVisible(false);
 
infoPanel.add(id1);
infoPanel.add(id2);
infoPanel.add(name1);
infoPanel.add(name2);
infoPanel.add(position1);
infoPanel.add(position2);
infoPanel.add(mail1);
infoPanel.add(mail2);
infoPanel.add(maxBorrowNum1);
infoPanel.add(maxBorrowNum2);
infoPanel.add(maxBorrowTime1);
infoPanel.add(maxBorrowTime2);
infoPanel.add(maxContinueBorrowTime1);
infoPanel.add(maxContinueBorrowTime2);
infoPanel.add(new JLabel());
infoPanel.add(change_bt);
 
add(infoPanel);
add(changeKeyPanel);
 
}
  public void actionPerformed(ActionEvent e){
  if(e.getSource().equals(change_bt)){
  infoPanel.setVisible(false);
  changeKeyPanel.setVisible(true);
   
  }
  }
   
   
   
  class ChangeKeyPanel extends JPanel implements ActionListener{
  private JLabel oldKeyLabel,newKeyLabel,confirmKeyLabel;
  private JPasswordField oldKeyField,newKeyField,confirmKeyField;
  private JButton ok_bt,ret_bt;
 
  public ChangeKeyPanel(){
  setLayout(new GridLayout(4,2));
  oldKeyLabel=new JLabel("旧密码 ");
  newKeyLabel=new JLabel("新密码 ");
  confirmKeyLabel=new JLabel("确认密码");
  oldKeyField=new JPasswordField();
  newKeyField=new JPasswordField();
  confirmKeyField=new JPasswordField();
  ok_bt=new JButton("确定");
  ret_bt=new JButton("返回");
 
  oldKeyField.setEditable(true);
  newKeyField.setEditable(true);
  confirmKeyField.setEditable(true);
 
  ok_bt.addActionListener(this);
  ret_bt.addActionListener(this);