So I'm using the code below to to make a frame with radio buttons and what not. I've pretty much just modeled this after some code in my programming book so it looks weird. =/
I've implemented this into my other code so that it displays when I need to prompt the user for a response, which I originally got via scanner. I want this to update the users response automatically with a preset response based by selection of one of the radio buttons. As you can see I've done this in itemStateChanged method. But my problem is the program won't stop and wait for the user to give a response via selecting a radio button. What kind of options do I have to make it wait for that response? I honestly have no clue what to do? I could say if the response == 0 instead of 1 2 or 3 then pause somehow? but then how would it know to go again?
On a side note, I'm using ItemListener and fonts and ItemEvent that change the text above. Which is completely useless to me for what I'm trying to accomplish.. -.- It was part of the other program I modeled this after.
But I decided to use them since the font is an Item I guess? That works with the stuff mentioned above. Soo.. any hints or samples I could use to pass the response from the radio buttons to the itemStateChanged method correctly? Can I create other kinds of objects like this: loginFont = new Font("Serif", Font.PLAIN, 14); with string or something different? cause fonts is a little ridiculous.
.. I swear I had another question. But it's not coming to me right now..
edit: Oh yes, how can I ensure the frame closes when I'm done with it? What kind of code is there to do this?
Thanks for any help! =)
import java.awt.*; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.*; import javax.swing.*; public class NewLogin extends JFrame { private Font loginFont; private Font registerFont; private Font exitFont; private JTextField textField; private JRadioButton loginButton; private JRadioButton registerButton; private JRadioButton exitButton; private ButtonGroup radioGroup; Login Login = new Login(); public NewLogin() { super("Super Game"); setLayout(new FlowLayout()); // set frame layout textField = new JTextField("Which action would you like to perform?", 30); add(textField); // add textField to Jframe // create radio buttons loginButton = new JRadioButton("Login", false); registerButton = new JRadioButton("Register", false); exitButton = new JRadioButton("Exit", false); add(loginButton); add(registerButton); add(exitButton); // create logical relationship between JRadioButtons radioGroup = new ButtonGroup(); // create button group radioGroup.add(loginButton); radioGroup.add(registerButton); radioGroup.add(exitButton); // create font objects loginFont = new Font("Serif", Font.PLAIN, 14); registerFont = new Font("Serif", Font.BOLD, 14); exitFont = new Font("Serif", Font.ITALIC, 14); // register events for JRadioButtons loginButton.addItemListener(new RadioButtonHandler(loginFont)); registerButton.addItemListener(new RadioButtonHandler(registerFont)); exitButton.addItemListener(new RadioButtonHandler(exitFont)); }// end RadioButtonFrame private class RadioButtonHandler implements ItemListener { private Font font; public RadioButtonHandler(Font f) { font = f; } public void itemStateChanged(ItemEvent event) { textField.setFont(font); if (font == loginFont) { Login.response1 = 1; } else if (font == registerFont) { Login.response1 = 2; } else if (font == exitFont) { Login.response1 = 3; } } }// end RadioButtonHandler }// end NewLogin