从JOptionPane更改为JFrame

时间:2014-03-19 13:06:30

标签: java jframe joptionpane

我有一个简单的问题,我有这个代码来计算货币收入和支出(对于一个企业),我使用了JOptionPane,这显示了对话框的对话框,我希望它在一个屏幕上显示所有这些/对话框,我必须使用JFrame,有没有什么方法可以编辑和转换项目到JFrame而无需再编码整个事情? 我的代码:

import javax.swing.JOptionPane;

public class expense_calc {

public static void main(String[] args) {

    // COUNTED MONEY
    int yes_no = JOptionPane.showConfirmDialog(null,
            "Have you counted the 150,000/=?", "User Input",
            JOptionPane.YES_NO_OPTION);

    if (yes_no == JOptionPane.YES_OPTION) {
        JOptionPane.showMessageDialog(null, "Continue!");
    } else {
        JOptionPane.showMessageDialog(null, "Count it First!");
        System.exit(0);
    }

    String tenthousand = JOptionPane.showInputDialog(null,
            "Enter the full 10,000/= note bundles you counted: ",
            "User Input", JOptionPane.QUESTION_MESSAGE);
    int ten_thousand = Integer.parseInt(tenthousand);
    JOptionPane
            .showMessageDialog(null,
                    "The amount of 10,000/= bundles you counted is "
                            + ten_thousand + "/=", "User Message",
                    JOptionPane.INFORMATION_MESSAGE);

    String fivethousand = JOptionPane.showInputDialog(null,
            "Enter the full 5,000/= note bundles you counted: ",
            "User Input", JOptionPane.QUESTION_MESSAGE);
    int five_thousand = Integer.parseInt(fivethousand);
    JOptionPane
            .showMessageDialog(null,
                    "The amount of 5,000/= bundles you counted is "
                            + five_thousand + "/=", "User Message",
                    JOptionPane.INFORMATION_MESSAGE);

    String extraten = JOptionPane.showInputDialog(null,
            "Enter the extra 10,000/= notes you counted: ", "User Input",
            JOptionPane.QUESTION_MESSAGE);
    int extra_ten = Integer.parseInt(extraten);
    JOptionPane.showMessageDialog(null,
            "The amount of extra 10,000/= notes you counted is "
                    + extra_ten + "/=", "User Message",
            JOptionPane.INFORMATION_MESSAGE);

    String extrafive = JOptionPane.showInputDialog(null,
            "Enter the extra 5000/= notes you counted: ", "User Input",
            JOptionPane.QUESTION_MESSAGE);
    int extra_five = Integer.parseInt(extrafive);
    JOptionPane.showMessageDialog(null,
            "The amount of extra 5,000/= notes you counted is "
                    + extra_five + "/=", "User Message",
            JOptionPane.INFORMATION_MESSAGE);

    String dollars = JOptionPane.showInputDialog(null,
            "Enter the amount of US Dollar you counted: ", "User Input",
            JOptionPane.QUESTION_MESSAGE);
    int usd = Integer.parseInt(dollars);
    String usdrate = JOptionPane.showInputDialog(null,
            "Enter the dollar rate: ", "User Input",
            JOptionPane.QUESTION_MESSAGE);
    int usd_rate = Integer.parseInt(usdrate);

    JOptionPane.showMessageDialog(null,
            "The converted amount of usd in tsh you counted is "
                    + (usd * usd_rate) + "/=", "User Message",
            JOptionPane.INFORMATION_MESSAGE);

    // EXPENSES
    String lunchmoney = JOptionPane.showInputDialog(null,
            "Enter the lunch expenses: ", "User Input",
            JOptionPane.QUESTION_MESSAGE);
    int lunch = Integer.parseInt(lunchmoney);
    JOptionPane
            .showMessageDialog(null, "The amount given on lunch is "
                    + lunch + "/=", "User Message",
                    JOptionPane.INFORMATION_MESSAGE);

    String otherexpense = JOptionPane.showInputDialog(null,
            "Enter other expenses: ", "User Input",
            JOptionPane.QUESTION_MESSAGE);
    int other_expense = Integer.parseInt(otherexpense);
    JOptionPane.showMessageDialog(null, "The amount of other expenses is "
            + other_expense + "/=", "User Message",
            JOptionPane.INFORMATION_MESSAGE);

    // TOTAL
    JOptionPane
            .showMessageDialog(
                    null,
                    "The total amount today is "
                            + ((150000 + ten_thousand + five_thousand
                                    + extra_ten + extra_five + (usd * usd_rate)) - (lunch + other_expense)),
                    "User Message", JOptionPane.INFORMATION_MESSAGE);
}

public static void counted() {

    int yes_no = JOptionPane.showConfirmDialog(null,
            "Have you counted the 150,000/=?", "User Input",
            JOptionPane.YES_NO_OPTION);

    if (yes_no == JOptionPane.YES_OPTION) {
        JOptionPane.showMessageDialog(null, "Continue!");
    } else {
        JOptionPane.showMessageDialog(null, "Count it First!");
        System.exit(0);
    }

    String tenthousand = JOptionPane.showInputDialog(null,
            "Enter the full 10,000/= notes you counted: ", "User Input",
            JOptionPane.QUESTION_MESSAGE);
    int ten_thousand = Integer.parseInt(tenthousand);
    JOptionPane.showMessageDialog(null, "The amount you counted is "
            + ten_thousand + "/=", "User Message",
            JOptionPane.INFORMATION_MESSAGE);

    String fivethousand = JOptionPane.showInputDialog(null,
            "Enter the full 5000/= notes you counted: ", "User Input",
            JOptionPane.QUESTION_MESSAGE);
    int five_thousand = Integer.parseInt(fivethousand);
    JOptionPane.showMessageDialog(null, "The amount you counted is "
            + five_thousand + "/=", "User Message",
            JOptionPane.INFORMATION_MESSAGE);

    String extraten = JOptionPane.showInputDialog(null,
            "Enter the extra 10,000/= notes you counted: ", "User Input",
            JOptionPane.QUESTION_MESSAGE);
    int extra_ten = Integer.parseInt(extraten);
    JOptionPane.showMessageDialog(null, "The amount you counted is "
            + extra_ten + "/=", "User Message",
            JOptionPane.INFORMATION_MESSAGE);

    String extrafive = JOptionPane.showInputDialog(null,
            "Enter the extra 5000/= notes you counted: ", "User Input",
            JOptionPane.QUESTION_MESSAGE);
    int extra_five = Integer.parseInt(extrafive);
    JOptionPane.showMessageDialog(null, "The amount you counted is "
            + extra_five + "/=", "User Message",
            JOptionPane.INFORMATION_MESSAGE);

    String extranotes = JOptionPane.showInputDialog(null,
            "Enter the extra notes you counted: ", "User Input",
            JOptionPane.QUESTION_MESSAGE);
    int extra_notes = Integer.parseInt(extranotes);
    JOptionPane.showMessageDialog(null, "The amount you counted is "
            + extra_notes + "/=", "User Message",
            JOptionPane.INFORMATION_MESSAGE);

}

}

1 个答案:

答案 0 :(得分:1)

我不太清楚你为什么要把它作为JOptionPane来完成的?如果您希望用户在继续之前输入信息,建议您使用带有JPanel的JFrame并重新编写应用程序代码 -

    JFrame frame = new JFrame();
    frame.setBounds(100, 100, 784, 533);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    // Use whatever layout suits you but I prefer mig
    frame.getContentPane().setLayout(new MigLayout("", "[344.00,grow,fill][-457.00]", "[grow]"));
    frame.setTitle("Title");
    frame.setAlwaysOnTop(true);

    JPanel panel = new JPanel();
    frame.getContentPane().add(panel, "cell 0 0,grow");

 // Then add Text fields and labels so the user knows what to enter

    JLabel lbl = new JLabel("Environment Name");
    panel .add(lbl, "cell 0 0");

    JTextField textField = new JTextField();
    panel.add(textField , "cell 0 1,growx");

然后设置一个带有动作监听器的按钮,以便在点击时检索所需的信息,并随意使用。