我要实现窗口间的转换,即点了登陆后,出现第二个窗口,登陆窗口消失。第一个登陆代码如下:
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.*;
public class DengLu2 extends Frame implements ActionListener
{
static DengLu2 frm=new DengLu2();
static Label lab1=new Label("用户名");
static Label lab2=new Label("密码");
static Label lab3=new Label("修改密码");
static Button btn1=new Button("登陆");
static Button btn2=new Button("重置");
static Button btn3=new Button("退出");
static TextField txf1=new TextField("admin");
static TextField txf2=new TextField();

public static void main(String args[])
{

frm.setTitle("登陆窗口");
frm.setLayout(null);
frm.setSize(280,200);
frm.setLocation(250, 250);
frm.setBackground(Color.yellow);
lab1.setBounds(20, 40, 40, 20);
lab2.setBounds(20, 60, 40, 20);
txf1.setBounds(80, 40, 120, 20);
txf2.setBounds(80, 60, 120, 20);
txf1.setEditable(true);
txf2.setEchoChar('*');
btn1.setBounds(30, 120, 50, 30);
btn2.setBounds(100, 120, 50, 30);
btn3.setBounds(170, 120, 50, 30);
btn2.addActionListener(frm);
btn3.addActionListener(frm);
frm.add(lab1);
frm.add(txf1);
frm.add(lab2);
frm.add(txf2);
frm.add(lab3);
frm.add(btn1);
frm.add(btn2);
frm.add(btn3);
frm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
Button btn=(Button)e.getSource();
if(btn==btn1)

if(btn==btn2)
txf1.setText(null);
txf2.setText(null);
if(btn==btn3)
System.exit(0);
}
}第二个是登陆成功后进入的代码,如下:
import java.awt.*;
import java.awt.event.*;
public class YuanGongXinXi extends Frame implements ActionListener
{
static YuanGongXinXi frm=new YuanGongXinXi();
static Button btn1=new Button("部门信息");
static Button btn2=new Button("职工信息");
static Button btn3=new Button("添加信息");
static Button btn4=new Button("删除信息");
static Button btn5=new Button("修改信息");
static Button btn6=new Button("员工工资结算信息");
static Button btn7=new Button("考勤信息");
static Button btn8=new Button("基本工资信息");
static Button btn9=new Button("修改密码");
static Button btn10=new Button("退出系统");
static Label lab1=new Label("职工信息");
static Label lab2=new Label("姓名");
static Label lab3=new Label("性别");
static Label lab4=new Label("编号");
static Label lab5=new Label("工类");
static Label lab6=new Label("入职时间");
static Label lab7=new Label("备注");
static TextField txt1=new TextField();
static TextField txt2=new TextField();
static TextField txt3=new TextField();
static TextField txt4=new TextField();
static TextField txt5=new TextField();
static TextField txt6=new TextField();
public static void main(String args[])
{ btn10.addActionListener(frm);
frm.setTitle("工资代发系统");
frm.setLayout(null);
frm.setSize(600,800);
btn1.setBounds(40, 100, 100, 20);
btn2.setBounds(40, 120, 100, 20);
btn3.setBounds(40, 140, 100, 20);
btn4.setBounds(40, 160, 100, 20);
btn5.setBounds(40, 180, 100, 20);
btn6.setBounds(40, 200, 100, 20);
btn7.setBounds(40, 220, 100, 20);
btn8.setBounds(40, 240, 100, 20);
btn9.setBounds(40, 260, 100, 20);
btn10.setBounds(40, 280, 100, 20);
lab1.setBounds(200, 100, 100, 20);
lab2.setBounds(220, 120, 100, 20);
lab3.setBounds(320, 120, 100, 20);
lab4.setBounds(420, 120, 100, 20);
lab5.setBounds(520, 120, 100, 20);
lab6.setBounds(620, 120, 100, 20);
lab7.setBounds(720, 120, 100, 20);
txt1.setBounds(220, 140, 100, 20);
txt2.setBounds(320, 140, 100, 20);
txt3.setBounds(420, 140, 100, 20);
txt4.setBounds(520, 140, 100, 20);
txt5.setBounds(620, 140, 100, 20);
txt6.setBounds(720, 140, 100, 20);
frm.add(btn1);
frm.add(btn2);
frm.add(btn3);
frm.add(btn4);
frm.add(btn5);
frm.add(btn6);
frm.add(btn7);
frm.add(btn8);
frm.add(btn9);
frm.add(btn10);
frm.add(lab1);
frm.add(lab2);
frm.add(lab3);
frm.add(lab4);
frm.add(lab5);
frm.add(lab6);
frm.add(lab7);
frm.add(txt1);
frm.add(txt2);
frm.add(txt3);
frm.add(txt4);
frm.add(txt5);
frm.add(txt6);
frm.setVisible(true);

}
public void actionPerformed(ActionEvent e)
{
Button btn=(Button)e.getSource();
if(btn==btn10)
System.exit(0);
}
}

解决方案 »

  1.   

    可能因为swing不属于j2ee范畴,我记得是。
      

  2.   

    主要是你不应该在显示窗口的时候用new应该用隐藏和显示。有那个属性自己找。我忘了。
      

  3.   

    一个项目,就别用两个main了。修改了一点点:import java.awt.Button;
    import java.awt.Color;
    import java.awt.Frame;
    import java.awt.Label;
    import java.awt.TextField;
    import java.awt.event.*;public class DengLu2 extends Frame implements ActionListener {
    static DengLu2 frm = new DengLu2(); static Label lab1 = new Label("用户名"); static Label lab2 = new Label("密码"); static Label lab3 = new Label("修改密码"); static Button btn1 = new Button("登陆"); static Button btn2 = new Button("重置"); static Button btn3 = new Button("退出"); static TextField txf1 = new TextField("admin"); static TextField txf2 = new TextField(); public static void main(String args[]) { frm.setTitle("登陆窗口");
    frm.setLayout(null);
    frm.setSize(280, 200);
    frm.setLocation(250, 250);
    frm.setBackground(Color.yellow);
    lab1.setBounds(20, 40, 40, 20);
    lab2.setBounds(20, 60, 40, 20);
    txf1.setBounds(80, 40, 120, 20);
    txf2.setBounds(80, 60, 120, 20);
    txf1.setEditable(true);
    txf2.setEchoChar('*');
    btn1.setBounds(30, 120, 50, 30);
    btn2.setBounds(100, 120, 50, 30);
    btn3.setBounds(170, 120, 50, 30);
    btn2.addActionListener(frm);
    btn1.addActionListener(frm);//加上的
    btn3.addActionListener(frm);
    frm.add(lab1);
    frm.add(txf1);
    frm.add(lab2);
    frm.add(txf2);
    frm.add(lab3);
    frm.add(btn1);
    frm.add(btn2);
    frm.add(btn3);
    frm.setVisible(true);
    } public void actionPerformed(ActionEvent e) {
    Button btn = (Button) e.getSource();
    if (btn == btn1) {
    System.out.println("登陆");
    this.dispose();
    new YuanGongXinXi();
    } if (btn == btn2) {
    txf1.setText(null);
    txf2.setText(null);
    }
    if (btn == btn3)
    System.exit(0);
    }
    }
    package swingTest;import java.awt.*;
    import java.awt.event.*;public class YuanGongXinXi extends Frame implements ActionListener {
    static Button btn1 = new Button("部门信息"); static Button btn2 = new Button("职工信息"); static Button btn3 = new Button("添加信息"); static Button btn4 = new Button("删除信息"); static Button btn5 = new Button("修改信息"); static Button btn6 = new Button("员工工资结算信息"); static Button btn7 = new Button("考勤信息"); static Button btn8 = new Button("基本工资信息"); static Button btn9 = new Button("修改密码"); static Button btn10 = new Button("退出系统"); static Label lab1 = new Label("职工信息"); static Label lab2 = new Label("姓名"); static Label lab3 = new Label("性别"); static Label lab4 = new Label("编号"); static Label lab5 = new Label("工类"); static Label lab6 = new Label("入职时间"); static Label lab7 = new Label("备注"); static TextField txt1 = new TextField(); static TextField txt2 = new TextField(); static TextField txt3 = new TextField(); static TextField txt4 = new TextField(); static TextField txt5 = new TextField(); static TextField txt6 = new TextField();



    public YuanGongXinXi() {
    btn10.addActionListener(this);
    this.setTitle("工资代发系统");
    this.setLayout(null);
    this.setSize(600, 800);
    btn1.setBounds(40, 100, 100, 20);
    btn2.setBounds(40, 120, 100, 20);
    btn3.setBounds(40, 140, 100, 20);
    btn4.setBounds(40, 160, 100, 20);
    btn5.setBounds(40, 180, 100, 20);
    btn6.setBounds(40, 200, 100, 20);
    btn7.setBounds(40, 220, 100, 20);
    btn8.setBounds(40, 240, 100, 20);
    btn9.setBounds(40, 260, 100, 20);
    btn10.setBounds(40, 280, 100, 20);
    lab1.setBounds(200, 100, 100, 20);
    lab2.setBounds(220, 120, 100, 20);
    lab3.setBounds(320, 120, 100, 20);
    lab4.setBounds(420, 120, 100, 20);
    lab5.setBounds(520, 120, 100, 20);
    lab6.setBounds(620, 120, 100, 20);
    lab7.setBounds(720, 120, 100, 20);
    txt1.setBounds(220, 140, 100, 20);
    txt2.setBounds(320, 140, 100, 20);
    txt3.setBounds(420, 140, 100, 20);
    txt4.setBounds(520, 140, 100, 20);
    txt5.setBounds(620, 140, 100, 20);
    txt6.setBounds(720, 140, 100, 20);
    this.add(btn1);
    this.add(btn2);
    this.add(btn3);
    this.add(btn4);
    this.add(btn5);
    this.add(btn6);
    this.add(btn7);
    this.add(btn8);
    this.add(btn9);
    this.add(btn10);
    this.add(lab1);
    this.add(lab2);
    this.add(lab3);
    this.add(lab4);
    this.add(lab5);
    this.add(lab6);
    this.add(lab7);
    this.add(txt1);
    this.add(txt2);
    this.add(txt3);
    this.add(txt4);
    this.add(txt5);
    this.add(txt6);
    this.setVisible(true);
    }
    public void actionPerformed(ActionEvent e) {
    Button btn = (Button) e.getSource();
    if (btn == btn10)
    System.exit(0);
    }
    }
    你这代码写得不好、多看看书,打好基础。
      

  4.   

    你把代码放在mian方法中??服了
    你把画窗口的代码放到构造方法中,登录的时候再new出来。