import java.awt.*;
import java.awt.event.*;
import java.net.*;public class QQSampleFrame extends Frame  {
public void lunchFrame() {
this.setTitle("我的QQ");
this.setLocation(50,300);//屏幕的左上角点的位置
this.setSize(50, 600);  //屏幕的宽度和高度
this.setLayout(new FlowLayout(FlowLayout.CENTER,20,40));

Button b1 = new Button ("客户");
Button b2 = new Button ("系统");

Monitor m= new Monitor();

b1.addActionListener(m);
b2.addActionListener(m);
b2.setActionCommand("welcome to QQSanple :=. 0");
this.add(b1);
this.add(b2);


this.addWindowListener(new WindowAdapter(){  //窗口关闭的匿名类
public void windowClosing(WindowEvent e) {
System.exit(0);
}

});




this.setResizable(true);//能改变窗口的大小
this.setBackground(new Color(202, 204, 205));
setVisible(true);//显示出Frame这个桌面
}
public static void main (String[] args) {
QQSampleFrame qq = new QQSampleFrame();
qq.lunchFrame();

}

}
class Monitor implements ActionListener {
QQSampleFrame qq;
public void actionPerformed(ActionEvent e) {
TextField tf = new TextField();   //怎么才能将这个Button传到TextField ??? here 
qq.add(tf);
}

}
 想要类试于QQ界面的 .现在的目的是想点击那个客户按钮弹出个textField 文本框.. 该怎么办呢

解决方案 »

  1.   

    先排版
    import java.awt.*; 
    import java.awt.event.*; 
    import java.net.*; public class QQSampleFrame extends Frame  { 
    public void lunchFrame() { 
    this.setTitle("我的QQ"); 
    this.setLocation(50,300);//屏幕的左上角点的位置 
    this.setSize(50, 600);  //屏幕的宽度和高度 
    this.setLayout(new FlowLayout(FlowLayout.CENTER,20,40)); Button b1 = new Button ("客户"); 
    Button b2 = new Button ("系统"); Monitor m= new Monitor(); b1.addActionListener(m); 
    b2.addActionListener(m); 
    b2.setActionCommand("welcome to QQSanple :=. 0"); 
    this.add(b1); 
    this.add(b2); 
    this.addWindowListener(new WindowAdapter(){  //窗口关闭的匿名类 
    public void windowClosing(WindowEvent e) { 
    System.exit(0); 
    } }); 
    this.setResizable(true);//能改变窗口的大小 
    this.setBackground(new Color(202, 204, 205)); 
    setVisible(true);//显示出Frame这个桌面 

    public static void main (String[] args) { 
    QQSampleFrame qq = new QQSampleFrame(); 
    qq.lunchFrame(); } } 
    class Monitor implements ActionListener { 
    QQSampleFrame qq; 
    public void actionPerformed(ActionEvent e) { 
    TextField tf = new TextField();  //怎么才能将这个Button传到TextField ??? here 
    qq.add(tf); 
    } } 
      

  2.   

    QQSampleFrame qq;
    尚未初始化就qq.add(tf);会报空指针异常public void actionPerformed(ActionEvent e) { 
    TextField tf = new TextField();  //怎么才能将这个Button传到TextField ??? here 
    qq.add(tf); 

    这个地方,e.getSource()可以得到触发该事件的对象,你这里就是你的button
    点那个按钮这里就可以得到。返回值是Object类型,需要强转一下。
    而且这种做法……一个监听对应多个按钮,可能多了会有些乱。
      

  3.   


    class Monitor implements ActionListener {
    QQSampleFrame qq; public void actionPerformed(ActionEvent e) {
    TextField tf = new TextField(10); // 怎么才能将这个Button传到TextField ??? here
    Button button = (Button) e.getSource();
    button.getParent().add(tf);
    button.getParent().validate();
    // qq.add(tf);
    }}这样可以解决你这个问题,
    但你代码逻辑不应该是这样,得好好整理整理
      

  4.   


    点击button再new一个frame出来,代码如下:package com.briup;import java.awt.Button;
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Frame;
    import java.awt.TextField;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;public class QQSampleFrame extends Frame { public void lunchFrame() {
    this.setTitle("我的QQ");
    this.setLocation(50, 50);// 屏幕的左上角点的位置
    this.setSize(50, 600); // 屏幕的宽度和高度
    this.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 40)); Button b1 = new Button("客户");
    Button b2 = new Button("系统"); Monitor m = new Monitor(); b1.addActionListener(m);
    b2.addActionListener(m);
    b2.setActionCommand("welcome to QQSanple :=. 0");
    this.add(b1);
    this.add(b2); this.addWindowListener(new WindowAdapter() { // 窗口关闭的匿名类
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    } }); this.setResizable(true);// 能改变窗口的大小
    this.setBackground(new Color(202, 204, 205));
    setVisible(true);// 显示出Frame这个桌面 } public static void main(String[] args) {
    QQSampleFrame qq = new QQSampleFrame();
    qq.lunchFrame();
    }
    }class Monitor implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    Frame frame = new Frame("会话中"); TextField tf = new TextField();
    frame.add(tf); frame.setLocation(200, 200);
    frame.setSize(250, 300);
    frame.setVisible(true);
    }
    }
      

  5.   

    还没完呢 是一个工作者给我提出的一个问题..让我解出来..还有呢 我还想把他弄成client端. 系统那变成 server端. 我初学者 刚学了4个学.. 呵呵...等下我再发问 我先试着弄弄.