If-else condition within a JButton ActionEvent

拜拜、爱过 提交于 2019-12-23 05:29:04

问题


I've just uploaded my progress into the program. Please have a look and see what is lacking in it. Also, please ignore the lame joke from the strings in it while you're at it. It can be downloaded from this link: http://www.mediafire.com/?n0sp8v22egfsx7t

Note that I used NetBeans to easily make the program.

Edited:

How do I stop the ActionEvent method of a JButton from re-iterating the reading of the first layer of the nested if-else condition once the innermost line or last line of codes is read?

Also, as an additional question, how do I prompt the system to choose between choices thus literally branch out from the other once a choice is given? Apparently what I'm doing was just connect these block of strings together and not branch them out thus appear iterated once a new block of codes is to be displayed in connection to the previous block of code displayed. .

Snippet:

// Strings of text displayed on the JTextArea is shown here
// prompting to choose between two options: example, left or right

jButton1.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent e)
    {
       jButton1actionPerformed(e);
    }
});


private void jButton1actionPerformed(ActionEvent e) 
{                                             
    if(jRadioButton1.isSelected()) 
    {     
       // Display strings of text as response to the choice left
       // Another options to choose from here:, example, up or down
       JTextArea.setText("You've chosen: " );
       JTextArea.append("Now choose between: ");

       if (jRadioButton1.isSelected())
       {
         JTextArea.append("From first choice, choose between: ");
         // stop from here
       }

       else if (jRadioButton2.isSelected())
       {
         //same as above
       }
     }

     if (jRadioButton2.isSelected())
     {
        // Same as above
     }
}

回答1:


Use JTextField.setText("") whenever you want to clean a text area. Regardind the main issue - Post some code..




回答2:


How do I stop the ActionEvent method of a JButton from re-iterating the reading of the first layer of the nested if-else condition once the innermost line or last line of codes is read?

If I understand what you are asking, this is the whole point of an if ... else if ... else if ... (etc) ... else statement. When one of the conditions is matched, none of the other blocks is executed. So it looks like you just need to add elses to match your ifs. For example:

// Strings of text displayed on the JTextArea is shown here
// prompting to choose between two options: example, left or right

jButton1.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent e)
    {
       jButton1actionPerformed(e);
    }
});


private void jButton1actionPerformed(ActionEvent e) 
{                                             
    if(jRadioButton1.isSelected()) 
    {     
       // Display strings of text as response to the choice left
       // Another options to choose from here:, example, up or down
       JTextArea.setText("You've chosen: " );
       JTextArea.append("Now choose between: ");

       if (jRadioButton1.isSelected())
       {
         JTextArea.append("From first choice, choose between: ");
         // stop from here
       }

       else if (jRadioButton2.isSelected())
       {
         //same as above
       }
     }

     else if (jRadioButton2.isSelected()) // ***** Note the "else" added here.
     {
        // Same as above
     }
}

However, there is a problem with your code. Why are you checking two times if jRadioButton1 is selected? After the first if statement, you already know it is selected and don't need to check again.




回答3:


I think if you are asking how to stop the action even once you hit the condition, all you need to do is add a return statement to your code. For example, if button 1 is pushed, display message, then add a return statement, that will result in the code exiting that method and not getting to the other if statement.

Maybe you want to use something like a Hash, or List to store your choices and each time a choice is made, add it to the List. Then you can always find the last choice made by getting our last Element of the list. I'm not real clear on what you are trying to make here, but maybe that will give you some ideas.


I wrote a small program here that has 2 radio buttons and a submit. When you click submit it updates the text in the window based on the radio button selection.

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;


public class MyWindow extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MyWindow frame = new MyWindow();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public MyWindow() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        final JRadioButton rdbtnNewRadioButton = new JRadioButton("Choice 1");
        rdbtnNewRadioButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        rdbtnNewRadioButton.setSelected(true);
        rdbtnNewRadioButton.setBounds(5, 7, 424, 23);
        contentPane.add(rdbtnNewRadioButton);

        JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("Choice 2");
        rdbtnNewRadioButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        rdbtnNewRadioButton_1.setBounds(5, 33, 109, 23);
        contentPane.add(rdbtnNewRadioButton_1);

        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(rdbtnNewRadioButton);
        buttonGroup.add(rdbtnNewRadioButton_1);

        final JTextArea textArea = new JTextArea();
        textArea.setBounds(57, 80, 321, 94);
        contentPane.add(textArea);

        JButton btnNewButton = new JButton("Submit");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (rdbtnNewRadioButton.isSelected()) {
                    textArea.setText("Choice 1 Selected.");
                } else {
                    textArea.setText("Choice 2 Selected.");
                }
            }
        });

        btnNewButton.setBounds(135, 212, 89, 23);
        contentPane.add(btnNewButton);
    }
}

Check out the Java Tutorials on using buttons. Specifically look at the How to use Radio Buttons section to see if that sheds any light on how you would design it.

How to use Radio Buttons



来源:https://stackoverflow.com/questions/12683333/if-else-condition-within-a-jbutton-actionevent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!