Chinaunix首页 | 论坛 | 博客
  • 博客访问: 443681
  • 博文数量: 1496
  • 博客积分: 79800
  • 博客等级: 大将
  • 技术积分: 9940
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-09 13:22
文章分类

全部博文(1496)

文章存档

2011年(1)

2008年(1495)

我的朋友

分类:

2008-09-09 13:26:06

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class TestGUI 
{
    
public static void main(String[] args) 
    {
        frame f 
= new frame();
        
//还可以在这里加上一句
        ①f.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        f.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        f.setVisible(
true);
        f.addWindowListener(
new WindowAdapter()
        {
            
public void windowClosing(WindowEvent e)
            {
                
int i;
                i
=JOptionPane.showConfirmDialog(null,"are you ok");
                
                
if(i==JOptionPane.YES_OPTION)
                {
                    System.exit(
0););//或者还可以写成
                    this.dispose();//这两句都可以实现关闭窗口
                }
                
else
                {
                    
                    
                }
 
            }
        });
    }
}
 
class panel extends JPanel
{
    
public panel()
    {
        makeButton(
"red",Color.RED);
        makeButton(
"yellow",Color.yellow);
    }
 
    
private void makeButton(String name,final Color c)
    {
        Button button
=new Button(name);
        button.addActionListener(
new ActionListener()
        {
            
public void actionPerformed(ActionEvent event)
            {
                setBackground(c);
            }
        }    
        );
        add(button);
 
    }
 
}
 
class frame extends JFrame
{
    
public frame()
    {
        setTitle(
"test");
        setSize(
300,200);
        panel p
= new panel();
        
        add(p);
    }
 
}

--------------------next---------------------

阅读(81) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~