Note that there are some explanatory texts on larger screens.

plurals
  1. POIF statement inside ActionPerformed method in JAVA
    primarykey
    data
    text
    <p>I'm trying to build a basic game where the user's knowledge of State capitals is tested. At this stage I'm just trying to set up some basic functionality for the buttons.</p> <p>I'm trying to get it so that when the user types in a string which corresponds to a correct answer the program displays "correct!". I've tried using an IF ELSE statement but even when I type in the right answer it outputs "WRONG!". It's probably something simple but I can't spot it. If someone could point me in the right direction that would be much appreciated. I'm very much a beginner so perhaps this completely the wrong way to go about it but here is what I have so far:</p> <pre><code>public class Main { public static void main(String[] args){ Gui a = new Gui(); } } import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Gui extends JFrame{ private static final long serialVersionUID = 1L; JPanel row1 = new JPanel(); JLabel instructions = new JLabel("What is the capital of Alabama?"); JPanel row2 = new JPanel(); JLabel aLabel = new JLabel("Answer: "); JTextField aField = new JTextField(14); JPanel row3 = new JPanel(); JButton submit = new JButton("Submit"); JButton reset = new JButton("Reset"); public Gui() { super("State Capitals Game"); setLookAndFeel(); setSize(400, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); setResizable(false); setLayout(new GridBagLayout()); GridBagConstraints gc = new GridBagConstraints(); gc.insets = new Insets(5,5,5,5); gc.gridx = 0; gc.gridy = 0; add(instructions, gc); gc.gridx = 0; gc.gridy = 1; add(aLabel, gc); gc.gridx = 0; gc.gridy = 2; add(aField, gc); gc.gridwidth = 3; gc.fill = GridBagConstraints.HORIZONTAL; gc.gridx = 0; gc.gridy = 4; add(submit, gc); ///this is where I'm stuck//// submit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String userIP = aField.getText(); String mg = "Montgomery"; if(userIP == mg){ System.out.println("correct!"); }else{ System.out.println("WRONG!"); } } }); gc.gridwidth = 3; gc.fill = GridBagConstraints.HORIZONTAL; gc.gridx = 0; gc.gridy = 5; add(reset, gc); reset.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { reset.getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "pressed"); aField.setText(""); } }); } private void setLookAndFeel() { try { UIManager.setLookAndFeel( "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel" ); } catch (Exception exc) { // ignore error } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload