I am trying to take the values from the text fields below to use with parent.addNewRoom(roomNo,roomEnSuite);
but roomEnSuite is a Boolean value in the parent class. What is the correct procedure to get a Boolean from a JTextField?
public void actionPerformed( ActionEvent ae)
{
String item = ae.getActionCommand();
if ( item.equals("Confirm"))
{
String roomNo = nameJTextField.getText();
String roomEnSuiteS = idJTextField.getText();
parent.addNewRoom(roomNo,roomEnSuite);
this.dispose();
}
else if ( item.equals("Cancel"))
{
parent.resetButtons();
this.dispose();
}
}
To give a full answer from my above comments:
Handling boolean
input using a JTextField
would not be a good way to go about things as there are many variations the user could type yes
/ no
/ true
/ false
, etc. mispelling?
Using a JRadioButton
(for single answers) or JCheckbox
(for multiple answers) would be a better way to go about handling true
or false
input. I would suggest a JRadioButton
as you wouldn't want the user checking true
and false
.
http://docs.oracle.com/javase/tutorial/uiswing/components/button.html
假设用户输入字符串true
或false
,则可以使用以下命令将其转换为boolean
:
boolean value = Boolean.parseBoolean(idJTextField.getText());
A JTextField
is meant to provide String
s. So unless you want the user to type true
or false
in the textfield (or whatever string you will parse to a boolean), there are better options available
JCheckBox
, which is typically used for toggle settings, like true-false JRadioButton
s (one for each setting, so two in this case) And here a link to the corresponding Swing tutorial with examples on how to use these buttons
But if you really want to got with a textfield, then you should get the text from it and parse it by using for example Boolean.valueOf
只要输入的值始终为true或false,就可以使用布尔值;
boolean value = Boolean.parseBoolean(enSuiteJTextField.getText());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.