swing - Learning Event handling in Java -


I'm learning event handling using the Java Swing class. I have tried various combinations based on the tutorial from the new Boston homepage. I have made Class Defined Panel, Jebtons, Jettextlds and Jellbell. I have created a class named "handler" which has ActionPerfomed-method, and I've added two textfield (item 1 and item 2) in the addActionListener method. When I run my program, I get the program to do two textfolds (which indicates a choice of the desired text. Shomos Dialliog box).

The problem comes when I want a massage pens. When I push one button to show a certain text - window: - "You pushed the button number 1!" Instead of displaying the text below. As I hope my code will be, an empty apppoint instead Shomosse delog-window appears. / P>

I know what an experienced Java programmer would expect to be wrong in my code. Here is the part of the code that should be wrong:

  Private class Thandler implementation implementation (If event.getSource () == item1) text = String.format ("You've written% s in field 1!", Event.getActionCommand ()); Else if (event.getSource () == item2) text = String.format ("You've written% 2 in field 2!", Event.getActionCommand ()); Else if (event.getSou Rce () == button1) text = "You pushed the button number 1!"; Else if (event.getSource () == Button 2) text = "You pushed button number 2!"; Employmentpan Shmosse Delog (empty, lesson);    

first add a System.out.println () Your Joppen Call:

  System.out.println ("text =" + text); Employmentpan Shmosse Delog (empty, lesson);   

I'm sure that you think text is an empty string. Why? Your object reference may be empty.

Another approach, which can solve your problem and does not rely on object references, is to use the action command:

  button1.setActionCommand (" DoAction1 "); Public Zero ActionPerfed (Action Event Event) {String Action Command = Event.actioncomment (); If (actionCommand.equals ("doAction1")) // Press 1 button}}    

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -