这是第一个窗口
import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.*;
import javax.swing.JLabel;
public class ATM extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JPanel jContentPane1 = null;
private JLabel jLabel = null;
private JLabel jLabel1 = null;
private JTextField jTextField = null;
private JTextField jTextField1 = null;
public ATM() {
super();
initialize();
}
private void initialize() {
this.setSize(579, 422);
this.setContentPane(getJContentPane());
this.setTitle("ATM自动取款机");
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(185, 317, 210, 50));
jLabel1.setFont(new Font("Dialog", Font.BOLD, 14));
jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
jLabel1.setText("按任意键进入");
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(142, 151, 283, 67));
jLabel.setFont(new Font("Dialog", Font.BOLD, 24));
jLabel.setText("欢迎使用ATM自动取款机");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel, null);
jContentPane.add(jLabel1, null);
}
return jContentPane;
}
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(new Rectangle(224, 133, 185, 30));
}
return jTextField;
}
private JTextField getJTextField1() {
if (jTextField1 == null) {
jTextField1 = new JTextField();
jTextField1.setBounds(new Rectangle(225, 181, 184, 28));
}
return jTextField1;
}
}
这是第二个代码
import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.Dimension;
import java.awt.Rectangle;
public class ATM2 extends JFrame { private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JLabel jLabel2 = null;
private JTextField jTextField = null;
private JLabel jLabel3 = null;
private JTextField jTextField1 = null;
private JButton jButton = null;
private JButton jButton1 = null;
public ATM2() {
super();
initialize();
}
private void initialize() {
this.setSize(579, 422);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel3 = new JLabel();
jLabel3.setBounds(new Rectangle(106, 182, 119, 28));
jLabel3.setText("请输入密码:");
jLabel2 = new JLabel();
jLabel2.setBounds(new Rectangle(105, 136, 118, 25));
jLabel2.setText("请输入卡号:");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel2, null);
jContentPane.add(getJTextField(), null);
jContentPane.add(jLabel3, null);
jContentPane.add(getJTextField1(), null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJButton1(), null);
}
return jContentPane;
}
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(new Rectangle(224, 133, 185, 30));
}
return jTextField;
}
private JTextField getJTextField1() {
if (jTextField1 == null) {
jTextField1 = new JTextField();
jTextField1.setBounds(new Rectangle(225, 181, 184, 28));
}
return jTextField1;
}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(124, 312, 114, 29));
jButton.setText("确认");
}
return jButton;
}
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setBounds(new Rectangle(299, 311, 111, 29));
jButton1.setText("取消");
}
return jButton1;
}

如何实现上面两个窗口在同一代码里,并实现按任意键替换到第二个窗口效果,且不是通过重新new一个JFrame来实现转换的!就是在一窗口中实现几种不同的效果!
高手请指教!

解决方案 »

  1.   

    以下是ATM前台的代码 给你参考下  代码太多,我分为两段发给你 若需要全部代码联系我 Q41533911 :
     package.com.yw.BankFrame;import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.GridLayout;
    import java.io.File;import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;
    import javax.swing.border.LineBorder;import com.yw.Listener.ATM_Number_Listener;
    import com.yw.Listener.ATM_Option_Listener;
    import com.yw.Listener.ATM_View_Listener;public class ATM_Frame extends JFrame { private static final long serialVersionUID = 1L;
    private JButton[] leftButton = new JButton[4];
    private JButton[] rightButton = new JButton[4];
    private JButton[] number = new JButton[12];
    private JButton[] option = new JButton[3];
    private JButton card1, card2;
    private JLabel scanLabel;
    private JPanel leftPan, rightPan, topPan;
    private JPanel bigPan, pan[];
    private JPanel bottPan, bleftPan, brightPan;
    private Font font = new Font("方正舒体", Font.BOLD, 32);
    private ATM_View_Listener viewH = new ATM_View_Listener(this);
    private ATM_Number_Listener numberH = new ATM_Number_Listener(this);
    private ATM_Option_Listener optionH = new ATM_Option_Listener(this);
    private String num = "";// 控制数字的输入
    private boolean next = false;
    private boolean last = false;
    private int count = 0;
    public JTextField field;// 插卡操作
    public JPasswordField ps;
    public JLabel lab1[];// 登陆失败
    public JTextField filed;// 取款
    public JLabel lab4[];// 查询余额
    public JPasswordField ps7[];// 更改密码
    public JTextField field5;// 转账
    public JTextField field52;// 转账确认
    public JLabel lab6[];
    public File file;// 检查系统文件是否存在
    public JTextField field4;// 余额查询
    public JTextField field42;
    public ATM_Frame() {// ATM构造器
    super("ATM");
    // ----------------------点击插卡后的的界面--------------------
    pan = new JPanel[13];
    for (int i = 0; i < pan.length; i++) {
    pan[i] = new JPanel(null);
    pan[i].setBounds(80, 5, 220, 180);
    pan[i].setBackground(Color.BLUE);
    pan[i].setForeground(Color.WHITE);
    }
    bigPan = new JPanel(null);
    bigPan.setBorder(new LineBorder(Color.black));
    topPan = new JPanel(null);
    topPan.setBounds(5, 5, 380, 190);
    topPan.setBorder(new LineBorder(Color.black));
    leftPan = new JPanel(new GridLayout(0, 1, 5, 5));
    leftPan.setBounds(5, 5, 70, 180);
    rightPan = new JPanel(new GridLayout(0, 1, 5, 5));
    rightPan.setBounds(305, 5, 70, 180);
    topPan.add(rightPan, BorderLayout.EAST);
    topPan.add(leftPan, BorderLayout.WEST);
    for (int i = 0; i < leftButton.length; i++) {
    leftButton[i] = new JButton("===>");
    leftButton[i].addActionListener(viewH);
    rightButton[i] = new JButton("<===");
    rightButton[i].addActionListener(viewH);
    leftPan.add(leftButton[i]);
    rightPan.add(rightButton[i]);
    }
    card1 = new JButton("|__________取钱处__________|");
    card1.addActionListener(optionH);
    card1.setBounds(5, 200, 220, 50);
    card2 = new JButton("|____插卡处____|");
    card2.addActionListener(optionH);
    card2.setBounds(240, 200, 145, 50);
    bottPan = new JPanel(null);
    bottPan.setBounds(5, 260, 380, 200);
    bottPan.setBorder(new LineBorder(Color.BLACK));
    bleftPan = new JPanel(new GridLayout(4, 3, 5, 5));
    bleftPan.setBounds(5, 5, 190, 190);
    String string[] ={ "7", "8", "9", "4", "5", "6", "1", "2", "3", ".","0", "00" };
    for (int i = 0; i < number.length; i++) {
    number[i] = new JButton(string[i]);
    if (i != 9) {
    number[i].addActionListener(numberH);
    }
    bleftPan.add(number[i]);
    }
    brightPan = new JPanel(new GridLayout(0, 1, 15, 15));
    brightPan.setBounds(245, 5, 110, 190);
    String op[] = { "确定", "更正", "取消" };
    for (int i = 0; i < op.length; i++) {
    option[i] = new JButton(op[i]);
    option[i].addActionListener(optionH);
    brightPan.add(option[i]);
    }
    // ------------------------------欢迎界面-----------------------------
    this.welcome();
    bottPan.add(brightPan);
    bottPan.add(bleftPan);
    bigPan.add(bottPan);
    bigPan.add(card2);
    bigPan.add(card1);
    bigPan.add(topPan);
    this.add(bigPan);
    this.setBounds(350, 150, 400, 500);
    this.setIconImage(this.getToolkit().getImage("image/greendiamond.gif"));
    this.setResizable(false);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public JButton[] getLeftButton() {
    return leftButton;
    } public void setLeftButton(JButton[] leftButton) {
    this.leftButton = leftButton;
    }
    public JButton[] getRightButton() {
    return rightButton;
    }
    public void setRightButton(JButton[] rightButton) {
    this.rightButton = rightButton;
    }
    public JButton getCard1() {
    return card1;
    }
    public void setCard1(JButton card1) {
    this.card1 = card1;
    }
    public JButton getCard2() {
    return card2;
    }
    public void setCard2(JButton card2) {
    this.card2 = card2;
    }
    public JButton[] getNumber() {
    return number;
    }
    public void setNumber(JButton[] number) {
    this.number = number;
    }
    public JButton[] getOption() {
    return option;
    }
    public void setOption(JButton[] option) {
    this.option = option;
    }
    public JPanel getTopPan() {
    return topPan;
    }
    public void setTopPan(JPanel topPan) {
    this.topPan = topPan;
    }
    public JLabel getScanLabel() {
    return scanLabel;
    }
    public void setScanLabel(JLabel scanLabel) {
    this.scanLabel = scanLabel;
    }
    public JPanel[] getPan() {
    return pan;
    }
    public void setPan(JPanel[] pan) {
    this.pan = pan;
    }
    public boolean isLast() {
    return last;
    }
    public void setLast(boolean last) {
    this.last = last;
    }
    public boolean isNext() {
    return next;
    }
    public void setNext(boolean next) {
    this.next = next;
    }
    public String getNum() {
    return num;
    }
    public void setNum(String num) {
    this.num = num;
    }
    public int getCount() {
    return count;
    }
    public void setCount(int count) {
    this.count = count;
    }
    public void controlInit() {// 控制属性的初始化
    this.num = "";this.next = false;this.last = false;this.count = 0;
    }
    public void welcome() {
    scanLabel = new JLabel();
    if (this.sysOK()) {
    scanLabel.setText("欢迎使用ATM");
    } else {
    scanLabel.setText("系统繁忙.......");
    this.removeListen();
    }
    scanLabel.setFont(font);
    scanLabel.setBounds(80, 5, 220, 180);
    scanLabel.setBackground(Color.BLUE);
    scanLabel.setForeground(Color.WHITE);
    scanLabel.setOpaque(true);
    scanLabel.setBorder(BorderFactory.createBevelBorder(0));
    if (this.topPan.getComponentCount() > 2) {
    topPan.remove(2);
    }
    topPan.add(scanLabel);
    this.repaint();
    }
      

  2.   

    public void insertCard() {
    JLabel lab[] = new JLabel[6];field = new JTextField();ps = new JPasswordField();
    String str0[] = { "欢迎使用ATM,请插入IC卡", "请输入账号:", "请输入密码:", "取消", "确认",
    "取卡" };for (int i = 0; i < str0.length; i++) {lab[i] = new JLabel(str0[i]);lab[i].setForeground(Color.WHITE);}
    lab[0].setBounds(30, 20, 180, 20);// 欢迎使用ATM,请插入IC卡
    lab[1].setBounds(10, 70, 70, 20);// 请输入账号:
    lab[2].setBounds(10, 100, 70, 20);// 请输入密码:
    lab[3].setBounds(2, 150, 40, 20);// 取消
    lab[4].setBounds(190, 150, 40, 20);// 确认
    lab[5].setBounds(190, 60, 40, 20);// 取卡
    field.setBounds(85, 70, 100, 20);
    ps.setBounds(85, 100, 100, 20);
    for (int i = 0; i < lab.length; i++) {
    pan[0].add(lab[i]);
    }
    pan[0].add(field);
    pan[0].add(ps);
    topPan.remove(scanLabel);
    topPan.remove(pan[1]);
    topPan.add(pan[0]);
    this.repaint();
    }
    public void failLoad() {
    lab1 = new JLabel[4];
    String str1[] = { "", "", "返回", "取卡" }; for (int i = 0; i < str1.length; i++) {
    lab1[i] = new JLabel(str1[i]);
    lab1[i].setForeground(Color.WHITE);
    }
    lab1[0].setBounds(30, 60, 160, 20);
    lab1[1].setBounds(35, 85, 150, 20);
    lab1[2].setBounds(190, 105, 40, 20);// 返回
    lab1[3].setBounds(190, 60, 40, 20);// 取卡
    for (int i = 0; i < str1.length; i++) {
    pan[1].add(lab1[i]);
    }
    topPan.remove(2);topPan.add(pan[1]);this.repaint();
    }
    public void succeedLoad() {
    JLabel lab2[] = new JLabel[5];
    String str2[] = { "取款", "查询余额", "转账", "密码更改", "取卡" };
    for (int i = 0; i < str2.length; i++) {
    lab2[i] = new JLabel(str2[i]);
    if (i < str2.length - 1) {
    lab2[i].setBounds(2, 15 + 45 * i, 80, 20);
    } else {
    lab2[i].setBounds(190, 60, 40, 20);
    }
    lab2[i].setForeground(Color.WHITE);
    pan[2].add(lab2[i]);
    }
    topPan.remove(2);
    topPan.add(pan[2]);
    this.repaint();
    }
    public void draw() {
    JLabel lab3[] = new JLabel[9];
    String str3[] = { "", "300", "500", "1000", "提交", "100", "取消", "输入","金额:" };
    filed = new JTextField();
    filed.setBounds(85, 100, 100, 20);
    for (int i = 0; i < str3.length; i++) {
    lab3[i] = new JLabel(str3[i]);
    if (i < 4) {
    lab3[i].setBounds(2, 15 + 45 * i, 80, 20);}
    if (i >= 4 && i < 8) {
    lab3[i].setBounds(190, 15 + 45 * (i - 4), 80, 20);}
    if (i == 8) {
    lab3[i].setBounds(40, 100, 40, 20);}
    lab3[i].setForeground(Color.WHITE);
    pan[3].add(lab3[i]);}
    pan[3].add(filed);
    this.addListenerToPoin();
    topPan.remove(2);
    topPan.add(pan[3]);
    this.repaint();}
    public void inquireMoney() {
    lab4 = new JLabel[3];
    String str4[] = { "当前余额为:", "可用余额为:", "返回" };
    field4 = new JTextField();
    field42 = new JTextField();
    for (int i = 0; i < str4.length; i++) {
    lab4[i] = new JLabel(str4[i]);
    lab4[i].setForeground(Color.WHITE);
    pan[4].add(lab4[i]);}
    lab4[0].setBounds(10, 70, 70, 20);
    lab4[1].setBounds(10, 95, 70, 20);
    field4.setBounds(85, 70, 100, 20);
    field42.setBounds(85, 95, 100, 20);
    lab4[2].setBounds(190, 105, 40, 20);// 返回
    pan[4].add(field4);
    pan[4].add(field42);
    topPan.remove(2);
    topPan.add(pan[4]);
    this.repaint();}
    public void transferAccounts() {
    JLabel lab5[] = new JLabel[4];
    field5 = new JTextField();
    field52 = new JTextField();
    String str5[] = { "账户账号:", "转入金额:", "取消", "确认" };
    for (int i = 0; i < lab5.length; i++) {
    lab5[i] = new JLabel(str5[i]);
    lab5[i].setForeground(Color.WHITE);
    pan[5].add(lab5[i]);}
    lab5[0].setBounds(10, 70, 70, 20);// 请输入账号:
    lab5[1].setBounds(10, 100, 70, 20);// 请输入密码:
    field5.setBounds(85, 70, 100, 20);
    field52.setBounds(85, 100, 100, 20);
    lab5[2].setBounds(2, 150, 80, 20);
    lab5[3].setBounds(190, 150, 80, 20);
    pan[5].add(field5);
    pan[5].add(field52);
    topPan.remove(2);
    topPan.add(pan[5]);
    this.repaint();}
    public void transferOK() {
    lab6 = new JLabel[7];
    String str6[] = { "转账操作确认信息:", "转入账户:", "转出金额:", "确认", "取消", "", "" };
    for (int i = 0; i < str6.length; i++) {
    lab6[i] = new JLabel(str6[i]);
    pan[6].add(lab6[i]);
    lab6[i].setForeground(Color.WHITE);
    }
    lab6[0].setBounds(20, 50, 150, 20);
    lab6[1].setBounds(30, 75, 70, 20);
    lab6[5].setBounds(105, 75, 100, 20);
    lab6[5].setBorder(new LineBorder(Color.BLACK));
    lab6[2].setBounds(30, 100, 70, 20);
    lab6[6].setBounds(105, 100, 100, 20);
    lab6[6].setBorder(new LineBorder(Color.BLACK));
    lab6[3].setBounds(2, 150, 80, 20);
    lab6[4].setBounds(190, 15, 80, 20);
    topPan.remove(2);
    topPan.add(pan[6]);
    this.repaint();}
    public void moldPassword() {
    JLabel lab7[] = new JLabel[5];
    ps7 = new JPasswordField[3];
    String str7[] = { "请输入旧密码:", "请输入新密码:", "再次输入新密码:", "确认", "取消" };
    for (int i = 0; i < str7.length + 3; i++) {
    if (i < str7.length) {
    lab7[i] = new JLabel(str7[i]);
    lab7[i].setForeground(Color.WHITE);
    if (i < 3) {
    lab7[i].setBounds(10, 50 + 25 * i, 100, 20);
    }
    if (i == 3) {
    lab7[i].setBounds(190, 150, 80, 20);
    }
    if (i == 4) {
    lab7[i].setBounds(190, 15, 80, 20);
    }
    ;
    pan[7].add(lab7[i]);
    }
    if (i >= str7.length) {
    ps7[i - str7.length] = new JPasswordField();
    pan[7].add(ps7[i - str7.length]);
    ps7[i - str7.length].setBorder(new LineBorder  (Color.BLACK));
    ps7[i - str7.length].setBounds(110,
    50 + 25 * (i - str7.length), 100, 20);}}
    topPan.remove(2);topPan.add(pan[7]);this.repaint();}
             public void succeedPassword() {
    JLabel lab81 = new JLabel("您的新秘密已成功提交!!!");
    lab81.setForeground(Color.WHITE);
    lab81.setBounds(20, 50, 180, 30);
    JLabel lab82 = new JLabel("返回");
    lab82.setForeground(Color.WHITE);
    lab82.setBounds(2, 150, 80, 20);
    pan[8].add(lab81);
    pan[8].add(lab82);
    topPan.remove(2);
    topPan.add(pan[8]);
    this.repaint();}
             public void failPassword() {
    JLabel lab91 = new JLabel("密码不匹配,请从新输入");
    lab91.setForeground(Color.WHITE);
    lab91.setBounds(20, 50, 180, 30);
    JLabel lab92 = new JLabel("返回");
    lab92.setForeground(Color.WHITE);
    lab92.setBounds(2, 150, 80, 20);
    pan[9].add(lab91);
    pan[9].add(lab92);
    topPan.remove(2);
    topPan.add(pan[9]);
    this.repaint();}
    public void failDraw() {
    JLabel lab = new JLabel("您卡上的余额不足.........");
    JLabel lab2 = new JLabel("返回");
    lab.setForeground(Color.WHITE);
    lab2.setForeground(Color.WHITE);
    lab.setBounds(10, 50, 150, 20);
    lab2.setBounds(190, 150, 80, 20);
    pan[10].add(lab);
    pan[10].add(lab2);
    topPan.remove(2);
    topPan.add(pan[10]);
    this.repaint();}
    public void succeedTransfer() {
    JLabel lab = new JLabel("转账操作成功,请选择您要的服务");
    JLabel lab2 = new JLabel("返回");
    lab.setForeground(Color.WHITE);
    lab2.setForeground(Color.WHITE);
    lab.setBounds(10, 70, 220, 20);
    lab2.setBounds(190, 150, 80, 20);
    pan[11].add(lab);
    pan[11].add(lab2);
    topPan.remove(2);
    topPan.add(pan[11]);
    this.repaint();}
    public void failTransfer() {
    JLabel lab = new JLabel("转账操作失败,可能是对方");
    JLabel lab3 = new JLabel("账号不存在,或被冻结!!!");
    JLabel lab2 = new JLabel("返回");
    lab.setForeground(Color.WHITE);
    lab3.setForeground(Color.WHITE);
    lab2.setForeground(Color.WHITE);
    lab.setBounds(10, 70, 190, 20);
    lab3.setBounds(15, 90, 150, 20);
    lab2.setBounds(190, 150, 80, 20);
    pan[12].add(lab3);
    pan[12].add(lab);
    pan[12].add(lab2);
    topPan.remove(2);
    topPan.add(pan[12]);
    this.repaint();}
    public boolean sysOK() {// 判断是否是系统更新
    file = new File("D:/ac.acSys");
    return file.exists();}
    public void removeListen() {// 当系统繁忙时,移除插卡监听
    card2.removeActionListener(viewH);
    for (int i = 0; i < number.length; i++) {
    number[i].removeActionListener(viewH);}}
    public void addListenerToPoin() {// 当输入金额时給小数点添加监听
    for (int i = 0; i < number.length; i++) {
    if (i == 9) {
    number[i].addActionListener(viewH); }}}}