0
点赞
收藏
分享

微信扫一扫

swing-基础Event事件2

MaxWen 2022-03-13 阅读 32
java

概述

在swing-基础Event事件1 中 已经对swing的事件的结构进行了概括, 但是知识的理解要付出实践才可以让心中的疑惑解除 .
因为写了很多的h5代码了, 所以在对swing的组件进行事件操作时感觉很不顺手. 但是事件操作就是常见几种类型, 动手尝试一下就能找到答案


测试swing中事件

  • 点击事件(核心方法:addActionListener)
 controlBtn.addActionListener((e) -> {
            targetBtn.setVisible(!targetBtn.isVisible());
        });
  • 显影事件( 核心方法:addAncestorListener)
      targetBtn.addAncestorListener(new AncestorListener() {
            @Override
            public void ancestorAdded(AncestorEvent event) {
                displayRecord.setText("panel show");
            }

            @Override
            public void ancestorRemoved(AncestorEvent event) {
                displayRecord.setText("panel hide");

            }

            @Override
            public void ancestorMoved(AncestorEvent event) {
                displayRecord.setText("panel move");
            }
        });

  • 输入事件( 核心方法:addKeyListener)
inputText.addKeyListener(new KeyAdapter() {
         @Override
         public void keyPressed(KeyEvent e) {
             inputRecord.setText("最后一次按键:" + e.getKeyCode());
         }
     });

  • 焦点事件( 核心方法:addFocusListener)
focusText.addFocusListener(new FocusAdapter() {
          @Override
          public void focusGained(FocusEvent e) {
              focusRecord.setText("获得焦点");
          }

          @Override
          public void focusLost(FocusEvent e) {
              focusRecord.setText("失去焦点");
          }
      });

  • 光标事件( 核心方法:getCaret().addChangeListener)
  /* 光标事件*/
      caretText.getCaret().addChangeListener(new ChangeListener() {
          @Override
          public void stateChanged(ChangeEvent e) {
              caretRecord.setText("光标位置变化" + caretText.getCaret().getDot());
          }
      });

  • 下拉事件( 核心方法:addItemListener)
 selectBox.addItemListener(new ItemListener() {
          @Override
          public void itemStateChanged(ItemEvent e) {
              selectRecord.setText("下拉框选择:" + e.getItem());
          }
      });

  • 拖拽事件( 核心方法:addMouseListener addMouseMotionListener)
 /* 拖拽事件*/
        dragBtn.addMouseListener(dragListener);
        dragBtn.addMouseMotionListener(dragListener);



当然后续的窗口事件,菜单事件,表格事件,树事件等自己需要用到的时候再测试把

gif 代码示例

@Slf4j
public class ElementEventTab extends AbstractView {
    private JButton controlBtn;
    private JButton targetBtn;
    private JLabel displayRecord;

    private JButton hoverBtn;
    private Color defaultBtnColor;
    private Color redBtnColor;
    private JLabel hoverRecord;

    private JTextField inputText;
    private JLabel inputRecord;


    private JTextField focusText;
    private JLabel focusRecord;


    private JTextField caretText;
    private JLabel caretRecord;

    private JComboBox selectBox;
    private JLabel selectRecord;


    private JButton dragBtn;
    private JLabel dragText;
    private DragListerner dragListener;


    @Override
    public void init() {
        controlBtn = new JButton("control");
        targetBtn = new JButton("hide/show");
        displayRecord = new JLabel();

        hoverBtn = new JButton("hover");
        hoverRecord = new JLabel();
        defaultBtnColor = hoverBtn.getBackground();
        redBtnColor = Color.RED;


        inputText = new JTextField();
        inputRecord = new JLabel();

        focusText = new JTextField();
        focusRecord = new JLabel();


        caretText = new JTextField("12345");
        caretRecord = new JLabel();

        String[] items = {"all", "first", "second", "three"};
        selectBox = new JComboBox(items);
        selectRecord = new JLabel();

        dragListener = new DragListerner();
        dragBtn = new JButton("dragBtn");
        dragText = new JLabel();


    }

    /**
     * render视图
     */
    @Override
    protected void render() {
        view.setLayout(new MigLayout("wrap 1"));
        createViewRow(new JLabel("组件显隐事件:"), controlBtn, targetBtn, displayRecord);

        createViewRow(new JLabel("组件悬浮事件:"), hoverBtn, hoverRecord);
        createViewRow(new JLabel("组件输入事件:"), inputText, inputRecord);

        createViewRow(new JLabel("组件焦点事件:"), focusText, focusRecord);
        createViewRow(new JLabel("组件光标事件:"), caretText, caretRecord);
        createViewRow(new JLabel("组件下拉事件:"), selectBox, selectRecord);

        createViewRow(new JLabel("组件拖拽事件:"), dragBtn);
        super.add(view, "w 100%,h 100%");
    }

    /**
     * 绑定事件
     */
    @Override
    public void bindEvents() {

        /*显示隐藏事件*/
        controlBtn.addActionListener((e) -> {
            targetBtn.setVisible(!targetBtn.isVisible());
        });
        targetBtn.addAncestorListener(new AncestorListener() {
            @Override
            public void ancestorAdded(AncestorEvent event) {
                displayRecord.setText("panel show");
            }

            @Override
            public void ancestorRemoved(AncestorEvent event) {
                displayRecord.setText("panel hide");

            }

            @Override
            public void ancestorMoved(AncestorEvent event) {
                displayRecord.setText("panel move");
            }
        });


        /* 悬浮事件*/
        hoverBtn.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseExited(MouseEvent e) {
                hoverBtn.setBackground(defaultBtnColor);
                hoverRecord.setText("鼠标离开");
            }
        });

        hoverBtn.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                hoverBtn.setBackground(redBtnColor);
                hoverRecord.setText("鼠标进入");
            }
        });

        /*键盘事件 中文输入法下不好使*/
        inputText.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                inputRecord.setText("最后一次按键:" + e.getKeyCode());
            }
        });

        /* 焦点事件*/
        focusText.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent e) {
                focusRecord.setText("获得焦点");
            }

            @Override
            public void focusLost(FocusEvent e) {
                focusRecord.setText("失去焦点");
            }
        });

        /* 光标事件*/
        caretText.getCaret().addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                caretRecord.setText("光标位置变化" + caretText.getCaret().getDot());
            }
        });

        /* 选择事件*/
        selectBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                selectRecord.setText("下拉框选择:" + e.getItem());
            }
        });

        /* 拖拽事件*/
        dragBtn.addMouseListener(dragListener);
        dragBtn.addMouseMotionListener(dragListener);


    }

    public static void main(String[] args) {
        FlatLightLaf.install();
        FrameUtil.launchTest(new ElementEventTab());
    }

    /**
     * 拖拽
     */
    private class DragListerner extends MouseAdapter
            implements MouseMotionListener {
        Point press = new Point();
        boolean dragging = false;

        public void mousePressed(MouseEvent event) {
            press.x = event.getX();
            press.y = event.getY();
            dragging = true;
        }

        public boolean isDragging() {
            return dragging;
        }

        public void mouseReleased(MouseEvent event) {
            dragging = false;
        }

        public void mouseClicked(MouseEvent event) {
            dragging = false;
        }

        public void mouseMoved(MouseEvent event) {
        }

        public void mouseDragged(MouseEvent event) {
            Component c = (Component) event.getSource();
            if (dragging) {
                Point loc = c.getLocation();
                Point pt = new Point();
                pt.x = event.getX() + loc.x - press.x;
                pt.y = event.getY() + loc.y - press.y;
                c.setLocation(pt.x, pt.y);
                c.getParent().repaint();
            }
        }

    }
}
举报

相关推荐

swing-组件测试实现模态框

swing-组件tooltip测试及美化

swing-组件Collapse折叠面板

事件(Event)

Event事件对象

C# 事件(Event)

0 条评论