Java GUI 点击按钮退出


import java.awt.*;
import java.awt.event.*;

public class TestFrameTwo implements ActionListener
{
    Frame f = new Frame("((((99999");
    
    public static void main(String[] args)
    {
        TestFrameTwo tf = new TestFrameTwo();
        tf.init();
    }
    
    public void init()
    {
        Button btn = new Button("退出");
        btn.addActionListener(this);
        f.add(btn);
        f.setSize(300,300);
        f.setVisible(true);
    }
    
    public void actionPerformed(ActionEvent e)
    {
        f.setVisible(false);
        f.dispose();
        System.exit(0);
    }
}

 使用匿名对象版~

import java.awt.*;
import java.awt.event.*;

public class TestFrameThree {
    Frame f = new Frame("AAAA");
    public static void main(String[] args)
    {
        new TestFrameThree().init();
    }
    
    public void init()
    {
        Button btn = new Button("退出");
        btn.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                f.setVisible(false);
                f.dispose();
                System.exit(0);
            }
        });
        f.add(btn);
        f.setSize(300,300);
        f.setVisible(true);
    }
}

确实简洁了很多。。Go Go Go ~


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM