java - 使用 Swing 组件在 Java Swing 中制作一个简单的 GUI 编辑器

标签 java swing user-interface editor jcomponent

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

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

我想知道我是否可以在 JPanel 上重用真正的 Swing 小部件并让用户放置、四处移动和调整它们的大小。如果是这样,我应该检查哪些事项?

我知道这可能看起来信息很少,但老实说,我一直在寻找解决方案。

最佳答案

我想我应该记住过去美好的 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();
  }

}

关于java - 使用 Swing 组件在 Java Swing 中制作一个简单的 GUI 编辑器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11990045/

相关文章:

java - 这个 Swing 代码是如何工作的?

user-interface - 是否有一种语言和平台不可知的声明式 GUI 语言不是 XML?

java - ArrayAdapter getView 返回 NullPointerException -

java - 在 LINKEDLIST 制作的 QUEUE 中使用什么来代替 NULL

java - 更新准备好的语句未提交

java - JFrame 标题不会正确对齐

java - 从 JSP 输出环境变量

java - Java JScrollPane 的内存使用

java - JScrollPane (for JTable) 在 MigLayout 中填充和增长

java - 在循环内填充时更新并观察 jtables 单元格值