在JOptionPane.showMessageDialog上显示Jtextfield

时间:2016-12-27 14:41:25

标签: java

我在JOptionPane.showMessageDialog上显示Jtextfield时遇到了麻烦。 它显示了类似的东西 名称: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);
  }
 }

1 个答案:

答案 0 :(得分:0)

您正在打印toString() JTextField if。我相信你只会打印用户信息对话框(如果有的话)。因此,将value1语句更改为围绕所有其余代码,现在将value2outputStr传递给fullName,而不是section和{{1} }。或者,只需致电fullName.getText()section.getText()即可。像那样:

<强> EX1

String value1 = fullName.getText();
String value2 = section.getText();
String outputStr = "Name: " + value1 + "\n" + "Section: " + value2;

<强> EX2

String outputStr = "Name: " + fullName.getText() + "\n" + "Section: " + section.getText();

扩展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);
    }