我一直在尋找幾個小時爲我的問題的解決方案,我有點Java的languaje新的編程,並已設法創建一個自定義面板,其中包含6自定義按鈕,但現在我需要創建一個可以檢測到我按下的事件的事件,我已經使用C#完成了這一操作,但無法在Java中找到方法。如何創建自定義事件(他們必須在netbeans調色板)
這段代碼是用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」這樣我就可以選擇該事件,並會生成的代碼。
我會apreciatte很多任何幫助,你可以給我。
非常感謝,如果我的英語不是很好,還在學習。 :)
當我在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;
}
}
我會一個ActionListener簡單地添加到每個JButton的,也許是一個匿名內部類。查看ActionListener和JButton教程以獲得更多關於如何執行此操作的信息:[ActionListener教程](http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html),[JButton教程](http: //docs.oracle。com/javase/tutorial/uiswing/components/button.html) –
*「JAVA中的新編程」*爲了將來參考,它是Java,而不是JAVA。 –