2016-12-27 15 views
0

Ich habe Probleme bei der Anzeige der Jtextfield auf JOptionPane.showMessageDialog. Es zeigt so etwas wie Name: javax swing.Jtextfield [, 0,19,195x20, invalid.layout ......... Bereich: javax swing.Jtextfield [, 0,19,195x20, invalid.layout .. ....... Hier ist der Code:Jtextfield auf JOptionPane.showMessageDialog anzeigen

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); 
    } 
} 
+0

' "Name: "+ fullname +" \ n"' es sollte sein "" Name: "+ fullName.getText() +" \ n "' –

+0

bitte verbessern Formation in Header –

Antwort

0

Sie drucken toString() sowohl JTextField. Ich glaube, dass Sie nur den Benutzerinformationen-Dialog drucken werden, wenn es einen gibt. Also, ändern Sie die if Anweisung um den ganzen Rest des Codes und übergeben Sie jetzt und value2 an outputStr anstelle von fullName und section. Oder rufen Sie einfach fullName.getText() und section.getText(). Wie folgt aus:

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(); 

Expanded If Statement

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); 
    } 
+0

Schließlich, vielen Dank. Ich habe versucht, fullName und Abschnitt zu value1 und value2 zu ändern, dann habe ich gerade herausgefunden, dass ich die geschweiften Klammern an die falsche Position gesetzt habe. –

Verwandte Themen