结果程序运行出来 JInternalFrame并不能正常显示
两个文件arrow.java test.java
import javax.swing.*;import java.awt.*;
import java.awt.event.*;
public class Test extends JFrame {
private int prevx, prevy;
private Arrow arrow;
JDesktopPane desk = new JDesktopPane();
JInternalFrame source = new JInternalFrame("甲方");
JInternalFrame target = new JInternalFrame("乙方");
private MouseListener ml = new MouseAdapter () {
public void mousePressed (MouseEvent e) {
prevx = e.getX ();
prevy = e.getY ();
}
};
private MouseMotionListener mml = new MouseMotionAdapter () {
public void mouseDragged (MouseEvent e) {
Component c = e.getComponent ();
c.setLocation (c.getLocation ().x + e.getX () - prevx, c.getLocation ().y + e.getY () - prevy);
repaint ();
}
};
public Test() throws Exception {
getContentPane().add(desk, BorderLayout.CENTER);
desk.add(source);
desk.add(target);
source.reshape(10,10,100,100);
target.reshape(30,30,100,100);
source.setVisible(true);
target.setVisible(true);
source.addMouseListener(ml);
source.addMouseMotionListener(mml);
target.addMouseListener(ml);
target.addMouseMotionListener(mml);
setBounds(100,100,400,400);
show();
setLayout(null);
add(source,null);add(target,null);
arrow = new Arrow ("指向", source, target, "甲方区", "乙方区");
/*source.setSize (50,50);
source.setLocation (0, 0);
source.setBorder (BorderFactory.createEtchedBorder ());
source.setHorizontalAlignment (JLabel.CENTER);
source.addMouseListener (ml);
source.addMouseMotionListener (mml); JLabel target = new JLabel ("乙方");*/
/*target.setSize (50, 50);
target.setLocation (100, 100);
target.setBorder (BorderFactory.createEtchedBorder ());
target.setHorizontalAlignment (JLabel.CENTER);
target.addMouseListener (ml);
target.addMouseMotionListener (mml); setLayout (null);
add (source, null);
add (target, null); arrow = new Arrow ("指向", source, target, "甲方区", "乙方区");*/
} public void paint (Graphics g) {
super.paint (g);
arrow.draw (g);
} public static void main(String[] args) throws Exception {
Test test = new Test();
}
}
两个文件arrow.java test.java
import javax.swing.*;import java.awt.*;
import java.awt.event.*;
public class Test extends JFrame {
private int prevx, prevy;
private Arrow arrow;
JDesktopPane desk = new JDesktopPane();
JInternalFrame source = new JInternalFrame("甲方");
JInternalFrame target = new JInternalFrame("乙方");
private MouseListener ml = new MouseAdapter () {
public void mousePressed (MouseEvent e) {
prevx = e.getX ();
prevy = e.getY ();
}
};
private MouseMotionListener mml = new MouseMotionAdapter () {
public void mouseDragged (MouseEvent e) {
Component c = e.getComponent ();
c.setLocation (c.getLocation ().x + e.getX () - prevx, c.getLocation ().y + e.getY () - prevy);
repaint ();
}
};
public Test() throws Exception {
getContentPane().add(desk, BorderLayout.CENTER);
desk.add(source);
desk.add(target);
source.reshape(10,10,100,100);
target.reshape(30,30,100,100);
source.setVisible(true);
target.setVisible(true);
source.addMouseListener(ml);
source.addMouseMotionListener(mml);
target.addMouseListener(ml);
target.addMouseMotionListener(mml);
setBounds(100,100,400,400);
show();
setLayout(null);
add(source,null);add(target,null);
arrow = new Arrow ("指向", source, target, "甲方区", "乙方区");
/*source.setSize (50,50);
source.setLocation (0, 0);
source.setBorder (BorderFactory.createEtchedBorder ());
source.setHorizontalAlignment (JLabel.CENTER);
source.addMouseListener (ml);
source.addMouseMotionListener (mml); JLabel target = new JLabel ("乙方");*/
/*target.setSize (50, 50);
target.setLocation (100, 100);
target.setBorder (BorderFactory.createEtchedBorder ());
target.setHorizontalAlignment (JLabel.CENTER);
target.addMouseListener (ml);
target.addMouseMotionListener (mml); setLayout (null);
add (source, null);
add (target, null); arrow = new Arrow ("指向", source, target, "甲方区", "乙方区");*/
} public void paint (Graphics g) {
super.paint (g);
arrow.draw (g);
} public static void main(String[] args) throws Exception {
Test test = new Test();
}
}
import javax.swing.*;
import java.awt.*;/**
public class Arrow {
/** 源控件,即箭头的出发控件 */
private JComponent source;
private JComponent target;
private String name = "";private String sourceField = "";
/** 目标控件的关联字段 */
private String targetField = "";
/** 箭头扇形的开始角度 */
private int startAngle = 0; /** 箭头扇形的半径 */
private static final int ARROW_HEIGHT = 20;
private static final int ARROW_ANGLE = 30; public Arrow (JComponent source, JComponent target) {
this (source.getName () + "_" + target.getName (), source, target);
}
public Arrow (String name, JComponent source, JComponent target) {
this (name, source, target, source.getName (), target.getName ());
}
public Arrow (String name, JComponent source, JComponent target, String sourceField, String targetField) {
setName (name);
setSource (source);
setTarget (target);
this.setSourceField (sourceField);
this.setTargetField (targetField);
} private Point getCenter (JComponent c) {
int x = c.getLocation ().x;
int y = c.getLocation ().y;
int w = c.getSize ().width;
int h = c.getSize ().height; return new Point (x + w / 2, y + h / 2);
} public void draw (Graphics g) {
g.setColor (Color.black);
Point ps = getClosestPoint (getTarget (), getSource ());
Point pt = getClosestPoint (getSource (), getTarget ());
// 画线
g.drawLine (ps.x, ps.y, pt.x, pt.y); // 画箭头
// 简单起见,在这里从线段终点填充一个30度的扇形
if (ps.x != pt.x && ps.y != pt.y) {
double k = getK (ps, pt);
if (ps.x > pt.x)
startAngle = 360 - (int) (Math.atan (k) * 180 / Math.PI) - 15;
else
startAngle = 180 - (int) (Math.atan (k) * 180 / Math.PI) - 15;
}
// 圆心
Point pc = new Point (pt.x - ARROW_HEIGHT, pt.y - ARROW_HEIGHT);
g.fillArc (pc.x, pc.y, 2 * ARROW_HEIGHT, 2 * ARROW_HEIGHT, startAngle, ARROW_ANGLE); FontMetrics fm = g.getFontMetrics ();
int ascent = fm.getAscent ();
int descent = fm.getDescent (); // 在线条中心点处显示名称
int mx = (ps.x + pt.x) / 2;
int my = (ps.y + pt.y) / 2;
g.drawString (name, mx, my + ascent); if (sourceField == null) sourceField = "";
if (targetField == null) targetField = ""; if (ps.y < pt.y) {// 源在目标上方,目标文字应该在更上面一些,源文字应该更下面一些
// 在箭头处显示目标
if (ps.x > pt.x) // 目标在源的左方
g.drawString (getTargetField (), pt.x - fm.stringWidth (getTargetField ()), pt.y - ascent - descent);
else
g.drawString (getTargetField (), pt.x, pt.y - ascent - descent);
// 在线段起点处显示源
g.drawString (getSourceField (), ps.x, ps.y + ascent);
} else {
// 在箭头处显示目标
if (ps.x > pt.x) // 目标在源的左方
g.drawString (getTargetField (), pt.x - fm.stringWidth (getTargetField ()), pt.y + ascent + descent);
else
g.drawString (getTargetField (), pt.x, pt.y + ascent + descent);
// 在线段起点处显示源
g.drawString (getSourceField (), ps.x, ps.y - descent);
}
}
private Point getClosestPoint (JComponent source, JComponent target) {
Point ps = getCenter (source);
Point pt = getCenter (target); if (ps.x == pt.x) { // 垂直线
if (ps.y < pt.y) {// 源在目标上方
this.startAngle = 90 - ARROW_ANGLE / 2;
return new Point (ps.x, target.getLocation ().y);
}
startAngle = 270 - ARROW_ANGLE / 2;
return new Point (ps.x, target.getLocation ().y + target.getSize ().height);
} if (Math.abs (ps.y - pt.y) < 15) { // 水平线
if (ps.x < pt.x) {// 源在目标左边
startAngle = 180 - ARROW_ANGLE / 2;
return new Point (target.getLocation ().x, ps.y);
}
startAngle = - ARROW_ANGLE / 2;
return new Point (target.getLocation ().x + target.getSize ().width, ps.y);
} double k0 = getK (ps, pt);
// 直线方程:
// y = kx + b
// x = (y - b) / k
double b = getB (ps, pt);
int xt = target.getLocation ().x;
int yt = target.getLocation ().y;
int w = target.getWidth ();
int h = target.getHeight (); class DPoint extends Point {
public static final int D_H = 0;
public static final int D_V = 1;
private int d; public DPoint (int x, int y, int d) {
super (x, y);
this.d = d;
} public int getD () {
return d;
}
} // 取于水平边相交的点
DPoint[] pts = new DPoint[4];
// 取上边的点
DPoint pq = new DPoint ((int) ((yt - b) / k0), yt, DPoint.D_H);
pts[0] = pq; // 取下边的点
pq = new DPoint ((int) ((yt + h - b) / k0), yt + h, DPoint.D_H);
pts[1] = pq;
// 取左边的点
pq = new DPoint (xt, (int) (k0 * xt + b), DPoint.D_V);
pts[2] = pq;
// 取右边的点
pq = new DPoint (xt + w, (int) (k0 * (xt + w) + b), DPoint.D_V);
pts[3] = pq; DPoint p1 = null;
DPoint p2 = null;
for (int i = 0; i < pts.length; i++) {
DPoint p = pts[i];
if (p.x < target.getLocation ().x || p.x > target.getLocation ().x + target.getSize ().width ||
p.y < target.getLocation ().y || p.y > target.getLocation ().y + target.getSize ().height)
continue;
if (p1 == null)
p1 = p;
else
p2 = p;
} if (p1.getD () == DPoint.D_H) { // 水平线
if (Math.abs (p1.y - ps.y) < Math.abs (p2.y - ps.y))
return p1;
return p2;
} else { // 垂直线
if (Math.abs (p1.x - ps.x) < Math.abs (p2.x - ps.x))
return p1;
return p2;
}
}
private double getK (Point p1, Point p2) {
return ((double) p2.y - p1.y) / (p2.x - p1.x);
}
private double getB (Point ps, Point pt) {
double x1 = ps.x;
double y1 = ps.y;
double x2 = pt.x;
double y2 = pt.y; return ((double) y1 * x2 - y2 * x1) / (x2 - x1);
} public JComponent getSource () {
return source;
} public JComponent getTarget () {
return target;
} public String getName () {
return name;
} public void setName (String name) {
this.name = name;
} public void setSource (JComponent source) {
this.source = source;
} public void setTarget (JComponent target) {
this.target = target;
} public String getSourceField () {
return sourceField;
} public void setSourceField (String sourceField) {
this.sourceField = sourceField;
} public String getTargetField () {
return targetField;
} public void setTargetField (String targetField) {
this.targetField = targetField;
}
}
JInternalFrame之间有两条直线,JInternalFrame可以拖动.
我的问题是JInternalFrame不能正常显示
请高手帮忙,有类是的代码感激不尽