📄 bean.java
字号:
package com.wiziflow.gui;import javax.swing.*;import java.awt.event.*;import java.awt.*;import java.awt.font.*;import java.util.*;/** * 活动图标。定义“活动”中的所有属性 */public class Bean extends JLabel { //private ImageIcon icon = new ImageIcon(com.wiziflow.gui.StartBean.class.getResource("images/activity.gif")); private LineBean line = null; private Vector vector = new Vector(); private int mouseX=0;//鼠标移动的前一个位置的x坐标 private int mouseY=0;//鼠标移动的前一个位置的y坐标 private boolean lineMouseExit=false;//当鼠标移出控件时开始画 public Bean() { try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } private void jbInit() throws Exception { //this.setIcon(icon); this.setFont(new Font("宋体",0,12)); this.setSize(50,65);//图标大小 this.setVerticalTextPosition(JLabel.BOTTOM); this.setHorizontalAlignment(JLabel.CENTER);//全部居中 this.setHorizontalTextPosition(JLabel.CENTER); this.addKeyListener(new java.awt.event.KeyAdapter() { public void keyReleased(KeyEvent e) { this_keyReleased(e); } }); this.addFocusListener(new java.awt.event.FocusAdapter() { public void focusGained(FocusEvent e) { this_focusGained(e); } public void focusLost(FocusEvent e) { this_focusLost(e); } }); this.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(MouseEvent e) { this_mousePressed(e); } public void mouseEntered(MouseEvent e) { this_mouseEntered(e); } public void mouseExited(MouseEvent e) { this_mouseExited(e); } public void mouseReleased(MouseEvent e) { this_mouseReleased(e); } }); this.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { this_mouseDragged(e); } }); } void this_mouseDragged(MouseEvent e) { Point p = this.getLocation(); int x=(int)p.getX(); int y=(int)p.getY(); if(Mouse.STATE==Mouse.LINE ) {//画线模式 //鼠标拖动,移出图标区时开始画线,传递坐标值为相对startBean的坐标 if(lineMouseExit) { line.firePropertyChange("x",e.getY(),e.getX()); } return; } if(Mouse.STATE!=Mouse.LINE && line!=null) { line.firePropertyChange("x",e.getY(),e.getX()); } this.setLocation(x+(e.getX()-mouseX),y+(e.getY()-mouseY)); } void this_mousePressed(MouseEvent e) { if(Mouse.STATE==Mouse.LINE) {//画线模式 //添加LineBean到FlowPane,但不进行任何计算 line = new LineBean(); ((JLayeredPane)getParent()).add(line); ((JLayeredPane)getParent()).moveToBack(line); line.setStartComponent(this); vector.add(line); return; } mouseX=e.getX(); mouseY=e.getY(); this.requestFocus(); //如果是左键双击,并且鼠标正常状态 //双击图标弹出属性编辑框 //双击文字弹出文字编辑框 if(e.getClickCount()==2 && SwingUtilities.isLeftMouseButton(e)){ //this.add(new javax.swing.JTextField()); //this.setText("哈哈阿瑟扩大军方"); showPropertyDialog(); } } void this_focusGained(FocusEvent e) { ((JLayeredPane)getParent()).moveToFront(this); this.setBorder(BorderFactory.createLineBorder(Color.blue)); } void this_focusLost(FocusEvent e) { //this.setSize(icon.getIconWidth()+getText().length(),icon.getIconHeight()+35); this.setBorder(null); } void this_keyReleased(KeyEvent e) { if(e.getKeyCode()==127) {//Delete键 JLabel message=new JLabel("删除该控件和它的所有关系,确定吗?"); message.setFont(new java.awt.Font("宋体", 0, 12)); int i=JOptionPane.showConfirmDialog(null,message,"删除",JOptionPane.OK_CANCEL_OPTION); if(i==0) {//确定 //为什么要用super //是否因为加入控件时,都是Bean的子类? JLayeredPane flowPane = (JLayeredPane)getParent(); //删除线 if(!vector.isEmpty()){ Iterator iter = vector.iterator(); while(iter.hasNext()) { LineBean lineBean = (LineBean)iter.next(); flowPane.remove(lineBean); } } //flowPane.getComponentAt(line);//删除连接线 //line=null;// flowPane.removeAll(); flowPane.remove(this); flowPane.repaint();// this.removeAll(); } } } void this_mouseEntered(MouseEvent e) { if(Mouse.STATE==Mouse.LINE){//画线模式 this.setBorder(BorderFactory.createLineBorder(Color.red)); lineMouseExit=false; } } void this_mouseExited(MouseEvent e) { if(Mouse.STATE==Mouse.LINE){//画线模式 this.setBorder(null); lineMouseExit=true; } } //鼠标释放才表明画线结束。如果鼠标释放时,没有在另一个Bean上面,表明画线失败 void this_mouseReleased(MouseEvent e) { //e.getX是相对于事件源坐标而言,所以要转换成绝对坐标 if(Mouse.STATE == Mouse.LINE) { JLayeredPane flowPane = (JLayeredPane)getParent(); Bean bean = null; try{ bean = (Bean)flowPane.findComponentAt(e.getX()+this.getX(),e.getY()+this.getY()); }catch(Exception ex) { } if(bean!=null) { //System.out.println( bean.getText()); //判断鼠标时是否是同一个对象。如果是同一个对象,也就没有意义,建议FlowPane去掉LineBean对象 if(this.hashCode()==bean.hashCode()) {//根据hashCode能判断出来吗? line.remove(); }else{ //不是同一个对象 line.setEndComponent(bean); //线画完之后,它既是起点Bean的,也是终点Bean的 //但我实在想不通,好多线交会在一个bean的时候,居然行的通。 bean.line = line; bean.vector.add(line); line.repaint(); } }else{ //如果是null,没有意义,建议FlowPane去掉LineBean对象 //System.out.println("null"); line.remove(); } } } //弹出属性对话框,需要继承的类予以改写 protected void showPropertyDialog(){ System.out.println("showPropertyDialog()"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -