Java 中用于拖动组件的 Swing 库

2023-12-30

我正在尝试创建一种图形编辑器,允许用户创建美式足球比赛的图形描述。为此,用户应该能够执行以下操作:

1) 单击鼠标左键并移动图像

2) 更改图像(圆形、方形和线条)

3)重置所有对象的大小

理想情况下,我希望能够添加可调整的颜色和线条粗细,但这还很遥远。

现在,我所能做的就是创建 JButton,单击时循环浏览图像。我想我想将其更改为 JComboBox,以便用户可以直接转到正确的图像。这是我的类:FBButton

import javax.swing.*;
import java.awt.event.*;

@SuppressWarnings("serial")
public class FBButton extends JButton implements ActionListener {
    ImageIcon SN, SL, SR, SC, CN, CL, CR, CC, IN;
    byte value = 0;
    FBMouseListener listener;

    public FBButton() {

        listener = new FBMouseListener();

        SN = new ImageIcon(this.getClass().getResource("square_null.png"));
        SL = new ImageIcon(this.getClass().getResource("square_left.png"));
        SR = new ImageIcon(this.getClass().getResource("square_right.png"));
        SC = new ImageIcon(this.getClass().getResource("square_line.png"));

        CN = new ImageIcon(this.getClass().getResource("circle_null.png"));
        CL = new ImageIcon(this.getClass().getResource("circle_left.png"));
        CR = new ImageIcon(this.getClass().getResource("circle_right.png"));
        CC = new ImageIcon(this.getClass().getResource("circle_line.png"));

        IN = new ImageIcon(this.getClass().getResource("invisible.png"));

        addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        value++;
        value %= 9;

        if (value == 1) {
            setIcon(SN);
        } else if (value == 2) {
            setIcon(SL);
        } else if (value == 3) {
            setIcon(SR);
        } else if (value == 4) {
            setIcon(SC);
        } else if (value == 5) {
            setIcon(CN);
        } else if (value == 6) {
            setIcon(CL);
        } else if (value == 7) {
            setIcon(CR);
        } else if (value == 8) {
            setIcon(CC);
        } else {
            setIcon(IN);
        }


    }

}

这些按钮起作用并且可以找到图像。这是我的 FBPlayerFrame 类的代码

package swing;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class FBPlayerFrame extends JFrame {

    JPanel p = new JPanel();
    FBButton buttons[] = new FBButton[22];
    String choices[] = { "Hallo", "Bonjour", "Conichuwa" };
    JComboBox boxes[];
    JComboBox here = new JComboBox(choices);
    FBComboBox vince;

    Dimension dim = new Dimension(52, 52);

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

    public FBPlayerFrame() {
        super("Football Start");
        setSize(400, 400);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        p.setLayout(null);


        for (int i = 0; i < 4; i++) {
            buttons[i] = new FBButton();
            buttons[i].setPreferredSize(dim);
            buttons[i].setLocation(20, 40 + 60 * i);
            p.add(buttons[i]);


        }


        add(p);

        setVisible(true);
    }

}

本着保持具体的精神,我首先寻找的是在整个框架中左键单击并拖动 JButtons 或 JComboBoxes 的能力。如果可以在某个时候保存按钮的坐标,这也会对以后有所帮助,但现在没有必要。

我在 StackOverflow 和 youtube 上搜索了类似的问题,但在找到专门回答我的问题的东西时遇到了挑战。

UPDATE:这是我的 FB MouseListener 代码

package swing;

import java.awt.Component;
import java.awt.Point;
import java.awt.event.*;
import javax.swing.event.MouseInputAdapter;

public class FBMouseListener extends MouseInputAdapter {
    Point location;
    MouseEvent pressed;

    public void mousePressed(MouseEvent me) {
        pressed = me;
        System.out.println("Found me");
    }

    public void mouseDragged(MouseEvent me) {
        Component component = me.getComponent();
        location = component.getLocation(location);
        int x = location.x - pressed.getX() + me.getX();
        int y = location.y - pressed.getY() + me.getY();
        System.out.println("(" + x + ", " + y + ")");
        component.setLocation(x, y);
    }
}

拖动组件的基本代码是:

public class DragListener extends MouseInputAdapter
{
    Point location;
    MouseEvent pressed;

    public void mousePressed(MouseEvent me)
    {
        pressed = me;
    }

    public void mouseDragged(MouseEvent me)
    {
        Component component = me.getComponent();
        location = component.getLocation(location);
        int x = location.x - pressed.getX() + me.getX();
        int y = location.y - pressed.getY() + me.getY();
        component.setLocation(x, y);
     }
}

您创建该类的单个实例,然后将其添加到您想要拖动的任何组件。

DragListener drag = new DragListener();
component.addMouseListener( drag );
component.addMouseMotionListener( drag );

您还可以查看组件移动器 https://tips4java.wordpress.com/2009/06/14/moving-windows/班级。它允许您拖动桌面上的窗口或面板中的组件。它提供了更多的拖动功能。

Edit:

我需要几行代码来测试这个解决方案:

JButton button = new JButton("hello");
button.setSize( button.getPreferredSize() );

DragListener drag = new DragListener();
button.addMouseListener( drag );
button.addMouseMotionListener( drag );

JPanel panel = new JPanel( null );
panel.add( button );

JFrame frame = new JFrame();
frame.add( panel );
frame.setSize(400, 400);
frame.setVisible( true );

将上面的代码放入 main() 方法中,您就可以得到简单的代码来测试。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Java 中用于拖动组件的 Swing 库 的相关文章

随机推荐

  • 在 C# 中从字符串数组获取随机值的最快方法?

    在 net 2 0 框架上用 C 从字符串数组中获取随机值的最快方法是什么 我想他们可能有这个 string fileLines File ReadAllLines filePath fileLines GetRandomValue 是的
  • 哪种 rpc/消息传递框架最适合这种情况?

    用例 一个 Java 进程与一个或两个 C 进程 始终在同一台机器上 需要双向 二进制 非持久通信 其中一个 C 进程负责实例化其他进程 我环顾四周 看到了 XML JSON RPC Protocol Buffers Thrift zero
  • 无法在 python 中对 URL 进行 urllib.urlencode 编码

    为什么我在尝试对该字符串进行 urlencode 时收到此错误 gt gt gt callback http localhost application authtwitter twitterCallback gt gt gt urllib
  • 用于在任务内等待的 Thread.Sleep 的无冻结替代方案[重复]

    这个问题在这里已经有答案了 我必须调用一个 Web API 其工作原理如下 上传歌曲 请求对该歌曲进行具体分析 wait该过程已完成 检索并返回结果 我有一个问题 没有 3 我已经尝试过了Thread Sleep但这会冻结用户界面 如何在不
  • 如何在 Flutter 控制台中隐藏此消息 - ViewPostIme 指针 0

    每次我点击设备屏幕时 我都会在 Flutter 控制台中看到此消息 D ViewRootImpl 38eee14 MainActivity 7994 ViewPostIme pointer 0 其次是 D ViewRootImpl 38ee
  • 考虑到危险,为什么项目要使用 -I include 开关?

    阅读细则 I在 GCC 中切换时 我相当震惊地发现在命令行上使用它会覆盖系统包含 来自预处理器文档 https gcc gnu org onlinedocs cpp Invocation html Invocation 您可以使用 I覆盖系
  • Delphi 执行应用程序

    我正在尝试使用 delphi 创建一个 dll 我设置了一些文件属性 但随后我想从工作目录运行 exe 文件 我尝试用这段代码运行exe文件 ShellExecute Handle open start exe nil nil SW SHO
  • c++ std::unique_ptr 不会在地图中编译

    我目前正在尝试将 std unique ptr 存储在 std unordered map 中 但出现奇怪的编译错误 相关代码 pragma once include Entity h include
  • java求n个数组之间的公共元素之和

    我有一个程序对两个数组的公共元素求和 为此 我使用了两个 for 循环 如果我有三个 那么我可以使用三个 for 循环 但是如何对运行时出现的 n 个数组的公共元素求和 我不知道如何在运行时更改循环数 或者是否有其他相关概念 这是我尝试对两
  • 如何测试变量是否是 Moment.js 对象?

    我的应用程序有一个 HTML 表单 其中一些输入是从后端填充的 其他输入是由用户输入的 在time输入 一个onChange当用户更改值时 函数会遍历每个输入 从后端填充的输入被转换为moment对象 用户输入的日期只是字符串 这意味着on
  • 为什么阻止未来被认为是一种不好的做法?

    我试图理解该声明背后的理性对于绝对需要封锁的情况 期货可以被封锁 尽管不鼓励这样做 http docs scala lang org sips pending futures promises html 背后的想法ForkJoinPool是
  • 为什么backgroundColor应用到了我不想要的地方| MUI 中的菜单组件 |样式组件

    我将 mui 中的菜单组件的背景颜色设置为粉红色 这就是我单击时看到的内容dashboard button 我预计这会为菜单添加背景颜色 但结果页面的一半变成了粉红色 如何将背景颜色应用于菜单 import as React from re
  • 在 Express 服务器上成功调用 api 后使用角度路由重定向页面

    在使用角度路由的单页面应用程序中 如何在 api 调用后重定向页面 就我而言 我想在用户调用登录 api 后将用户重定向到个人资料页面 所以这就是我认为可行的方法 但事实并非如此 在客户端 main js 我已经设置了角度路由 app co
  • 使用 PHP 查看 HTML 中的 base64 编码的 blob

    我遇到了一些麻烦 我正在尝试查看以 blob 形式保存在我的服务器上的 Base64 编码图像 我想使用图像标签查看内部内容 或者将其作为 html 文件回显 我可以使用图像标签引用该文件 问题是 blob 数据从我的服务器返回错误
  • 如何使用C#graphics.DrawString绘制unicode字符串

    我正在尝试使用 NET 框架提供的 PrintDocument 将高棉脚本 unicode 字符串发送到打印机 不幸的是 在我看来 Graphics DrawString 无法正确渲染高棉脚本 平台 Windows 7旗舰版IDE VS 2
  • 每次打开 Xcode 时 Xcode 都会意外退出

    当我打开 Xcode 版本 7 3 1 时显示此消息 Xcode 意外退出 我测试这个命令 sudo gem 安装 cocoapods sudo 应用程序 Xcode xcode 我看到这个链接但对我不起作用在此输入链接描述 https s
  • 为什么 std::is_copy_constructible_v> 为 true?

    在我的 clang 和 libc 版本中 接近HEAD this static assert passes static assert std is copy constructible v
  • 如何将应用程序生成的文件存储在Android的“下载”文件夹中?

    我正在我的应用程序中生成一个 Excel 工作表 生成后应自动保存在 下载 通常保存所有下载的任何 Android 设备的文件夹 我有以下内容将文件保存在 我的文件 文件夹下 File file new File context getEx
  • 在循环中评估 Tensorflow 操作非常慢

    我试图通过编码一些简单的问题来学习张量流 我试图使用直接采样蒙特卡罗方法找到 pi 的值 运行时间比我想象的要长得多for loop去做这个 我看过其他关于类似事情的帖子 并且我尝试遵循解决方案 但我认为我仍然一定做错了什么 下面附上我的代
  • Java 中用于拖动组件的 Swing 库

    我正在尝试创建一种图形编辑器 允许用户创建美式足球比赛的图形描述 为此 用户应该能够执行以下操作 1 单击鼠标左键并移动图像 2 更改图像 圆形 方形和线条 3 重置所有对象的大小 理想情况下 我希望能够添加可调整的颜色和线条粗细 但这还很