import java.awt.*;
import java.awt.event.*;
public class Show_Hide_message {

/**
 * Method main
 *
 *
 * @param args
 *
 */
public static void main(String[] args) 
{
// TODO: Add your code here
MF__Show_Hide_message t1=new MF__Show_Hide_message();
t1.show();
}
}
class MF__Show_Hide_message extends Frame
{
Panel p1=new Panel();
Button b1=new Button("显示消息");
Label L1=new Label("");
public MF__Show_Hide_message()
{
setTitle("显示_隐藏消息");
setSize(400,250);
p1.setLayout(new BorderLayout());
p1.add(b1,BorderLayout.NORTH);
p1.add(L1,BorderLayout.CENTER);
b1.addActionListener(new myActionListener());
this.add(p1);
this.addWindowListener(new myWinEvent());
}

}
class myActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="显示消息")
{


((Button)e.getSource()).setLabel("隐藏消息");

L1.setText("Hello World!");//这里有语法问题
}
else
{
((Button)e.getSource()).setLabel("显示消息");

L1.setText("");//这里有语法问题

}
}
}class myWinEvent extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
我知道在类 myActionListener 中是不能直接访问L1的,如果一定要在 myActionListener 中访问L1的话又应该怎样实现呢?

解决方案 »

  1.   

    问一句楼主,为什么不用swing?还是用老的awt??
      

  2.   

    晕,直接学swing不就得了,
    把Frame作为内隐类就可以访问out class的属性了
      

  3.   

    //package others;
    import java.awt.*;
    import java.awt.event.*;public class Show_Hide_message {
    public Show_Hide_message() {
    MF__Show_Hide_message t1 = new MF__Show_Hide_message();
    t1.show();
    } Panel p1 = new Panel();
    Button b1 = new Button("显示消息");
    Label L1 = new Label("");
    public static void main(String[] args) {
    // TODO: Add your code here
    new Show_Hide_message(); } class MF__Show_Hide_message extends Frame { public MF__Show_Hide_message() {
    setTitle("显示_隐藏消息");
    setSize(400, 250);
    p1.setLayout(new BorderLayout());
    p1.add(b1, BorderLayout.NORTH);
    p1.add(L1, BorderLayout.CENTER);
    b1.addActionListener(new myActionListener());
    this.add(p1);
    this.addWindowListener(new myWinEvent()); } }
    class myActionListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand() == "显示消息") { ((Button) e.getSource()).setLabel("隐藏消息"); L1.setText("Hello World!");// 这里有语法问题
    } else {
    ((Button) e.getSource()).setLabel("显示消息"); L1.setText("");// 这里有语法问题 }
    }
    } class myWinEvent extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    }
    }
    ----
    我给你重写了,回答你这么多问题,记得给分哈!!不给分切掉小JJ