Jump to content
erokun

Ada Sapa2 Boleh Tolong?

Recommended Posts

ada sapa2 boleh tolong aku tak? pening nih... haha :P

Penerangan pasal program nih: aku nak result terakhir kluar JOptionPane punye window yg ada vertical scrollbar..

tapi dr program yg aku buat nih.. tetingkap terakhir tu tak kluar pun scrollbar yg aku nak tu..

yg kluar pun result dia yg pjg berjela sampai tak muat screen... hahaha...

ada sapa2 boleh tolong tgk2kan coding ni tak?

mana tau aku tertinggal atau tersilap ke kan? hehe..

terima kasih.. :P

ada 2 file.. ni coding dia

[File1] JOptionPaneDemo.java

-----------------------------------------------------------------

// JOptionPaneDemo.java: Using standard dialogs
import javax.swing.*;
import java.awt.*;

public class JOptionPaneDemo {
    public static void main(String[] args) {
        // Create an array for annualinterest rates 
        Object[] rateList = new Object[25];
        int i = 0;
        for(double rate = 5; rate <= 8; rate += 1.0 / 8)
            rateList[i++] = new Double(rate);
        
        // Prompt the user to select an annual interest rate
        Object annualInterstRateObject = JOptionPane.showInputDialog(
         null,"Select annual interest rate:", "JOptionPane Demo",
         JOptionPane.QUESTION_MESSAGE, null, rateList, null);
        double annualInterestRate = ((Double)annualInterstRateObject).doubleValue();
        
        // Create an array for number of years
        Object[] yearList = {new Integer(7), new Integer(15),
         new Integer(30)};
        
        // Prompt the user to enter the number of years
        Object numOfYearsObject = JOptionPane.showInputDialog(null,
         "Select number of years:", "JOption Demo",
         JOptionPane.QUESTION_MESSAGE, null, yearList, null);
        int numOfYears = ((Integer)numOfYearsObject).intValue();
        
        // Prompt the user to enter loan amount
        String loanAmountString = JOptionPane.showInputDialog(null,
         "Enter loan amount, \nfor example, 150000 for $150000",
         "JOptionPane Demo", JOptionPane.QUESTION_MESSAGE);
        double loanAmount = Double.parseDouble(loanAmountString);
        
        // Obtain monthly payment and total payment
        Mortgage mortgage = new Mortgage(
         annualInterestRate, numOfYears, loanAmount);
        double monthlyPayment = mortgage.monthlyPayment();
        double totalPayment = mortgage.totalPayment();
        
        // Prepare output string
        String output = "Interest Rate: " + annualInterestRate + "%" +
         " Number of Years: " + numOfYears + " Loan Amount: $" + 
         loanAmount;
        output += "\nMonthly Payment: " + "$" +
         (int)(monthlyPayment * 100) / 100.0;
        output += "\nTotal Payment: $" +
         (int)(monthlyPayment * 12 * numOfYears * 100) / 100.0 + "\n";
         
        //Obtain monthly interest rate
        double monthlyInterestRate = annualInterestRate / 1200;
        
        double balance = loanAmount;
        double interest;
        double principal;
        
        // Display the header
        output += "\nPayment#\tInterest\tPrincipal\tBalance\n";
        
        for(i = 1; i <= numOfYears * 12; i++) {
            interest = (int)(monthlyInterestRate * balance * 100) / 100.0;
            principal = (int)((monthlyPayment - interest) * 100) / 100.0;
            balance = (int)((balance - principal) * 100) / 100.0;
            output += i + "\t" + interest + "\t" + principal + "\t" 
             + balance + "\n";
        }
        
        // Display monthly payment and total payment
        JOptionPane.showMessageDialog(null,
         new JScrollPane(new JTextArea(output)), 
         "JOption Demo", JOptionPane.INFORMATION_MESSAGE, null);    <---- masalah rasanya di sini...
        
        System.exit(0);
    }
}
[File2] Mortgage.java
 
--------------------------------------------------------------------
// Mortgage.java: Encapsulate mortgage information
public class Mortgage {
    private double annualInterestRate;
    private int numOfYears;
    private double loanAmount;
    
    /** Default constructor */
    public Mortgage() {
        this(7.5, 30, 100000);
    }
    
    /** Construct a mortgage with specified annual interest rate,
         number of years and loan amount.
    */
    public Mortgage(double annualInterestRate, int numOfYears,
     double loanAmount) {
         this.annualInterestRate = annualInterestRate;
         this.numOfYears = numOfYears;
         this.loanAmount = loanAmount;
     }
     
     /** Return annualInterestRate */
     public double getAnnualInterestRate() {
         return annualInterestRate;
     }
     
     /** Set a new annualInterestRate */
     public void setAnnualInterestRate(double annualInterestRate) {
         this.annualInterestRate = annualInterestRate;
     }
     
     /** Return numOfYears */
     public int getNumOfYears() {
         return numOfYears;
     }
     
     /** Set a new numOfYears */
     public void setNumOfYears(int numOfYears) {
         this.numOfYears = numOfYears;
     }
     
     /** Return loanAmount */
     public double getLoanAmount() {
         return loanAmount;
     }
     
     /** Set a new loanAmount */
     public void setLoanAmount() {
         this.loanAmount = loanAmount;
     }
     
     /** Find monthly payment */
     public double monthlyPayment() {
         double monthlyInterestRate = annualInterestRate / 1200;
         return loanAmount * monthlyInterestRate / (1 - 
          (Math.pow(1 / (1 + monthlyInterestRate), numOfYears * 12))); 
     }
     
     /** Find total payment */
     public double totalPayment() {
         return monthlyPayment() * numOfYears * 12;
     }
}

Edited by erokun

Share this post


Link to post
Share on other sites

1. tukar tajuk (masalah JOptionPane) :lol:

2. ada keluar error masa compile / run? paste kat sini..

3. http://java.sun.com/j2se/1.4.2/docs/api/ja...OptionPane.html

tengok showMessageDialog terima parameter apa..

1. ok sudah..

2. takde error... compile sume ok.. tp cuma hasil akhir tu tak seperti yg dikehendaki... patut kluar JTextArea dgn

scroll (JScrollPane) kalau lebih dr frame yg ditetapkan... yg kluar cuma JTextArea jek..

3. takde masalah dgn JOptionPane rasanya.. cuma pelik.. patut dah betul dah.. tp JScrollPane yg aku guna tu tak kluar2..

yg tu yg bertambah pelik tu... <_< penin2.. takut takle gune JScrollPane dlm JOptionPane.. isk.. tp baca kat buku

patut boleh.. bertambah pening la plak..

Share this post


Link to post
Share on other sites

sepatutnya jadi code ko tu..

aku test apa yang ko cakap tu berdasarkan contoh daniweb ni, dan jadi..

from javax.swing import *

outputArea = JTextArea()
scroller = JScrollPane(outputArea)
JOptionPane.showMessageDialog(None, scroller, "...", JOptionPane.INFORMATION_MESSAGE)

cuba ko try yang simple2 camni dulu, tanpa logic (jangan tiru code kat atas, aku pakai jython)

JOptionPane.showMessageDialog(null, new JScrollPane(new JTextArea(output)), "JOption Demo", JOptionPane.INFORMATION_MESSAGE, null);

kena declare satu2 dulu kot..

Share this post


Link to post
Share on other sites

owh.. aku dah buat dah... tapi still benda sama yg kluar...

cuba ko try code aku tu.. mana tau jython tu boleh kluar ke? pasal aku dah cuba yg simple2 pun result dia tetap sama..

scrollPane tu tak kluar.. tapi kalau aku gune JFrame biasa2 je.. kluar plak... tensen tol~~

Share this post


Link to post
Share on other sites

JTextArea t = new JTextArea("foo");
JScrollPane s = new JScrollPane(t);
JOptionPane.showMessageDialog(null, s, "...", JOptionPane.INFORMATION_MESSAGE);

try execute ni, pastu cuba taip2 banyak2 kat dlm text area tu..

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...