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

📄 circlenode.java~2~

📁 源程序(包括最初的版本
💻 JAVA~2~
字号:
package treeandbtreedemo;

/**
 * <p>Title:节点的图像,用了一个JLable的表示 </p>
 * <p>Description:节点的图行显示 ,用了一个JLable的表示</p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 3.0(2005.7.8)
 */
/**
 * <p>Title:节点的图像,用了一个JLable的表示 </p>
 * <p>Description:节点的图行显示 ,用了一个JLable的表示</p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 3.0(2005.7.8)
 */
import java.awt.*;
import java.awt.Dimension;
import javax.swing.*;
import java.awt.event.*;


public class CircleNode extends JLabel{
  String name;//节点名字
  //int color;//颜色
  int radius;//半径
  boolean fill;//填充
  //JLabel j=new JLabel();


  public CircleNode() { }
  public CircleNode(String name,int r,boolean fill){
    this.name=name; radius=r; this.fill=fill;
    //paintComponent(this.getGraphics());
    this.setPreferredSize(new Dimension( radius*2+2,radius*2+ 2));
    this.setBackground(Color.white);//背景色
    this.setOpaque(true);//可见
    this.addMouseMotionListener(new MyLabel_mouseMotionAdapter(this));
    this.addMouseListener(new MyLabel_mouseAdapter(this));
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);


    g.setColor(Color.RED);
    g.drawOval(0,0,radius*2,radius*2);//外框

    if(fill==false){//如果不需要填充颜色
      g.setColor(Color.WHITE);
      g.fillOval(1, 1, radius*2 - 1, radius*2 - 1);//填白色
    }
    else{//需要填充颜色
      g.setColor(Color.yellow);
      g.fillOval(1, 1, radius*2 - 1, radius*2 - 1);//填yellow色
    }

    g.setColor(Color.BLACK);
    g.setFont(new Font("dialog",0,13));
    g.drawString(name,7,14);//文字

  }

  class MyLabel_mouseMotionAdapter extends java.awt.event.MouseMotionAdapter {
  JLabel adaptee;

  MyLabel_mouseMotionAdapter(JLabel adaptee) {
    this.adaptee = adaptee;
  }
  public void mouseDragged(MouseEvent e) {
    adaptee.setLocation(adaptee.getX() + e.getX() - adaptee.getWidth() / 2,
                  adaptee.getY() + e.getY() - adaptee.getHeight() / 2);
    adaptee.getParent().repaint();
  }
}

class MyLabel_mouseAdapter extends java.awt.event.MouseAdapter {
  JLabel adaptee;

  MyLabel_mouseAdapter(JLabel adaptee) {
    this.adaptee = adaptee;
  }
  public void mousePressed(MouseEvent e) {
    adaptee.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
  }
  public void mouseReleased(MouseEvent e) {
    adaptee.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
  }
}

}

⌨️ 快捷键说明

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