关于可视化编程软件的开发! “拖动控件”是个假象,判断“拖动”的坐标,在设计区域就可以相应你自己写的package.我是这样用过,不知道适合不适合你。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 JasperEdit是一个for JasperReport的报表编辑器,支持控件的拖动。自己到sourceforge去down一个下来,看看人家是怎么实现的吧。 Schlemiel(维特根斯坦的扇子)""sourceforge"是什么啊? pxboy(阿土仔) 具体点啊 我没事做的一个玩意,希望对你有帮助import javax.swing.*;import java.awt.event.*;import java.awt.*;import javax.swing.border.*;public class DragFrame extends JFrame{ public JPanel jPanel; Component selected = null; public DragFrame() { try { jbInit(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { DragFrame dragFrame = new DragFrame(); dragFrame.setSize(500, 500); dragFrame.setVisible(true); dragFrame.jPanel.repaint(); } private void jbInit() throws Exception { JTextField t1 = new JTextField(); t1.setEditable(false); Border border1 = BorderFactory.createEtchedBorder(Color.white, new Color(178, 178, 178)); t1.setBackground(Color.white); t1.setBorder(border1); t1.setText("文本输入框"); t1.setName("JTextField"); t1.setBounds(new Rectangle(259, 131, 75, 25)); JButton b1 = new JButton() { Image image = Toolkit.getDefaultToolkit().createImage(DragFrame.class.getResource("resource/plus.gif")); public void paint(Graphics g) { g.setColor(new Color(178, 178, 178)); g.draw3DRect(0, 0, this.getWidth() - 1, this.getHeight() - 1, false); g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), null); } }; Image image = Toolkit.getDefaultToolkit().createImage(DragFrame.class.getResource("resource/plus.gif")); MediaTracker tracker = new MediaTracker(t1); tracker.addImage(image, 0); try { tracker.waitForID(0); } catch (InterruptedException e) { e.printStackTrace(); } b1.setBorder(border1); b1.setBackground(SystemColor.control); b1.setHorizontalAlignment(JTextField.CENTER); b1.setText("按纽"); b1.setName("JButton"); b1.setBounds(new Rectangle(278, 185, 75, 25)); b1.setIcon(common.Transfer.getAppIcon("resource/plus.gif")); JTextField cb1 = new JTextField(); cb1.setBorder(border1); cb1.setBackground(SystemColor.control); cb1.setText("下拉框"); cb1.setName("JComboBox"); cb1.setBounds(new Rectangle(146, 303, 75, 25)); MouseAdapter1 mouseAdapter1 = new MouseAdapter1(); t1.addMouseListener(mouseAdapter1); t1.addMouseMotionListener(mouseAdapter1); b1.addMouseListener(mouseAdapter1); b1.addMouseMotionListener(mouseAdapter1); cb1.addMouseListener(mouseAdapter1); cb1.addMouseMotionListener(mouseAdapter1); this.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(WindowEvent e) { this_windowClosing(e); } }); jPanel = new JPanel() { public void paint(Graphics g) { int nStartX = 0, nStartY = 0; int nEndX = 0, nEndY = 0; Component c = null, parent = null; Component components[] = getComponents(); int nCount = getComponentCount(); super.paint(g); g.setColor(Color.red); for (int i = 1; i < nCount; i++) { c = components[i]; parent = components[i - 1]; nStartX = (int) (c.getLocation().getX() + c.getWidth() / 2); nStartY = (int)c.getLocation().getY(); nEndX = (int) (parent.getLocation().getX() + parent.getWidth() / 2); nEndY = (int) (parent.getLocation().getY() + parent.getHeight()); g.drawLine(nStartX, nStartY - 4, nEndX, nEndY); g.drawOval(nStartX - 2, nStartY - 4, 4, 4); } if (selected != null) { g.setColor(Color.black); g.drawOval(selected.getLocation().x - 1, selected.getLocation().y - 1, 1, 1); g.drawOval(selected.getLocation().x + selected.getWidth() - 1, selected.getLocation().y - 1, 1, 1); g.drawOval(selected.getLocation().x - 1, selected.getLocation().y + selected.getHeight() - 1, 1, 1); g.drawOval(selected.getLocation().x + selected.getWidth() - 1, selected.getLocation().y + selected.getHeight() - 1, 1, 1); } } }; jPanel.setLayout(null); jPanel.add(t1); jPanel.add(cb1, null); jPanel.add(b1); JScrollPane jScrollPane1 = new JScrollPane(); jScrollPane1.getViewport().add(jPanel); this.getContentPane().add(jScrollPane1, null); } final static int ADJUST_MOVE = 0; final static int ADJUST_WEST = 1; final static int ADJUST_NORTH = 2; final static int ADJUST_EAST = 3; final static int ADJUST_SOUTH = 4; final static int ADJUST_WEST_SOUTH = 5; final static int ADJUST_WEST_NORTH = 6; final static int ADJUST_EAST_NORTH = 7; final static int ADJUST_EAST_SOUTH = 8; protected int offsetX; protected int offsetY; /** * current adjust mode of the mouse */ protected int adjustMode = ADJUST_MOVE; public class MouseAdapter1 extends MouseAdapter implements MouseMotionListener { public void mousePressed(MouseEvent e) { selected = (Component)e.getSource(); startAdjust(e); } public void mouseReleased(MouseEvent e) { endAdjust(); } public void mouseDragged(MouseEvent e) { adjust(e); } public void mouseMoved(MouseEvent e) { changeCursor(e); } } protected void startAdjust(MouseEvent e) { offsetX = e.getX(); offsetY = e.getY(); if (isEastSouth(e)) { adjustMode = DragFrame.ADJUST_EAST_SOUTH; } else if (isEastNorth(e)) { adjustMode = DragFrame.ADJUST_EAST_NORTH; } else if (isWestNorth(e)) { adjustMode = DragFrame.ADJUST_WEST_NORTH; } else if (isWestSouth(e)) { adjustMode = DragFrame.ADJUST_WEST_SOUTH; } else if (isWest(e)) { adjustMode = DragFrame.ADJUST_WEST; } else if (isNorth(e)) { adjustMode = DragFrame.ADJUST_NORTH; } else if (isEast(e)) { adjustMode = DragFrame.ADJUST_EAST; } else if (isSouth(e)) { adjustMode = DragFrame.ADJUST_SOUTH; } else { adjustMode = DragFrame.ADJUST_MOVE; } } protected void endAdjust() { adjustMode = DragFrame.ADJUST_MOVE; /** Reset jPanel's view size and adjust the scroll bar */ resetPrerredSize(); jPanel.repaint(); } protected void adjust(MouseEvent e) { Component source = (Component)e.getSource(); int width = source.getWidth(); int height = source.getHeight(); int x = source.getLocation().x; int y = source.getLocation().y; int mouseX = (int)e.getX(); int mouseY = (int)e.getY(); if (adjustMode == DragFrame.ADJUST_EAST_SOUTH) { width = mouseX; height = mouseY; } else if (adjustMode == DragFrame.ADJUST_EAST_NORTH) { width = mouseX; height -= mouseY - offsetY; y += mouseY - offsetY; } else if (adjustMode == DragFrame.ADJUST_WEST_NORTH) { width -= mouseX - offsetX; height -= mouseY - offsetY; x += mouseX - offsetX; y += mouseY - offsetY; } else if (adjustMode == DragFrame.ADJUST_WEST_SOUTH) { width -= mouseX - offsetX; height = mouseY; x += mouseX - offsetX; } else if (adjustMode == DragFrame.ADJUST_WEST) { width -= mouseX - offsetX; x += mouseX - offsetX; } else if (adjustMode == DragFrame.ADJUST_NORTH) { height -= mouseY - offsetY; y += mouseY - offsetY; } else if (adjustMode == DragFrame.ADJUST_EAST) { width = mouseX; } else if (adjustMode == DragFrame.ADJUST_SOUTH) { height = mouseY; } else if (adjustMode == DragFrame.ADJUST_MOVE) { x += mouseX - offsetX; y += mouseY - offsetY; } else { return; } if (width < 40) { x = source.getLocation().x; width = 40; } if (height < 20) { y = source.getLocation().y; height = 20; } source.setSize(width, height); source.setLocation(x, y); jPanel.repaint(); } private void resetPrerredSize() { Component c = null; Component components[] = jPanel.getComponents(); int nCount = jPanel.getComponentCount(); int maxWidth = 0, maxHeight = 0; int currentWidth = 0, currentHeight = 0; for (int i = 0; i < nCount; i++) { c = components[i]; currentWidth = (int) (c.getLocation().getX() + c.getWidth()); currentHeight = (int) (c.getLocation().getY() + c.getHeight()); if (currentWidth > maxWidth) { maxWidth = currentWidth; } if (currentHeight > maxHeight) { maxHeight = currentHeight; } } jPanel.setPreferredSize(new Dimension(maxWidth + 10, maxHeight + 10)); Container container = (Container)jPanel.getParent(); if (container != null) container.validate(); } private void changeCursor(MouseEvent e) { Component source = (Component)e.getSource(); if (isEastSouth(e)) { source.setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR)); } else if (isEastNorth(e)) { source.setCursor(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR)); } else if (isWestNorth(e)) { source.setCursor(Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR)); } else if (isWestSouth(e)) { source.setCursor(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR)); } else if (isWest(e)) { source.setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR)); } else if (isNorth(e)) { source.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR)); } else if (isEast(e)) { source.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR)); } else if (isSouth(e)) { source.setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR)); } else { source.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)); } } private boolean isEast(MouseEvent e) { return (e.getX() <= ( (Component)e.getSource()).getWidth() && e.getX() > ( (Component)e.getSource()).getWidth() - 4); } private boolean isSouth(MouseEvent e) { return (e.getY() <= ( (Component)e.getSource()).getHeight() && e.getY() > ( (Component)e.getSource()).getHeight() - 4); } private boolean isWest(MouseEvent e) { return (e.getX() < 4 && e.getX() >= 0); } private boolean isNorth(MouseEvent e) { return (e.getY() < 4 && e.getY() >= 0); } private boolean isEastSouth(MouseEvent e) { return isEast(e) && isSouth(e); } private boolean isWestSouth(MouseEvent e) { return isWest(e) && isSouth(e); } private boolean isEastNorth(MouseEvent e) { return isEast(e) && isNorth(e); } private boolean isWestNorth(MouseEvent e) { return isWest(e) && isNorth(e); } private void this_windowClosing(WindowEvent e) { System.exit(0); }} pxboy(阿土仔):有问题,编译无法通过。D:\java\JCreator Pro\MyProjects\DragFrame\DragFrame.java:75: package common does not exist b1.setIcon(common.Transfer.getAppIcon("resource/plus.gif"));这里将icon替换成你自己的,或者注释掉这行,这种小问题应该自己能解决吧? 【不给力啊~】 关于JPanel数组的一个问题 JAVA转JavaScript? 关于java图形界面GridLayout的问题 执行结果不对,请各位帮忙[可用分不多了,希望不影响各位心情] ????? java应用程序中使用odbc数据源连接数据库,如何获取已经定义的odbc数据源的用户名和密码? Java如何解析变长字符串? java计算器 请问jbuilder7中有没有像VC中的group box的控件 用JDBC如何连接Informix数据库 applet之间的通讯问题??? 着急呀,怎么样在jTextArea中画一幅jpg格式的图片?
""sourceforge"是什么啊?
具体点啊
import java.awt.event.*;
import java.awt.*;
import javax.swing.border.*;public class DragFrame
extends JFrame
{
public JPanel jPanel; Component selected = null; public DragFrame()
{
try
{
jbInit();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
DragFrame dragFrame = new DragFrame();
dragFrame.setSize(500, 500);
dragFrame.setVisible(true);
dragFrame.jPanel.repaint();
}
private void jbInit() throws Exception
{
JTextField t1 = new JTextField();
t1.setEditable(false);
Border border1 = BorderFactory.createEtchedBorder(Color.white, new Color(178, 178, 178));
t1.setBackground(Color.white);
t1.setBorder(border1);
t1.setText("文本输入框");
t1.setName("JTextField");
t1.setBounds(new Rectangle(259, 131, 75, 25)); JButton b1 = new JButton()
{
Image image = Toolkit.getDefaultToolkit().createImage(DragFrame.class.getResource("resource/plus.gif")); public void paint(Graphics g)
{
g.setColor(new Color(178, 178, 178));
g.draw3DRect(0, 0, this.getWidth() - 1, this.getHeight() - 1, false);
g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), null);
}
};
Image image = Toolkit.getDefaultToolkit().createImage(DragFrame.class.getResource("resource/plus.gif"));
MediaTracker tracker = new MediaTracker(t1);
tracker.addImage(image, 0);
try
{
tracker.waitForID(0);
}
catch (InterruptedException e)
{
e.printStackTrace();
} b1.setBorder(border1);
b1.setBackground(SystemColor.control);
b1.setHorizontalAlignment(JTextField.CENTER);
b1.setText("按纽");
b1.setName("JButton");
b1.setBounds(new Rectangle(278, 185, 75, 25));
b1.setIcon(common.Transfer.getAppIcon("resource/plus.gif")); JTextField cb1 = new JTextField();
cb1.setBorder(border1);
cb1.setBackground(SystemColor.control);
cb1.setText("下拉框");
cb1.setName("JComboBox");
cb1.setBounds(new Rectangle(146, 303, 75, 25)); MouseAdapter1 mouseAdapter1 = new MouseAdapter1(); t1.addMouseListener(mouseAdapter1);
t1.addMouseMotionListener(mouseAdapter1); b1.addMouseListener(mouseAdapter1);
b1.addMouseMotionListener(mouseAdapter1); cb1.addMouseListener(mouseAdapter1);
cb1.addMouseMotionListener(mouseAdapter1); this.addWindowListener(new java.awt.event.WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
this_windowClosing(e);
}
}); jPanel = new JPanel()
{
public void paint(Graphics g)
{
int nStartX = 0, nStartY = 0;
int nEndX = 0, nEndY = 0;
Component c = null, parent = null;
Component components[] = getComponents(); int nCount = getComponentCount(); super.paint(g); g.setColor(Color.red);
for (int i = 1; i < nCount; i++)
{
c = components[i];
parent = components[i - 1]; nStartX = (int) (c.getLocation().getX() + c.getWidth() / 2);
nStartY = (int)c.getLocation().getY();
nEndX = (int) (parent.getLocation().getX() + parent.getWidth() / 2);
nEndY = (int) (parent.getLocation().getY() + parent.getHeight()); g.drawLine(nStartX, nStartY - 4, nEndX, nEndY);
g.drawOval(nStartX - 2, nStartY - 4, 4, 4);
}
if (selected != null)
{
g.setColor(Color.black);
g.drawOval(selected.getLocation().x - 1, selected.getLocation().y - 1, 1, 1);
g.drawOval(selected.getLocation().x + selected.getWidth() - 1, selected.getLocation().y - 1, 1, 1);
g.drawOval(selected.getLocation().x - 1, selected.getLocation().y + selected.getHeight() - 1, 1, 1);
g.drawOval(selected.getLocation().x + selected.getWidth() - 1, selected.getLocation().y + selected.getHeight() - 1, 1, 1);
}
}
};
jPanel.setLayout(null);
jPanel.add(t1);
jPanel.add(cb1, null);
jPanel.add(b1);
JScrollPane jScrollPane1 = new JScrollPane();
jScrollPane1.getViewport().add(jPanel);
this.getContentPane().add(jScrollPane1, null);
} final static int ADJUST_MOVE = 0; final static int ADJUST_WEST = 1; final static int ADJUST_NORTH = 2; final static int ADJUST_EAST = 3; final static int ADJUST_SOUTH = 4; final static int ADJUST_WEST_SOUTH = 5; final static int ADJUST_WEST_NORTH = 6; final static int ADJUST_EAST_NORTH = 7; final static int ADJUST_EAST_SOUTH = 8; protected int offsetX; protected int offsetY; /**
* current adjust mode of the mouse
*/
protected int adjustMode = ADJUST_MOVE; public class MouseAdapter1
extends MouseAdapter
implements MouseMotionListener
{
public void mousePressed(MouseEvent e)
{
selected = (Component)e.getSource();
startAdjust(e);
}
public void mouseReleased(MouseEvent e)
{
endAdjust();
}
public void mouseDragged(MouseEvent e)
{
adjust(e);
}
public void mouseMoved(MouseEvent e)
{
changeCursor(e);
}
}
{
offsetX = e.getX();
offsetY = e.getY();
if (isEastSouth(e))
{
adjustMode = DragFrame.ADJUST_EAST_SOUTH;
}
else if (isEastNorth(e))
{
adjustMode = DragFrame.ADJUST_EAST_NORTH;
}
else if (isWestNorth(e))
{
adjustMode = DragFrame.ADJUST_WEST_NORTH;
}
else if (isWestSouth(e))
{
adjustMode = DragFrame.ADJUST_WEST_SOUTH;
}
else if (isWest(e))
{
adjustMode = DragFrame.ADJUST_WEST;
}
else if (isNorth(e))
{
adjustMode = DragFrame.ADJUST_NORTH;
}
else if (isEast(e))
{
adjustMode = DragFrame.ADJUST_EAST;
}
else if (isSouth(e))
{
adjustMode = DragFrame.ADJUST_SOUTH;
}
else
{
adjustMode = DragFrame.ADJUST_MOVE;
}
}
protected void endAdjust()
{
adjustMode = DragFrame.ADJUST_MOVE; /** Reset jPanel's view size and adjust the scroll bar */
resetPrerredSize(); jPanel.repaint();
}
protected void adjust(MouseEvent e)
{
Component source = (Component)e.getSource(); int width = source.getWidth();
int height = source.getHeight();
int x = source.getLocation().x;
int y = source.getLocation().y; int mouseX = (int)e.getX();
int mouseY = (int)e.getY(); if (adjustMode == DragFrame.ADJUST_EAST_SOUTH)
{
width = mouseX;
height = mouseY;
}
else if (adjustMode == DragFrame.ADJUST_EAST_NORTH)
{
width = mouseX;
height -= mouseY - offsetY;
y += mouseY - offsetY;
} else if (adjustMode == DragFrame.ADJUST_WEST_NORTH)
{
width -= mouseX - offsetX;
height -= mouseY - offsetY;
x += mouseX - offsetX;
y += mouseY - offsetY;
}
else if (adjustMode == DragFrame.ADJUST_WEST_SOUTH)
{
width -= mouseX - offsetX;
height = mouseY;
x += mouseX - offsetX;
} else if (adjustMode == DragFrame.ADJUST_WEST)
{
width -= mouseX - offsetX;
x += mouseX - offsetX;
}
else if (adjustMode == DragFrame.ADJUST_NORTH)
{
height -= mouseY - offsetY;
y += mouseY - offsetY;
}
else if (adjustMode == DragFrame.ADJUST_EAST)
{
width = mouseX;
}
else if (adjustMode == DragFrame.ADJUST_SOUTH)
{
height = mouseY;
}
else if (adjustMode == DragFrame.ADJUST_MOVE)
{
x += mouseX - offsetX;
y += mouseY - offsetY;
}
else
{
return;
} if (width < 40)
{
x = source.getLocation().x;
width = 40;
}
if (height < 20)
{
y = source.getLocation().y;
height = 20;
} source.setSize(width, height);
source.setLocation(x, y); jPanel.repaint();
}
private void resetPrerredSize()
{
Component c = null;
Component components[] = jPanel.getComponents(); int nCount = jPanel.getComponentCount();
int maxWidth = 0, maxHeight = 0;
int currentWidth = 0, currentHeight = 0;
for (int i = 0; i < nCount; i++)
{
c = components[i];
currentWidth = (int) (c.getLocation().getX() + c.getWidth());
currentHeight = (int) (c.getLocation().getY() + c.getHeight());
if (currentWidth > maxWidth)
{
maxWidth = currentWidth;
}
if (currentHeight > maxHeight)
{
maxHeight = currentHeight;
}
}
jPanel.setPreferredSize(new Dimension(maxWidth + 10, maxHeight + 10));
Container container = (Container)jPanel.getParent();
if (container != null)
container.validate();
}
private void changeCursor(MouseEvent e)
{
Component source = (Component)e.getSource(); if (isEastSouth(e))
{
source.setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
}
else if (isEastNorth(e))
{
source.setCursor(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
}
else if (isWestNorth(e))
{
source.setCursor(Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
}
else if (isWestSouth(e))
{
source.setCursor(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));
}
else if (isWest(e))
{
source.setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
}
else if (isNorth(e))
{
source.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
}
else if (isEast(e))
{
source.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
}
else if (isSouth(e))
{
source.setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
}
else
{
source.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
}
}
private boolean isEast(MouseEvent e)
{
return (e.getX() <= ( (Component)e.getSource()).getWidth()
&& e.getX() > ( (Component)e.getSource()).getWidth() - 4);
}
private boolean isSouth(MouseEvent e)
{
return (e.getY() <= ( (Component)e.getSource()).getHeight()
&& e.getY() > ( (Component)e.getSource()).getHeight() - 4);
}
private boolean isWest(MouseEvent e)
{
return (e.getX() < 4 && e.getX() >= 0);
}
private boolean isNorth(MouseEvent e)
{
return (e.getY() < 4 && e.getY() >= 0);
}
private boolean isEastSouth(MouseEvent e)
{
return isEast(e) && isSouth(e);
}
private boolean isWestSouth(MouseEvent e)
{
return isWest(e) && isSouth(e);
}
private boolean isEastNorth(MouseEvent e)
{
return isEast(e) && isNorth(e);
}
private boolean isWestNorth(MouseEvent e)
{
return isWest(e) && isNorth(e);
}
private void this_windowClosing(WindowEvent e)
{
System.exit(0);
}}
这里将icon替换成你自己的,或者注释掉这行,
这种小问题应该自己能解决吧?