2014-03-19 50 views
-1

我有一個快速的問題,我有這個代碼來計算貨幣收入和支出(對於一個企業),我已經使用了JOptionPane,這個對話框顯示對話框,我希望它顯示所有的它在一個屏幕/對話框中,我必須使用JFrame,有沒有什麼辦法可以編輯和轉換項目到JFrame,而無需再次編碼整個事情? 我的代碼:從JOptionPane更改爲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

您已爲所有內容創建了JOptionPanes。一個JFrame只需要一個自己的實例,它爲所有事物保存一個範圍。您將不得不取出JOptionPanes並將佈局應用於JFrame以獲得預期的結果。 –

回答

1

我也不太清楚爲什麼你做了這一切爲的JOptionPane的?如果你希望用戶繼續我建議使用一個JFrame與JPanel並重新代碼的應用程序之前輸入的信息 -

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

然後設置一個按鈕與動作監聽來獲取你需要點擊信息和按照你的意願去做。