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

📄 rangebean.java~39~

📁 java bean 编程程序
💻 JAVA~39~
字号:
package chapter8;

import java.beans.*;
import java.io.*;

import java.awt.*;
import javax.swing.*;

public class RangeBean
    extends JPanel implements Serializable, VetoableChangeListener
{
    private IntValueBean fromBean = new IntValueBean();
    private IntValueBean toBean = new IntValueBean();
    private JLabel fromLbl = new JLabel();
    private JLabel toLbl = new JLabel();
    private int lblStyle = 0;
    private static final String[][] LBL_STRS =
        {
        {"from", "to"},
        {"从", "到"},
        {"", "~"}
    };
    public RangeBean()
    {
        try
        {
            jbInit();
        }
        catch (Exception exception)
        {
            exception.printStackTrace();
        }
    }

    private void jbInit()
        throws Exception
    {
        fromLbl.setText(LBL_STRS[lblStyle][0]);
        toLbl.setText(LBL_STRS[lblStyle][1]); toBean.setBgColor(Color.green);
        toBean.setIntValue(3); fromBean.
            setBgColor(Color.orange);
        this.add(fromLbl);
        this.add(fromBean);
        this.add(toLbl);
        this.add(toBean);
        //将RangeBean设置为fromBean和toBean的intValue值变化的否决监听器
        fromBean.addVetoableChangeListener(this);
        toBean.addVetoableChangeListener(this);
    }

    public Color getFromBgColor()
    {
        return fromBean.getBgColor();
    }

    public Color getToBgColor()
    {
        return toBean.getBgColor();
    }

    public void setFromBgColor(Color color)
    {
        fromBean.setBgColor(color);
    }

    public void setToBgColor(Color color)
    {
        toBean.setBgColor(color);
    }

    public int getFrom()
    {
        return fromBean.getValue();
    }

    public int getTo()
    {
        return toBean.getValue();
    }

    public void setFrom(int intValue)
        throws PropertyVetoException
    {
        fromBean.setIntValue(intValue);
    }

    public void setTo(int intValue)
        throws PropertyVetoException
    {
        toBean.setIntValue(intValue);
    }

    private void readObject(ObjectInputStream ois)
        throws IOException, ClassNotFoundException
    {
        ois.defaultReadObject();
    }

    private void writeObject(ObjectOutputStream oos)
        throws IOException
    {
        oos.defaultWriteObject();
    }

    //实现否决监听器接口函数
    public void vetoableChange(PropertyChangeEvent evt)
        throws PropertyVetoException
    {
        int value = ( (Integer) evt.getNewValue()).intValue();
        if (evt.getSource() == fromBean && value > toBean.getIntValue())
        {
            throw new PropertyVetoException("下限值大于上限值", evt);
        }
        if (evt.getSource() == toBean && value < fromBean.getIntValue())
        {
            throw new PropertyVetoException("上限值小于下限值", evt);
        }
    }

    public int getLblStyle()
    {
        return lblStyle;
    }

    public void setLblStyle(int lblStyle)
    {
        this.lblStyle = lblStyle;
        fromLbl.setText(LBL_STRS[lblStyle][0]);
        toLbl.setText(LBL_STRS[lblStyle][1]);
    }
}

⌨️ 快捷键说明

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