如何创建自定义事件(它们必须在 Netbeans 调色板中)



我一直在寻找几个小时的解决方案来解决我的问题,我有点新用 Java 语言编程,并且已经设法创建了一个包含 6 个自定义按钮的自定义面板,但现在我需要创建一个可以检测我按下的事件,我已经用 C# 完成了此操作,但在 Java 中找不到方法。

这段代码是用 C# 编写的,但当然我需要类似的东西来用于 Java。

/// <summary>
/// Enumerator with buttons states
/// </summary>
public enum EstadoBoton
{
    Aceptar = 1,
    Nuevo = 2,
    Editar = 3,
    Cancelar = 4,
    Eliminar = 5,
    Imprimir = 6,
    Salir = 7
}
/// <summary>
/// Delegate method for buttons events
/// </summary>
/// <param name="edoBoton"></param>
public delegate void BotonPresionado(EstadoBoton edoBoton);
public partial class ctrlBotones : UserControl
{
    /// <summary>
    /// here I keep the state of the button
    /// </summary>
    private EstadoBoton estadoBoton;
    /// <summary>
    /// Event that manages the state of the button
    /// </summary>
    public event BotonPresionado BotonPresionado
    {
        add { this.botonpresion += value; }
        remove { this.botonpresion -= value; }
    }
    private BotonPresionado botonpresion;
    public ctrlBotones()
    {
        InitializeComponent();
    }

该示例在 Visual Studio 的事件列表中显示了名称"BotonPresionado",因此我可以选择该事件并生成代码。

我会感谢你能给我的任何帮助。

提前感谢,如果我的英语不是很好,还在学习,很抱歉。 :)

当我在 GUI 上按下自定义事件时,我忘记编写生成的代码。

//As we can see, it generates en event for this control not for the buttons.
private void ctrlBotones1_BotonPresionado(HerramientasInterfaz.EstadoBoton edoBoton)
    {
        //I just use a switch to check wich button I pressed.
        switch (edoBoton)
        {
            case HerramientasInterfaz.EstadoBoton.Aceptar:
                Aceptar();
                break;
            case HerramientasInterfaz.EstadoBoton.Nuevo:
                Nuevo();
                break;
            case HerramientasInterfaz.EstadoBoton.Salir:
                Close();
                break;
        }
    }

好的,Aaron Daw i 在某种程度上你想创建自己的"事件"。这意味着对组件重用相同的代码。

我过去尝试过自定义组件(JButtons,JMenu)

public class MyMenu extends JMenu
{
public MyMenu(ImageIcon i,String s)
{
    super();
    setIcon(i);
    setText(s);
    setVerticalTextPosition(SwingConstants.BOTTOM);
    setHorizontalTextPosition(SwingConstants.CENTER);
}
}

为"MyMenu"创建对象,这里我们不需要每次都添加属性,这是优势。

以同样的方式,我尝试使用 JButton 的自定义侦听器它正在工作.当您单击按钮时,它将打印"按钮按下"这是自定义事件类

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;

public class CustomListener 
{
JButton button;
public CustomListener(JButton b)
{
button=b;
button.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==button)
        {
            System.out.println("button pressed");
        }
    }
});
}
}

这是主类

import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class MainClass extends JFrame
{
JButton jbutton;
public MainClass()
{
    setLayout(new FlowLayout());
    add(jbutton());
    setSize(800,600);
    setVisible(true);
}
public JButton jbutton()
{
    if(jbutton==null)
    {
        jbutton=new JButton("click");
        new CustomListener(jbutton);
    }
    return jbutton;
}
public static void main(String args[])
{
    new MainClass();
}
}

现在我可以重用 actionListener。

我的朋友

如果您正在寻找类似于Visual Studio用户界面,例如Easy Drag Drop

然后在 NetBeans 中右键单击您的项目,然后单击新建 JFrame 表单(如果不存在,则在其他表单中),您将拥有一个简单的拖放用户界面

然后从调色板中删除一个JButton,然后双击该JButton,您将获得该JButton的事件....

最新更新