【问题标题】:How do I get my dialog box to show the output of my program?如何让我的对话框显示我的程序的输出?
【发布时间】:2016-12-15 08:22:04
【问题描述】:

我们最近在课堂上查看了对话框并分配了家庭作业。

任务是设计和实现一个应用程序,该应用程序使用对话框获取两个整数值(每个值一个对话框)并显示这些值的总和和乘积。使用另一个对话框询问用户是否要处理另一对值。

到目前为止,我尝试执行该项目,但在显示答案的一行代码中遇到问题。我正在使用 NetBeans IDE,并使用了显示方法 JOptionPane.showConfirmDialog 来显示我的答案。它一直给我一个错误,说“没有找到适合showMessageDialog 的方法”。我试图使用System.out.println,但它也给了我错误,所以我回到了这个方法。你能解释一下如何修复它以及为什么我的代码是错误的吗?

这是我目前的代码:

package DialogBoxes;

import javax.swing.JOptionPane;
/**
 *
 * @author Tony
 */

public class SumProduct {
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    String askNum1, askNum2, answerSum, answerPro;
    int num1, num2, repeat;

    do 
    { 
        askNum1 = JOptionPane.showInputDialog ("Enter your first integer:");
        num1 = Integer.parseInt(askNum1);

        askNum2 = JOptionPane.showInputDialog("Enter your second integer:");
        num2 = Integer.parseInt(askNum2);

        answerSum = "The sum is: " + ((num1 + num2));
        answerPro = "The product is: " + ((num1 * num2));

        JOptionPane.showMessageDialog(null, answerSum, answerPro);

        repeat = JOptionPane.showConfirmDialog(null, "Would you like to test another set of numbers?");           
    }
    while (repeat == JOptionPane.YES_OPTION);
}
}

【问题讨论】:

    标签: java swing dialog joptionpane


    【解决方案1】:

    你可以使用JOptionPane.showMessageDialog(parentComponent, message);方法

    你需要创建一个String,其产品和总和如下,并将其传递给showMessageDialog

    JOptionPane.showMessageDialog(null, answerSum + "  " + answerPro);
    

    【讨论】:

      【解决方案2】:

      看看 JOptionPane API:https://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html#showMessageDialog(java.awt.Component,%20java.lang.Object)

      似乎没有只接受 3 个参数的 showMessageDialog 方法。您需要使用 API 提供的方法之一。

      【讨论】:

        【解决方案3】:

        请试试这个。编码愉快。

        package com.pearson.nextgen.aggregatedsessionservice.web.rest;
        
        import javax.swing.JFrame;
        import javax.swing.JOptionPane;
        
            public class stacktest {
        
                public static void main(String[] args) {
                    // TODO code application logic here
                    String askNum1, askNum2, answerSum, answerPro;
                    int num1, num2, repeat;
        
                    do 
                    { 
                        askNum1 = JOptionPane.showInputDialog ("Enter your first integer:");
                        num1 = Integer.parseInt(askNum1);
        
                        askNum2 = JOptionPane.showInputDialog("Enter your second integer:");
                        num2 = Integer.parseInt(askNum2);
        
                        answerSum = "The sum is: " + ((num1 + num2));
                        answerPro = " The product is: " + ((num1 * num2));
        
                        JFrame frame = new JFrame("TestFrame");
                        JOptionPane.showMessageDialog(null, answerSum + answerPro);
        
                        repeat = JOptionPane.showConfirmDialog(null, "Would you like to test another set of numbers?");           
                    }
                    while (repeat == JOptionPane.YES_OPTION);
        
                }
        
            }
        

        【讨论】:

        • 既然他说这是他作业的一部分,我建议不要为他提供完整的解决方案。让他自己弄清楚——那样他会学到更多! :)
        • 我解决了我的问题,但这段代码看起来很有趣。感谢您展示另一种解决方法。我需要了解 JFrame 是什么。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多