As you know, when you make a JOP.SMD(null,"Message"); a small pop up window will come, sometimes you need MORE than that. You want to have a window with scroll bars to have enough space for all the information you'd like to enter.
Well, this is what you need to do:
JTextArea textBox = new JTextArea(20,20);
textBox.setText(someString);
JOP.SMG(null, new JScrollPane(textBox));
And well, that's it.
-----
Now, we are going to move on, and I'll post two of the assignments you can try, and if you have any questions you can post comments, and such.
In this exercise you are to create a new main class to do the following:
Declare a String variable and assign to it a String object constructed from your name.
Write a statement to display the variable in a JOptionPane window.
Declare an int variable and assign to it the value of the String variable's length method.
Write a statement to display the variable in a JOptionPane window.
Declare a String variable named out and assign to it the name variable followed by a space followed by the int variable (use the + operator).
Construct a BankAccount object using "123456789" as the account number and assign it to a Bank Account variable. The BankAccount class is available from Lab Exercise One.
Deposit 100 dollars into the account using the variable and the BankAccount class's deposit method.
Use the + operator to concatenate the BankAccount variable to the String variable out.
Construct a JTextArea with 20 rows and 20 columns and assign it to a JTextArea variable.
Use the setText method of JTextArea to output the String variable out on the JTextArea.
Construct a JScrollPane from the JTextArea and display it in a JOptionPane window.
Run your program.
SOLUTION FOR THE ASSIGNMENT -- DO IT, TRY IT, ASK QUESTIONS, IF YOU STILL CAN'T FIGURE IT OUT, LOOK BELOW.
import javax.swing.*;
/**
* Write a description of class Main here.
*
* @author Pablo
* @version (a version number or a date)
*/
public class Main
{
/**
* Constructor for objects of class Main
*/
public Main()
{
// initialise instance variables
}
public static void main(String[] args)
{
String myName = "Your Name";
JOptionPane.showMessageDialog(null,myName);
int nameLength = myName.length();
JOptionPane.showMessageDialog(null,"Name length is: " + nameLength);
BankAccount userAcc = new BankAccount ("123456789");
userAcc.deposit(100.00);
String accountBalance = userAcc.getAccountNumber() + " " + userAcc.getBalance();
JTextArea scrollArea = new JTextArea(20,20);
scrollArea.setText(accountBalance);
JOptionPane.showMessageDialog(null, new JScrollPane(scrollArea));
}
}
0 comments:
Post a Comment