?? circlenode.java
字號:
package treeandbtreedemo;
/**
* <p>Title:節點的圖像,用了一個JLable的表示 </p>
* <p>Description:節點的圖形化顯示 ,用了一個JLable的表示,有事件的監聽??赏蟿印?lt;/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(false); //可見
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);
}
public void setfill(boolean f) {
fill = f;
repaint();
}
/**鼠標移動事件*/
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 + -