我在显示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); } }I'm having trouble in display the Jtextfield on JOptionPane.showMessageDialog. 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:
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); } }最满意答案
您正在打印两个JTextField toString() 。 我相信你只会打印出用户信息对话框。 因此,将if语句更改为其余所有代码,然后将value1和value2传递给outputStr而不是fullName和section 。 或者,只需调用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); }You're printing toString() of both JTextField. 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. Or, just call fullName.getText() and section.getText(). Like that:
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); }更多推荐
发布评论