[英]Display Jtextfield on JOptionPane.showMessageDialog
I'm having trouble in display the Jtextfield on JOptionPane.showMessageDialog. 我在显示JOptionPane.showMessageDialog上的Jtextfield时遇到麻烦。 It shows something like Name: javax swing.Jtextfield[,0,19,195x20,invalid.layout......... Section: javax swing.Jtextfield[,0,19,195x20,invalid.layout......... Here is the code:
它显示类似名称的内容:javax swing.Jtextfield [,0,19,195x20,invalid.layout .........部分:javax swing.Jtextfield [,0,19,195x20,invalid.layout ..... ....这是代码:
package quiz_application;
import java.awt.Component;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
/**
*
* @author Christian
*/
public class Quiz_application {
public static void main(String[] args){
/**
* Input
*/
JTextField fullName = new JTextField();
JTextField section = new JTextField();
Object[] message = {
"Name:", fullName,
"Section:", section,
};
Component parent = null;
int option = JOptionPane.showConfirmDialog(parent, message,"User Information", JOptionPane.OK_CANCEL_OPTION);
if (option == JOptionPane.OK_OPTION)
{
String value1 = fullName.getText();
String value2 = section.getText();
}
String outputStr="Name: "+ fullName+"\n"
+"Section: "+ section;
JOptionPane.showMessageDialog(null, outputStr,"User Information",JOptionPane.INFORMATION_MESSAGE);
}
}
You're printing toString()
of both JTextField
. 您正在打印两个
JTextField
toString()
。 I believe you'll only print the User Information dialog if there's one. 我相信您只有在出现“用户信息”对话框的情况下才会打印。 So, change the
if
statement to around all the rest of the code and now pass value1
and value2
to outputStr
instead of fullName
and section
. 因此,将
if
语句更改为其余所有代码,然后将value1
和value2
传递给outputStr
而不是fullName
和section
。 Or, just call fullName.getText()
and section.getText()
. 或者,只需调用
fullName.getText()
和section.getText()
。 Like that: 像那样:
EX1 例1
String value1 = fullName.getText();
String value2 = section.getText();
String outputStr = "Name: " + value1 + "\n" + "Section: " + value2;
EX2 例2
String outputStr = "Name: " + fullName.getText() + "\n" + "Section: " + section.getText();
Expanded If
Statement 扩展
If
语句
if (option == JOptionPane.OK_OPTION) {
String value1 = fullName.getText();
String value2 = section.getText();
String outputStr = "Name: " + value1 + "\n" + "Section: " + value2;
JOptionPane.showMessageDialog(null, outputStr, "User Information",
JOptionPane.INFORMATION_MESSAGE);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.