在JOptionPane.showMessageDialog上显示Jtextfield(Display Jtextfield on JOptionPane.showMessageDialog)

编程入门 行业动态 更新时间:2024-10-27 06:34:22
在JOptionPane.showMessageDialog上显示Jtextfield(Display Jtextfield on JOptionPane.showMessageDialog)

我在显示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); }

更多推荐

本文发布于:2023-04-29 11:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336429.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:showMessageDialog   JOptionPane   Display   Jtextfield

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!