package jie;
import java.awt.*;
import java.awt.event.*;
/**
 * @author WangJie
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Zuoye 
{
public static void main(String args[])
{
MyClass my=new MyClass();
}}
class MyClass extends Frame implements ActionListener, WindowListener
{
Button[]buts;
String names[]={"9","8","7","/","6","5","4","*","3","2","1","-","0",".","=","+"};
Panel m1;
TextField text;
public MyClass()
{ super("我的作品");
setLayout(new BorderLayout());
m1=new Panel();
m1.setLayout(new GridLayout(4,4,4,4));
add("Center",m1);
text=new TextField();
//text.setAlignment();
add("North",text);
text.setBackground(Color.lightGray);



for(int i=0;i<names.length;i++)
{
Button btn=new Button(names[i]);
btn.addActionListener(this);
m1.add(btn);
    }
setVisible(true);
setSize(300,300);
setResizable(false);
}
public void windowClosing(WindowEvent e)
    {
     System.exit(0);
    }    public void windowOpened(WindowEvent e) { }    public void windowActivated(WindowEvent e) { }    public void windowDeactivated(WindowEvent e) { }    public void windowClosed(WindowEvent e) { }    public void windowIconified(WindowEvent e) { }    public void windowDeiconified(WindowEvent e) { }    

public void actionPerformed(ActionEvent e)
{
//dispose();
}

}
这各程序,为什么我点窗口上的X的时候,怎么也关不掉,初学者,真是烦死了,望哪位高手可以帮忙解决!在就是载textfield 里面如何才能能让输入框是右对齐的啊,就是从右边开始输入!

解决方案 »

  1.   

    你的程序写得乱七八糟,我很难改.给你个例子参考吧.import java.awt.*;
    import java.awt.event.*;
    //上面输入,回车,下面就显示
    class TextXie extends Frame implements KeyListener{
    TextField tf=new TextField();
    TextArea ta=new TextArea(7,15);
    TextXie()
    {
    this.setLayout(new BorderLayout());
    this.setSize(200,300);

    this.add(tf,BorderLayout.NORTH);
    this.add(ta,BorderLayout.CENTER);
    this.setVisible(true);
    tf.addKeyListener(this);
    this.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);//这句是关闭窗口
    }
    });
    }
    public void keyPressed(KeyEvent e){
    }

    public void keyTyped(KeyEvent e){
    if(e.getKeyChar()==e.VK_ENTER)
    {
    ta.setText(tf.getText());
    }
    }
    public void keyReleased(KeyEvent e){
    }
    public static void main(String[] args)
    {
    new TextXie();
    }
    }
      

  2.   

    你前面那个程序继承是JFrame,关闭用setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 这个程序继承是Frame,所以不能用这个.
      

  3.   

    楼主啊,你忘记了添加窗口事件监视器了addWindowListener(this);
      

  4.   

    你的程序问题太多了,错误多的都数不清楚,还能通过编译,真是你的运气,但是它肯定不可能正常运行,实现你的目的的。我简单改了一下:~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//package   jie;
    import   java.awt.*;
    import   java.awt.event.*;
    /**
      *   @author   WangJie
      *
      *   TODO   To   change   the   template   for   this   generated   type   comment   go   to
      *   Window   -   Preferences   -   Java   -   Code   Style   -   Code   Templates
      */
    public   class   Zuoye  
    {
    public   static   void   main(String   args[])
    {
    Myframe   my=new   Myframe();
    }
    }
    class   Myframe  extends   Frame   implements   ActionListener,WindowListener
    {
    Button buts[];
    String   names[]={"9","8","7","/","6","5","4","*","3","2","1","-","0",".","=","+"};
    Panel   m1;
    TextField   text;
    public   Myframe()
    { super("我的作品");
    //setLayout(new   BorderLayout());
    m1=new   Panel();
    m1.setLayout(new   GridLayout(4,4));
    add(m1,BorderLayout.CENTER);
    text=new   TextField();
    //text.setAlignment();
    add(text,BorderLayout.NORTH);
    text.setBackground(Color.lightGray);
    buts=new Button[names.length];
    for(int   i=0;i <names.length;i++)
    {
     buts[i]=new   Button(names[i]);
    buts[i].addActionListener(this);
    m1.add(buts[i]);
            }
    setVisible(true);
    setSize(300,300);
    validate();
    //setResizable(false);
    addWindowListener(this);
    }public void windowClosing(WindowEvent e)
    {
    //System.exit(0);
    dispose();
        }        public   void   windowOpened(WindowEvent   e)   {   }        public   void   windowActivated(WindowEvent   e)   {   }        public   void   windowDeactivated(WindowEvent   e)   {   }        public   void   windowClosed(WindowEvent   e)   {   }        public   void   windowIconified(WindowEvent   e)   {   }        public   void   windowDeiconified(WindowEvent   e)   {   }       public   void   actionPerformed(ActionEvent   e)
    {
    //dispose();
    for(int i=0;i<names.length;i++)
    {if (e.getSource()==buts[i])
         text.setText(names[i]);}
    }}
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      

  5.   

    前面程序的
    public   void   windowClosing(WindowEvent   e)
    {
    //System.exit(0);
    dispose();
            } 是我尝试修改的,忘记改回来了,应当是
    public   void   windowClosing(WindowEvent   e)
    {   System.exit(0);        }