问题
In a Codename One project, say project "PRJ1" i created on NetBeans, I defined a file that i shouldn't have as an Action event.
Specifically, in the designer interface to edit "theme.res" (I'm using the latest version of NetBeans & Codename One), I right-clicked a button, then in the drop-down menu Events -> Action event, entered a .jar file.
Ever since, i'm getting the error:
Error opening Netbeans.java.IOException: Cannot run program "..\MyApp.jar" CreateProcess error=193, %1 is not a valid Win32 application
whenever i get anywhere near to events.
I deleted PRJ1 and created a PRJ2 from scratch. When i tried to define an action event again the same way, i ended up with the same error in the pop-up window right after I clicked Action event or any other one of the events. i've got the same thing when i define a new component on a new project. "MyApp.jar" seems to have stuck on my Codename One for good. i'm not being able to get rid of it.
My questions are:
How can i define an action solely on source code without using the designer interface?
When I define an action event the way i did by using the designer interface, how does this get into the code? I'm seeing the methods added in the StateMachine.java. From what i see, that is all there is as far as the source code goes. Is there anything else changing anywhere in .xml or other confign files? I manually searched the files in the project directory. No trace of MyApp.jar anywhere.
Thanks in advance.
I'm new to Codename One. This may be a simple Q, but burned so much of my time.
回答1:
You need to launch the designer, click File->Setup NetBeans and select netbeans.exe/.app.sh file from the NetBeans bin directory so the designer can trigger NetBeans.
You can create a handcoded application in the Wizard (second option on the bottom) where the designer is only used for the theme and not the GUI. You can handle events just like you do in Swing/AWT addActionListner
etc. on any component.
回答2:
I had the same problem. I assume this happens when you change the default directory for your installation (Something I do all the time)
First you should reset your settings. In the GUI editor, go to 'codename one' -> advanced -> reset netbean settings.
Next time you get the file dialogue, find and select netbeans.exe (or netbeans64.exe).
To answer your specific question on how to avoid using the GUI editor, you can manually add the event handler code in the StateMachine.java file. Like so:
@Override
protected void onMain_Button1Action(Component c, ActionEvent event) {
//do stuff
}
Just use the correct name of your button or component instead of Button1.
回答3:
The correct way to define an action event solely using code is like such:
If the button was defined in the GUI builder:
Button btn = findMyButton(form);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
// do something
}
});
To define the button in code as well:
Button btn = new Button('my button');
btn.addActionListener(new ActionListener() {
...as above
});
来源:https://stackoverflow.com/questions/22120764/codename-one-action-event