首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Swing组件在Java Swing中创建简单的GUI编辑器

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

Stack Overflow用户
提问于 2012-08-16 22:58:56
回答 3查看 2.9K关注 0票数 6

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

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

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

我知道这可能看起来信息很少,但老实说,我陷入了寻找解决方案的困境。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-08-17 01:17:50

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

这只是一个概念证明,但它回答了您已经有的许多问题。我在那里添加了几个待办事项,比如您将知道下一步要做什么。

享受吧..。

代码语言:javascript
运行
复制
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();
  }

}
票数 6
EN

Stack Overflow用户

发布于 2012-08-16 23:26:46

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

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

票数 2
EN

Stack Overflow用户

发布于 2012-08-16 23:28:08

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

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11990045

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档