6

我目前正在做一个项目,我需要一个非常简单的编辑器来处理类似 GUI 的对象。这个编辑器将是一个可以放置众所周知的 GUI 小部件的画布。例如,可以在那里放置一个按钮和一个文本字段,移动它们并调整它们的大小。不需要与小部件本身进行交互。

我一直在尝试通过改编一个非常简单的绘画教程来实现这一点,我认为以这种方式实现它很容易,但是在画布上绘制自定义形状和文本以及拖放这些形状时遇到了许多问题。

我想知道是否可以在 JPanel 上重用真正的 Swing 小部件并让用户放置、移动和调整它们的大小。如果是这样,我应该研究什么?

我知道这似乎信息很少,但老实说,我一直在寻找解决方案。

4

3 回答 3

6

我想我应该记住过去的 Swing 美好时光,并为此写一个概念证明给你一点乐趣。它支持向主面板添加一个按钮,然后移动和一些基本的调整大小。

这只是一个概念证明,但它回答了您已经提出的许多问题。我在那里添加了一些 TODO,例如您将知道下一步该做什么。

享受...

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;

public class UIBuilder {
  private static final int NONE = -1;

  private static final int BORDER = 3;

  private JFrame frame = new JFrame("Builder");

  private JToolBar toolbar = new JToolBar();

  private JPanel main = new JPanel();

  private int startX = NONE;

  private int startY = NONE;

  private int prevX = NONE;

  private int prevY = NONE;

  private boolean resize = false;

  public UIBuilder() {
    frame.setBounds(100, 100, 600, 450);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new BorderLayout());
    frame.getContentPane().add(toolbar, BorderLayout.PAGE_START);
    frame.getContentPane().add(main, BorderLayout.CENTER);
    frame.setVisible(true);
    buildToolbox();
    buildMainPanel();
  }

  private void buildToolbox() {
    JButton button = new JButton("Button");
    button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        JButton btn = new JButton("Button");
        addComponent(btn);
      }
    });
    toolbar.add(button);
  }

  private void buildMainPanel() {
    main.setLayout(null);
  }

  private void addComponent(JComponent comp) {
    comp.setBounds(10, 10, 80, 24);

    comp.addMouseListener(new MouseAdapter() {
      public void mouseReleased(MouseEvent e) {
        startX = NONE;
        startY = NONE;
        ((JComponent) e.getSource()).setCursor(Cursor.getDefaultCursor());
      }

      public void mousePressed(MouseEvent e) {
        startX = e.getX();
        startY = e.getY();
      }
    });

    comp.addMouseMotionListener(new MouseMotionAdapter() {
      public void mouseMoved(MouseEvent e) {
        JComponent source = (JComponent) e.getSource();
        int x = e.getX();
        int y = e.getY();
        Rectangle bounds = source.getBounds();
        resize = x < BORDER || y < BORDER || Math.abs(bounds.width - x) < BORDER || Math.abs(bounds.height - y) < BORDER;
        if (resize) {
          // TODO: there are a lot of resize cursors here, this is just of proof of concept
          source.setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
        } else {
          source.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
        }
      }

      public void mouseDragged(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        if (startX != NONE && startY != NONE) {
          JComponent source = (JComponent) e.getSource();
          Rectangle bounds = source.getBounds();
          int deltaX = x - startX;
          int deltaY = y - startY;
          if (resize) {
            // TODO: handle all resize cases, left, right,...
            source.setSize(Math.max(10, bounds.width + x - prevX), Math.max(10, bounds.height + y - prevY));
          } else {
            source.setLocation(bounds.x + deltaX, bounds.y + deltaY);
          }
          // TODO: make sure you don't resize it as much as it disappears
          // TODO: make sure you don't move it outside the main panel
        } else {
          startX = x;
          startY = y;
        }
        prevX = x;
        prevY = y;
      }
    });
    main.add(comp);
    main.validate();
    main.repaint();
  }

  public static void main(String[] args) {
    new UIBuilder();
  }

}
于 2012-08-16T17:17:49.947 回答
2

这听起来像是一个有趣的项目。

我肯定会使用 JPanel 作为容器,布局设置为空。为了能够在容器中移动 JButton、JLabel 等组件,您必须在添加的组件上侦听 MouseListener 和 MouseMotionListener 事件并相应地处理它们。每当移动或调整一个组件的大小时,您都必须在容器上调用 validate() 和 repaint()。

于 2012-08-16T15:26:45.727 回答
1

我会以混合方式实现这一点:创建一个实现鼠标响应(拖动、调整大小)的类。此类负责管理用户交互。

对于组件的实际绘制,使用真正的Swing 组件(用户交互类将引用它应该表示的组件并将实际渲染委托给该组件)。

这种方法避免了扩展原始 Swing 组件时出现的大部分复杂性,但您仍然可以重用它们的所有绘制代码​​。

于 2012-08-16T15:28:08.447 回答