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

📄 groupbutton.java

📁 打印管理程序,测试完全通过.windows开发环境.
💻 JAVA
字号:
package jp.co.ntl.swing.ext;

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

import javax.swing.JComponent;

public abstract class GroupButton extends JComponent implements MouseListener, MouseMotionListener {

    private ButtonGroup group;

    private String actionCommand;
    protected boolean pushed = false;
    protected boolean toggle = true;

    private transient ActionListener actionListener;

    public GroupButton() {
        this(null);
    }

    public GroupButton(ButtonGroup group) {
        this.group = group;
   		if (group != null) {
   		    group.add(this);
   		}
   		addMouseListener(this);
   		addMouseMotionListener(this);
    }

    public void setPushed(boolean pushed) {
        this.pushed = pushed;
        repaint();
    }

    public boolean getPushed() {
        return pushed;
    } 

    public void setToggle(boolean toggle) {
        this.toggle = toggle;
        repaint();
    }

    public boolean getToggle() {
        return toggle;
    }

    public void setActionCommand(String command) {
        actionCommand = command;
    }

    public String getActionCommand() {
        return actionCommand;
    }

    public synchronized void addActionListener(ActionListener l) {
		if (l == null) {
		    return;
		}
		actionListener = AWTEventMulticaster.add(actionListener, l);
    }

    public synchronized void removeActionListener(ActionListener l) {
		if (l == null) {
		    return;
		}
		actionListener = AWTEventMulticaster.remove(actionListener, l);
    }

    protected void processEvent(AWTEvent e) {
        if (e instanceof ActionEvent) {
            processActionEvent((ActionEvent)e);
            return;
        }
		super.processEvent(e);
    }

    protected void processActionEvent(ActionEvent e) {
        if (actionListener != null) {
            actionListener.actionPerformed(e);
        }
    }

    public void mousePressed(MouseEvent me) {
        if (!isEnabled()) {
            return;
        }
        if (group != null && pushed) {
            return;
        }
        if (toggle) {
            pushed = !pushed;
            processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, getActionCommand()));
        } else {
            pushed = true;
        }
        requestFocus();
        repaint();
    }

    public void mouseReleased(MouseEvent me) {
        if (!isEnabled()) {
            return;
        }
        if (!toggle && pushed && contains(me.getPoint())) {
            pushed = false;
            repaint();
            processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, getActionCommand()));
        }
    }

    public void mouseClicked(MouseEvent me) {
        if (!isEnabled()) {
            return;
        }
    }

    public void mouseEntered(MouseEvent me) {
        if (!isEnabled()) {
            return;
        }
    }

    public void mouseExited(MouseEvent me) {
        if (!isEnabled()) {
            return;
        }
    }

    public void mouseMoved(MouseEvent me) {
    }

    public void mouseDragged(MouseEvent me) {
        if (!toggle && pushed && !contains(me.getPoint())) {
            pushed = false;
            repaint();
        }
    }
}

⌨️ 快捷键说明

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