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

📄 rangebeancustomizer.java

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

import java.awt.*;
import javax.swing.*;
import java.beans.Customizer;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.beans.PropertyVetoException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RangeBeanCustomizer
    extends JPanel implements Customizer
{
    JLabel jLabel1 = new JLabel();
    JTextField fromFld = new JTextField();
    JLabel jLabel2 = new JLabel();
    JTextField toFld = new JTextField();
    JLabel jLabel3 = new JLabel();
    String[] lblStyles =
        {"英文标签", "中文标签", "数学标签"};
    JComboBox lblStyleCmbox = new JComboBox(lblStyles);
    JButton okBtn = new JButton();
    JButton cancelBtn = new JButton();
    private transient PropertyChangeSupport pcs = new
        PropertyChangeSupport(this);
    private RangeBean oldRangeBean;
    private int oldFrom;
    private int oldTo;
    private int oldLblStyle;
    GridBagLayout gridBagLayout1 = new GridBagLayout();

    public RangeBeanCustomizer()
    {
        try
        {
            jbInit();
        }
        catch (Exception exception)
        {
            exception.printStackTrace();
        }
    }

    private void jbInit()
        throws Exception
    {
        this.setLayout(gridBagLayout1);
        jLabel1.setText("下限值");
        jLabel2.setText("上限值");
        jLabel3.setText("标签样式");
        okBtn.setText("确定"); okBtn.addActionListener(new
            RangeBeanCustomizer_okBtn_actionAdapter(this));
        cancelBtn.setText("取消"); cancelBtn.addActionListener(new
            RangeBeanCustomizer_cancelBtn_actionAdapter(this));
        this.add(jLabel2,
                 new GridBagConstraints(2, 0, 1,
                                        1, 0.0, 0.0
                                        , GridBagConstraints.WEST,
                                        GridBagConstraints.NONE,
                                        new Insets(23, 16, 0, 0), 0, 0)); this.
            add(jLabel3,
                new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
                                       , GridBagConstraints.WEST,
                                       GridBagConstraints.NONE,
                                       new Insets(21, 57, 0, 0), 0, 0)); this.
            add(
                jLabel1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
                                                , GridBagConstraints.WEST,
                                                GridBagConstraints.NONE,
                                                new Insets(23, 65, 0, 0), 0, 0)); this.
            add(fromFld,
                new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0
                                       , GridBagConstraints.WEST,
                                       GridBagConstraints.HORIZONTAL,
                                       new Insets(23, 0, 0, 0), 60, 0)); this.
            add(
                toFld, new GridBagConstraints(3, 0, 1, 1, 1.0, 0.0
                                              , GridBagConstraints.WEST,
                                              GridBagConstraints.HORIZONTAL,
                                              new Insets(23, 16, 0, 107), 58, 0)); this.
            add(lblStyleCmbox, new GridBagConstraints(1, 1, 3, 1, 1.0, 0.0
            , GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
            new Insets(15, 0, 0, 107), 119, 0)); this.add(cancelBtn,
            new GridBagConstraints(2, 2, 2, 1, 0.0, 0.0
                                   , GridBagConstraints.CENTER,
                                   GridBagConstraints.NONE,
                                   new Insets(17, 22, 22, 140), 0, 0)); this.
            add(okBtn, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0
                                              , GridBagConstraints.CENTER,
                                              GridBagConstraints.NONE,
                                              new Insets(17, 11, 22, 0), 9, 0));
    }

    public void setObject(Object bean)
    {
        if (bean instanceof RangeBean)
        {
            oldRangeBean = (RangeBean) bean;
            oldFrom = oldRangeBean.getFrom();
            oldTo = oldRangeBean.getTo();
            oldLblStyle = oldRangeBean.getLblStyle();
            fromFld.setText("" + oldFrom);
            toFld.setText("" + oldTo);
            lblStyleCmbox.setSelectedIndex(oldLblStyle);
        }
    }

    public synchronized void addPropertyChangeListener(PropertyChangeListener l)
    {
        super.addPropertyChangeListener(l);
        if (pcs == null)
        {
            pcs = new PropertyChangeSupport(this);
        }
        pcs.addPropertyChangeListener(l);
    }

    public synchronized void removePropertyChangeListener(
        PropertyChangeListener l)
    {
        super.removePropertyChangeListener(l);
        if (pcs == null)
        {
            pcs = new PropertyChangeSupport(this);
        }
        pcs.removePropertyChangeListener(l);
    }

    //更新from的值
    private void changeFromProperty(Integer newFrom)
    {
        Integer currentFrom = new Integer(oldRangeBean.getFrom());
        try
        {
            oldRangeBean.setFrom(newFrom.intValue());
        }
        catch (PropertyVetoException ex)
        {
            ex.printStackTrace();
        }
        if (pcs != null)
        {
            pcs.firePropertyChange("from", currentFrom, newFrom);
        }
    }

    //更新to的值
    private void changeToProperty(Integer newTo)
    {
        Integer currentTo = new Integer(oldRangeBean.getTo());
        try
        {
            oldRangeBean.setTo(newTo.intValue());
        }
        catch (PropertyVetoException ex)
        {
            ex.printStackTrace();
        }
        if (pcs != null)
        {
            pcs.firePropertyChange("to", currentTo, newTo);
        }
    }

    //更新lblStyle的值
    private void changeLblStyleProperty(Integer newLblStyle)
    {
        Integer currentLblStyle = new Integer(oldRangeBean.getLblStyle());
        oldRangeBean.setLblStyle(newLblStyle.intValue());
        if (pcs != null)
        {
            pcs.firePropertyChange("lblStyle", currentLblStyle, newLblStyle);
        }
    }

    public void okBtn_actionPerformed(ActionEvent e)
    {
        try
        {
            int newFrom = (new Integer(fromFld.getText())).intValue();
            int newTo = (new Integer(toFld.getText())).intValue();
            int newLblStyle = lblStyleCmbox.getSelectedIndex();
            if (newFrom > newTo)
            {
                JOptionPane.showMessageDialog(this, "下限值必须小于等于上限值", "值设置错误",
                                              JOptionPane.OK_OPTION);
                return;
            }
            changeFromProperty(new Integer(newFrom));
            changeToProperty(new Integer(newTo));
            changeLblStyleProperty(new Integer(newLblStyle));
        }
        catch (NumberFormatException ex)
        {
            JOptionPane.showMessageDialog(this, "上下限值必须是整数型值", "值设置错误",
                                          JOptionPane.OK_OPTION);
            return;
        }
    }

    //返回原来的值
    public void cancelBtn_actionPerformed(ActionEvent e)
    {
        fromFld.setText("" + oldFrom);
        toFld.setText("" + oldTo);
        lblStyleCmbox.setSelectedIndex(oldLblStyle);
    }
}

class RangeBeanCustomizer_cancelBtn_actionAdapter
    implements ActionListener
{
    private RangeBeanCustomizer adaptee;
    RangeBeanCustomizer_cancelBtn_actionAdapter(RangeBeanCustomizer adaptee)
    {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e)
    {
        adaptee.cancelBtn_actionPerformed(e);
    }
}

class RangeBeanCustomizer_okBtn_actionAdapter
    implements ActionListener
{
    private RangeBeanCustomizer adaptee;
    RangeBeanCustomizer_okBtn_actionAdapter(RangeBeanCustomizer adaptee)
    {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e)
    {
        adaptee.okBtn_actionPerformed(e);
    }
}

⌨️ 快捷键说明

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