Java:调整大小和大小可拖动组件

发布于 2024-12-13 04:20:25 字数 1663 浏览 5 评论 0原文

我正在用 Java 制作一个免费的跨平台模型设计器。我设计了用户界面和能够在面板上添加组件(由用户选择)。现在我希望执行以下操作

- 1.使添加的组件在应用程序运行时可调整大小。我的意思是我想在鼠标移动时显示双头箭头指针位于该组件的边界,以便用户开始拖动鼠标指针并单击该组件。它的大小增加或减少。

2.在拖动时启用面板内组件的平滑移动。(此时它看起来令人沮丧,因为它在移动时闪烁(闪烁)并将其自身放置在另一个位置而不是鼠标指针热点!)

3.一些组件(在添加过程中 )通过从组件窗格中选择该组件然后单击面板来将其放置在距预期点(热点)稍远的位置。 这是拖动的代码:

public void dragControl(MouseEvent evt)
{
    JComponent jc=(JComponent)evt.getSource();
    if((evt.getX()<drawingPane.getWidth()-64)&&(evt.getY()<drawingPane.getHeight()-32))
        {
            jc.setLocation(evt.getX(),evt.getY());
            drawingPane.validate();

        }

} 

这是在面板中添加组件的代码。(drawingpane)

private void finalizeControlAddition(JComponent c,JComponent cont,MouseEvent evt,int width,int height)
{
    if((evt.getX()<drawingPane.getWidth()-width)&&(evt.getY()<drawingPane.getHeight()-height))
                    addComponent(cont,c,evt.getX(),evt.getY(),width,height);
                //
                c.setName(Integer.toString(counter));//save a tag of its identification
                //
                components.add(counter, c);
                counter++;
                //
                selectedControl=-1;
                //
                cont.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                //
                statusLabel.setText("Control added.Drag to give proper position or select to change properties from Properties pane.");
}
private void addComponent(Container container,Component c,int x,int y,int width,int height)
{
      c.setBounds(x,y,width,height);
  container.add(c);
      c.repaint();
}

I am making a free cross platform mock up designer in Java. I have designed the UI & was able to add components(selected by user) on a Panel.Now I wish to do following things-

1.make the added components re-sizable while the app is running.I mean I want to show a double head arrow when the mouse pointer is at border of that component so that user starts drag the mouse pointer & its size increases or decreases.

2.enable smooth move of components inside the panel while dragged.(by this time it seems frustrating because it blinks(flickers) while moving & place it self in another position rather than mouse pointer hotspot!)

3.Some components(during adding to panel by selecting that component from component pane then clicking on the panel) are placed a bit far from expected point(hotspot).
Here's the code for drag:

public void dragControl(MouseEvent evt)
{
    JComponent jc=(JComponent)evt.getSource();
    if((evt.getX()<drawingPane.getWidth()-64)&&(evt.getY()<drawingPane.getHeight()-32))
        {
            jc.setLocation(evt.getX(),evt.getY());
            drawingPane.validate();

        }

} 

and here's one that for adding the component in panel.(drawingpane)

private void finalizeControlAddition(JComponent c,JComponent cont,MouseEvent evt,int width,int height)
{
    if((evt.getX()<drawingPane.getWidth()-width)&&(evt.getY()<drawingPane.getHeight()-height))
                    addComponent(cont,c,evt.getX(),evt.getY(),width,height);
                //
                c.setName(Integer.toString(counter));//save a tag of its identification
                //
                components.add(counter, c);
                counter++;
                //
                selectedControl=-1;
                //
                cont.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                //
                statusLabel.setText("Control added.Drag to give proper position or select to change properties from Properties pane.");
}
private void addComponent(Container container,Component c,int x,int y,int width,int height)
{
      c.setBounds(x,y,width,height);
  container.add(c);
      c.repaint();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奢望 2024-12-20 04:20:25

经过努力我已经解决了这些问题。由于这些是任何人都可能面临的非常常见的问题,因此我创建了一个开放库来完成所有这些任务,您可以在应用程序中使用它们。可以在这里找到:

http://sourceforge.net/p/actioncomponent/home/Home

After trying hard I have solved these problems. As these are very common problems that anyone may face, I have created a open library for doing all these tasks which you can use in your app. It can be found here:

http://sourceforge.net/p/actioncomponent/home/Home

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文