Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (546)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Event handling problem  (Read 1501 times)
0 Members and 1 Guest are viewing this topic.
Offline Leppy

Senior Newbie




Java games rock!


« Posted 2004-04-28 23:40:42 »

Hi, I'm relatively new to java, and brand new to these forums. In a simple program i'm writing just to test out my skills(what little I have). It's a simple chat program in which the program asks how you are doing, the user types something in the field, and depending on the answer(good great etc.) it will have a reply. The problem i'm having is that I can't get the program to respond to the input. I know that the listeners and stuff are correct because I used System.out.println() where the argument is the text in the field, and that works fine. If you want, I can post the source code. This probably has a very simple answer, but for now I'm stumped. :-/
                                              ,Leppy
Offline Serethos

Junior Member




Java games rock!


« Reply #1 - Posted 2004-04-29 18:36:33 »

better than saying to post the code snippet -->
posting the code snippet !
Offline Leppy

Senior Newbie




Java games rock!


« Reply #2 - Posted 2004-04-29 21:25:17 »

Huh? Huh

I'll post the code as soon as I can. Thank you for answering!

*edit: here's the code(It's in the noob section for a reason Wink)


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

public class Chat extends JFrame implements ActionListener {
     String questiont = "How are you doing today?";
     JTextField response = new JTextField(40);
     JLabel question = new JLabel(questiont);
     JButton send = new JButton ("Send!");
     

     public Chat() {
           super();
           setSize(350, 100);
           setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           response.addActionListener(this);
           send.addActionListener(this);
           Container pane = getContentPane();
           BorderLayout bord = new BorderLayout();
           pane.add(question, BorderLayout.NORTH);
           pane.add(response, BorderLayout.CENTER);
           setContentPane(pane);
           setVisible(true);
     }
     

     public void actionPerformed(ActionEvent input) {
           String evt = input.getActionCommand();
           String answer = evt;
           System.out.println(evt);
           
           if (evt == "good") {
                 String questiont = "Not great?";
           } else if (evt == "great") {
                 String questiont = "Great!";
           }else if (evt == "Terrible") {
                 String questiont = "That's too bad";
           } else if (evt == "bad") {
                 String questiont = "I hope you cheer up";
                 
           } else {
                 String questiont = "Could you repeat that? I couldn't understand";
           }
           
           
           
     }


     public static void main(String[] arguments) {
           Chat chat1 = new Chat();
     }
           
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline D.t.O

Junior Member




Psych'd about Java Games


« Reply #3 - Posted 2004-04-30 01:43:46 »

Instead of setting questiont in the actionlistener method, call question.setText("blah"); [not sure if it's setText, setLabel, or whatever]. Strings are immutable.

Enjoy.
Regards,
     - D.t.O
Offline Leppy

Senior Newbie




Java games rock!


« Reply #4 - Posted 2004-04-30 01:57:14 »

hmmm, now it just goes to the doesn't understand line Huh

here's what I changed(it's setText() BTW)

Quote
public void actionPerformed(ActionEvent input) {
           String evt = input.getActionCommand();
           String answer = evt;
           System.out.println(evt);
           if (evt == "good") {
                 question.setText("I'm glad to hear that.");
           } else if (evt == "great") {
                 question.setText("Great!");
           }else if (evt == "Terrible") {
                 question.setText("That's too bad");
           } else if (evt == "bad") {
                 question.setText("I hope you cheer up");
                 
           } else {
                 question.setText("Could you repeat that? I couldn't understand");
           }
           
           
           
     }
[/color]
Offline javawillie

Junior Member




Hola, Paco.


« Reply #5 - Posted 2004-04-30 06:22:40 »

Instead of (evt == "Good"), try ("Good".equals(evt)).  (The main point is to use the equals() method.  I've just switched the order to follow a common idiom that makes it impossible to get a NullPointerException.)

Willie
Offline Leppy

Senior Newbie




Java games rock!


« Reply #6 - Posted 2004-04-30 17:49:33 »

Thanks for the help! It works now. i had a lot of projects on hold because of this thank you. Grin
Offline cfmdobbie

Senior Member


Medals: 1


Who, me?


« Reply #7 - Posted 2004-05-01 10:27:29 »

In case anyone else has a similar problem, this is what went wrong:

The == operator tests reference equality, while the equals() method usually tests object equality.  Two objects may to all intents and purposes be the same, but if they are actually different objects, then the references will not be the same, so the == test will return false.

However, if you perform the obvious test to confirm this, you'll get a different result!

1  
2  
3  
4  
5  
public static void main(String[] args){
  String s1 = "Hello";
  String s2 = "Hello";
  System.out.println(s1 == s2);
}


This will actually output "true".

There are two things that lead to this non-obvious result:

  • Strings are immutable
    As there is no difference between the two "Hello" objects, the compiler is allowed to create a single instance and point both references at it.
  • String.intern()
    The String class maintains a pool of String objects, and all String literals are placed into it.  Even if the compiler didn't share the instances, both references will be pointing at the same object anyway.


In summary, use equals() when you want to check whether two objects represent the same information; use == when you want to check whether two references are pointing at the same object.

Hellomynameis Charlie Dobbie.
Offline D.t.O

Junior Member




Psych'd about Java Games


« Reply #8 - Posted 2004-05-01 17:30:06 »

Quote
[...]use == when you want to check whether two references are pointing at the same object.

...which you probably never want to do. IMHO, even if you are sure that two Strings are pointing to the same object, it's safer to use .equals()

Enjoy.
Regards,
     - D.t.O
Offline Leppy

Senior Newbie




Java games rock!


« Reply #9 - Posted 2004-05-01 22:44:26 »

Cheesy that's a great summary! I will definiatly remember that. I am very grateful for you help!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (21 views)
2014-08-19 09:29:53

Tekkerue (21 views)
2014-08-16 06:45:27

Tekkerue (21 views)
2014-08-16 06:22:17

Tekkerue (12 views)
2014-08-16 06:20:21

Tekkerue (19 views)
2014-08-16 06:12:11

Rayexar (55 views)
2014-08-11 02:49:23

BurntPizza (37 views)
2014-08-09 21:09:32

BurntPizza (28 views)
2014-08-08 02:01:56

Norakomi (35 views)
2014-08-06 19:49:38

BurntPizza (65 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!