⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 button.java

📁 Java 范例实战 光盘使用说明 ========================== 本光盘的文件结构如下所示: =====================================
💻 JAVA
字号:
import java.applet.Applet;
import java.applet.AppletContext;
import java.awt.*;
import java.net.MalformedURLException;
import java.net.URL;

public class button extends Applet
    implements Runnable
{

    int w = 0;
    int h = 0;
    int start = 0;
    Image offImage = null;
    Graphics offGraphics = null;
    Dimension offDimension = null;
    Color color = null;
    int num_buttons = 0;
    int direction = 0;
    int button_x[] = null;
    int button_y[] = null;
    int button_w = 0;
    int button_h = 0;
    int degree = 0;
    int with = 0;
    String label[] = null;
    String link[] = null;
    String frame[] = null;
    Color b_clr_light[] = null;
    Color b_clr_dark[] = null;
    int f_size = 0;
    int f_style = 0;
    int font_x[] = null;
    int font_y[] = null;
    int f_offset = 0;
    Color f_color = null;
    Color f_color2 = null;
    Font font1 = null;
    int selected = 0;
    int last = 0;
    int mouse_in = 0;
    int style = 0;
    int position = 0;
    int position_is[] = null;
    int position_be[] = null;
    int down = 0;
    int image_w[] = null;
    int image_h[] = null;
    int image_x[] = null;
    int image_y[] = null;
    Image sourceimage[] = null;

    public void start()
    {
        repaint();
    }

    public void paintFrame(Graphics g)
    {
        g.setFont(font1);
        for(int a = 0; a < num_buttons; a++)
            if(position_be[a] != position_is[a])
            {
                int dwn = 0;
                position_is[a] = position_be[a];
                if(position_be[a] == 1)
                    dwn = 1;
                g.setColor(color);
                g.fillRect(button_x[a], button_y[a], button_w, button_h);
                if(dwn == 1)
                {
                    g.drawImage(sourceimage[a], image_x[a], image_y[a], color, this);
                    g.setColor(f_color);
                    g.drawString(label[a], font_x[a], font_y[a]);
                }
                else
                {
                    g.drawImage(sourceimage[a], image_x[a] + 1, image_y[a] + 1, color, this);
                    g.setColor(f_color2);
                    g.drawString(label[a], font_x[a] + 1, font_y[a] + 1);
                }
                for(int b = 0; b < with; b++)
                {
                    if(dwn == 0)
                        g.setColor(b_clr_dark[with - b]);
                    else
                        g.setColor(b_clr_light[with - b]);
                    g.drawLine(button_x[a] + b, button_y[a] + b, (button_x[a] + button_w) - b, button_y[a] + b);
                    g.drawLine(button_x[a] + b, button_y[a] + b, button_x[a] + b, (button_y[a] + button_h) - b);
                    if(dwn == 1)
                        g.setColor(b_clr_dark[with - b]);
                    else
                        g.setColor(b_clr_light[with - b]);
                    g.drawLine(button_x[a] + b, (button_y[a] + button_h) - b, (button_x[a] + button_w) - b, (button_y[a] + button_h) - b);
                    g.drawLine((button_x[a] + button_w) - b, (button_y[a] + button_h) - b, (button_x[a] + button_w) - b, button_y[a] + b);
                }

            }
            else
            if(style == 0)
                if(a + 1 == mouse_in)
                {
                    g.setColor(color);
                    g.setColor(f_color2);
                    g.drawString(label[a], font_x[a], font_y[a]);
                }
                else
                {
                    g.setColor(color);
                    g.setColor(f_color);
                    g.drawString(label[a], font_x[a], font_y[a]);
                }

    }

    void loadimage(Image img, int btn)
    {
        MediaTracker mt = new MediaTracker(this);
        mt.addImage(img, 0);
        try
        {
            mt.waitForID(0);
        }
        catch(InterruptedException interruptedexception) { }
    }

    public boolean mouseExit(Event e, int x, int y)
    {
        for(int b = 0; b < num_buttons; b++)
            position_be[b] = 1;

        down = 0;
        mouse_in = 0;
        repaint();
        return true;
    }

    public boolean mouseMove(Event e, int x, int y)
    {
        for(int a = 0; a < num_buttons; a++)
        {
            if(x < button_x[a] || x >= button_x[a] + button_w || y < button_y[a] || y >= button_y[a] + button_h)
                continue;
            mouse_in = a + 1;
            if((style == 1) & (down == 0))
            {
                for(int b = 0; b < num_buttons; b++)
                    position_be[b] = 1;

                position_be[a] = 0;
            }
            break;
        }

        repaint();
        return true;
    }

    public boolean mouseDown(Event e, int x, int y)
    {
        for(int a = 0; a < num_buttons; a++)
        {
            if(x < button_x[a] || x >= button_x[a] + button_w || y < button_y[a] || y >= button_y[a] + button_h)
                continue;
            down = 1;
            for(int b = 0; b < num_buttons; b++)
                position_be[b] = 1;

            position_be[a] = 0;
            break;
        }

        repaint();
        return true;
    }

    public boolean mouseUp(Event e, int x, int y)
    {
        for(int a = 0; a < num_buttons; a++)
        {
            if(x < button_x[a] || x >= button_x[a] + button_w || y < button_y[a] || y >= button_y[a] + button_h)
                continue;
            down = 0;
            if(style == 0)
            {
                for(int b = 0; b < num_buttons; b++)
                    position_be[b] = 1;

            }
            if(!link[a].equalsIgnoreCase(""))
                try
                {
                    URL base_url = getDocumentBase();
                    URL link_to = new URL(base_url, link[a]);
                    getAppletContext().showDocument(link_to, frame[a]);
                }
                catch(MalformedURLException malformedurlexception) { }
            break;
        }

        repaint();
        return true;
    }

    public void run()
    {
    }

    public button()
    {
        color = new Color(0, 0, 0);
        num_buttons = 1;
        degree = 20;
        with = 1;
        f_size = 12;
        f_offset = 1;
    }

    public void init()
    {
        w = size().width;
        h = size().height;
        try
        {
            num_buttons = Integer.parseInt(getParameter("buttons"));
        }
        catch(Exception e) { }
        try
        {
            direction = Integer.parseInt(getParameter("direction"));
        }
        catch(Exception e) { }
        try
        {
            with = Integer.parseInt(getParameter("border width"));
        }
        catch(Exception e) { }
        try
        {
            position = Integer.parseInt(getParameter("position"));
        }
        catch(Exception e) { }
        try
        {
            degree = Integer.parseInt(getParameter("degree"));
        }
        catch(Exception e) { }
        try
        {
            f_offset = Integer.parseInt(getParameter("f_offset"));
        }
        catch(Exception e) { }
        try
        {
            this.style = Integer.parseInt(getParameter("style"));
        }
        catch(Exception e) { }
        String temp_color = getParameter("color");
        if(temp_color == null)
            temp_color = "000000";
        color = new Color(Integer.parseInt(temp_color, 16));
        try
        {
            f_size = Integer.parseInt(getParameter("f_size"));
        }
        catch(Exception e) { }
        try
        {
            f_style = Integer.parseInt(getParameter("f_style"));
        }
        catch(Exception exception) { }
        String my_font = getParameter("font");
        if(my_font == null)
            my_font = "Helvetica";
        String temp_f_color = getParameter("f_color");
        if(temp_f_color == null)
            temp_f_color = "FFFFFF";
        f_color = new Color(Integer.parseInt(temp_f_color, 16));
        temp_f_color = getParameter("f_color2");
        if(temp_f_color == null)
            temp_f_color = "FFFFFF";
        f_color2 = new Color(Integer.parseInt(temp_f_color, 16));
        image_w = new int[num_buttons];
        image_h = new int[num_buttons];
        image_x = new int[num_buttons];
        image_y = new int[num_buttons];
        position_is = new int[num_buttons];
        position_be = new int[num_buttons];
        sourceimage = new Image[num_buttons];
        link = new String[num_buttons];
        label = new String[num_buttons];
        frame = new String[num_buttons];
        for(int a = 0; a < num_buttons; a++)
        {
            position_be[a] = 1;
            position_is[a] = 0;
            image_w[a] = 0;
            image_h[a] = 0;
            image_x[a] = 0;
            image_y[a] = 0;
            label[a] = getParameter("label " + Integer.toString(a));
            if(label[a] == null)
                label[a] = "";
            link[a] = getParameter("link " + Integer.toString(a));
            if(link[a] == null)
                link[a] = "";
            String imagename = getParameter("image " + Integer.toString(a));
            if(imagename == null)
                imagename = "";
            frame[a] = getParameter("frame " + Integer.toString(a));
            if(frame[a] == null)
                frame[a] = "_parent";
            sourceimage[a] = getImage(getCodeBase(), imagename);
            loadimage(sourceimage[a], a);
            image_w[a] = sourceimage[a].getWidth(this);
            image_h[a] = sourceimage[a].getHeight(this);
        }

        button_x = new int[num_buttons];
        button_y = new int[num_buttons];
        if(direction == 0)
        {
            button_w = w - 1;
            button_h = h / num_buttons;
            for(int a = 0; a < num_buttons; a++)
            {
                button_x[a] = 0;
                button_y[a] = button_h * a;
            }

        }
        else
        {
            button_h = h - 1;
            button_w = w / num_buttons;
            for(int a = 0; a < num_buttons; a++)
            {
                button_y[a] = 0;
                button_x[a] = button_w * a;
            }

        }
        int r = color.getRed();
        int g = color.getGreen();
        int b = color.getBlue();
        b_clr_dark = new Color[with + 1];
        b_clr_dark[0] = color;
        b_clr_light = new Color[with + 1];
        b_clr_light[0] = color;
        for(int a = 1; a < with + 1; a++)
        {
            b_clr_dark[a] = new Color(Math.max(0, r - a * degree), Math.max(0, g - a * degree), Math.max(0, b - a * degree));
            b_clr_light[a] = new Color(Math.min(255, r + a * degree), Math.min(255, g + a * degree), Math.min(255, b + a * degree));
        }

        int style = 0;
        if(f_style == 1)
            style = 1;
        else
        if(f_style == 2)
            style = 2;
        font_x = new int[num_buttons];
        font_y = new int[num_buttons];
        font1 = new Font(my_font, style, f_size);
        setFont(font1);
        FontMetrics fm = getFontMetrics(font1);
        for(int a = 0; a < num_buttons; a++)
        {
            image_x[a] = button_x[a] + (button_w - image_w[a]) / 2;
            image_y[a] = button_y[a] + (button_h - image_h[a]) / 2;
            font_x[a] = button_x[a] + (button_w - fm.stringWidth(label[a])) / 2;
            font_y[a] = button_y[a] + (button_h / 2 + fm.getHeight() / 3);
        }

    }

    public void update(Graphics g)
    {
        Dimension dim = size();
        if(offGraphics == null || dim.width != offDimension.width || dim.height != offDimension.height)
        {
            offDimension = dim;
            offImage = createImage(dim.width, dim.height);
            offGraphics = offImage.getGraphics();
            offGraphics.setColor(color);
            offGraphics.fillRect(0, 0, w, h);
        }
        paintFrame(offGraphics);
        g.drawImage(offImage, 0, 0, null);
    }

    public void paint(Graphics g)
    {
        if(offImage != null)
            g.drawImage(offImage, 0, 0, null);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -