| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 898 人关注过本帖
标题:java GUI程序!不明全部作用,求各部分解释,谢谢!
只看楼主 加入收藏
daemon1
Rank: 1
等 级:新手上路
帖 子:72
专家分:3
注 册:2011-4-14
结帖率:80%
收藏
已结贴  问题点数:20 回复次数:2 
java GUI程序!不明全部作用,求各部分解释,谢谢!
import java.awt.*;
import java.awt.event.*;


class Display
{
    public static final int
    EVENT = 0,COMPONENT = 1,MOUSE = 2,MOUSE_MOVE = 3,
    FOCUS = 4,KEY = 5,ACTION = 6,LAST = 7;
    public String[] evnt;
    Display()
    {
        evnt = new String[LAST];
        for(int i = 0; i < LAST; i++)
        {
            evnt[i] = new String();
        }
    }
    public void show(Graphics g)
    {
        for(int i = 0; i < LAST; i++)
        {
            g.drawString(evnt[i], 0, 10*i+10);
        }
    }
}


class EnablePanel extends Panel
{
    Color c;
    int id;
    Display display = new Display();
    public EnablePanel(int i,Color mc)
    {
        id = i;
        c = mc;
        setLayout(new BorderLayout());
        add(new MyButton(),BorderLayout.SOUTH);
        addComponentListener(new CL());
        addFocusListener(new FL());
        addKeyListener(new KL());
        addMouseListener(new ML());
        addMouseMotionListener(new MML());
    }
    public void update(Graphics g)
    {
        paint(g);
    }
    public void paint(Graphics g)
    {
        g.setColor(c);
        Dimension s = getSize();
        g.fillRect(0, 0, s.width, s.height);
        g.setColor(Color.black);
        display.show(g);
    }
    public void processEvent(AWTEvent e)
    {
        display.evnt[] = "component moved";
        repaint();
    }
    class CL implements ComponentListener
    {
        public void componentMoved(ComponentEvent e)
        {
            display.evnt[] = "component moved";
            repaint();
        }
        public void componentResized(ComponentEvent e)
        {
            display.evnt[] = "component resized";
            repaint();
        }
        public void componentHidden(ComponentEvent e)
        {
            display.evnt[] = "component hidden";
            repaint();
        }
        public void componentShown(ComponentEvent e)
        {
            display.evnt[] = "component shown";
            repaint();
        }
    }
    class FL implements FocusListener
    {
        public void focusGained(FocusEvent e)
        {
            display.evnt[Display.FOCUS] = "focus gained";
            repaint();
        }
        public void focusLost(FocusEvent e)
        {
            display.evnt[Display.FOCUS] = " focus lost";
            repaint();
        }
    }
    class KL implements KeyListener
    {
        public void keyPressed(KeyEvent e)
        {
            display.evnt[Display.KEY] = "key pressed";
            showCode(e);
        }
        public void keyReleased(KeyEvent e)
        {
            display.evnt[Display.KEY] = "key released";
            showCode(e);
        }
        public void keyTyped(KeyEvent e)
        {
            display.evnt[Display.KEY] = "key typed:";
            showCode(e);
        }
        void showCode(KeyEvent e)
        {
            int code = e.getKeyCode();
            display.evnt[Display.KEY] += KeyEvent.getKeyText(code);
            repaint();
        }
    }
    class ML implements MouseListener
    {
        public void mouseClicked(MouseEvent e)
        {
            requestFocus();
            display.evnt[Display.MOUSE] = "mouse clicked";
            showMouse(e);
        }
        public void mousePressed(MouseEvent e)
        {
            display.evnt[Display.MOUSE] = "mouse pressed";
            showMouse(e);
        }
        public void mouseReleased(MouseEvent e)
        {
            display.evnt[Display.MOUSE] = "mouse released";
            showMouse(e);
        }
        public void mouseEntered(MouseEvent e)
        {
            display.evnt[Display.MOUSE] = "mouse entered";
            showMouse(e);
        }
        public void mouseExited(MouseEvent e)
        {
            display.evnt[Display.MOUSE] = "mouse exit";
            showMouse(e);
        }
        void showMouse(MouseEvent e)
        {
            display.evnt[Display.MOUSE] +=
                    ", x = " + e.getX() +", y =" +e.getY();
            repaint();
        }
    }
    class MML implements MouseMotionListener
    {
        public void mouseDragged(MouseEvent e)
        {
            display.evnt[Display.MOUSE_MOVE] = "mouse dragged";
            showMouse(e);
        }
        public void mouseMoved(MouseEvent e)
        {
            display.evnt[Display.MOUSE_MOVE] = "mouse moved";
            showMouse(e);
        }
        void showMouse(MouseEvent e)
        {
            display.evnt[Display.MOUSE_MOVE] +=
                    ",x = "+e.getX()+
                    ",y = "+e.getY();
            repaint();
        }
    }
}
class MyButton extends Button
{
    int clickCounter;
    String label = "";
    public MyButton()
    {
        addActionListener(new AL())    ;
    }
    public void paint(Graphics g)
    {
        g.setColor(Color.green);
        Dimension s = getSize();
        g.fillRect(0, 0, s.width, s.height);
        g.setColor(Color.black);
        g.drawRect(0, 0, s.width - 1, s.height - 1);
        drawLabel(g);
    }
    private void drawLabel(Graphics g)
    {
        FontMetrics fm = g.getFontMetrics();
        int width = fm.stringWidth(label);
        int height = fm.getHeight();
        int ascent = fm.getAscent();
        int leading = fm.getLeading();
        int horizMargin = (getSize().width - width)/2;
        int verMargin = (getSize().height - height)/2;
        g.setColor(Color.red);
        g.drawString(label, horizMargin, verMargin+ascent+leading);
    }
    class AL implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            clickCounter++;
            label = "clice #"+clickCounter+" "+e.toString();
            repaint();
        }
    }
}
public class Hello extends Frame
{
    Hello()
    {
        setLayout(new GridLayout(2,2));
        add(new EnablePanel(1,Color.cyan));
        add(new EnablePanel(2,Color.lightGray));
        add(new EnablePanel(3,Color.yellow));
    }
    public static void main(String[] args)
    {
        Frame f = new Hello();
        f.setTitle("good technique");
        f.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                System.out.println(e);
                System.out.println("window closing");
                System.exit(0);
            }
        });
        f.setSize(700,700);
        f.setVisible(true);
    }
}






搜索更多相关主题的帖子: java public import class 
2011-10-21 22:07
刘杰明
Rank: 8Rank: 8
来 自:山东大学威海校区
等 级:蝙蝠侠
帖 子:155
专家分:872
注 册:2011-6-20
收藏
得分:20 
各种事件、隐藏显示,好歹说清楚哪句不明白啊?

______________________________加油__!__!___!
2011-10-28 10:14
c_icer
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:42
专家分:139
注 册:2011-11-21
收藏
得分:0 

技术成就梦想,细节决定成败……
2011-11-23 16:52
快速回复:java GUI程序!不明全部作用,求各部分解释,谢谢!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017554 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved