繁体   English   中英

使用Swing组件在Java Swing中创建一个简单的GUI编辑器

[英]Make a simple GUI editor in Java Swing using Swing components

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

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

我想知道我是否可以在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();
  }

}

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM