-
converting to JOptionPane
hi, all, ok, i admit, that this is my assignment, but i really can't crack the answer. it requires me to convert the scripts below to a JOptionPane... how can it be possible, thank you in advance for those who are willing to help:
Code:
//import javax.swing.JOptionPane;
import java.io.*;
public class Q7
{
public static void main (String[]args) throws IOException
{
String que,temp;
int triangle;
BufferedReader DataMasuk;
DataMasuk = new BufferedReader (new InputStreamReader (System.in));
System.out.println ("Key in an integer triangle:");
//System.out.print("Key in an integer triangle: ");
triangle = Integer.parseInt(DataMasuk.readLine());
{
for(int x=0; x<triangle; x++)
{
for(int y=0; y<=x; y++)
{
System.out.print("*");
//temp = "*";
}
System.out.println(" ");
}
}
}
}
-
-
Read code carefully and understand it before you finish, mkay ?
Code:
import javax.swing.*;
public class Q7 {
public Q7() {} // empty default constructor
public void doTriangle() {
try {
int triangle = getUsersIntegerEntry();
printTriangle(triangle);
}
catch (Exception ex) {
// user cancelled, do nothing
}
}
/**
* Show input dialog, loops with error dialog until the user
* gets it right. If user cancels the parse will throw a
* nullPointerException that is caught by the exception handler
* in the doTriangle method
*/
private int getUsersIntegerEntry() throws Exception {
while (true) {
String s = JOptionPane.showInputDialog(null,
"Key in an integer triangle",
new Integer(0)); // could also be null
try {
return Integer.parseInt(s);
}
catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null,
s+" is not an integer",
"Input error",
JOptionPane.ERROR_MESSAGE);
}
}
}
/**
* Do triangle output
*/
private void printTriangle(int triangle) {
for (int x = 0; x < triangle; x++) {
for (int y = 0; y <= x; y++) {
System.out.print("*");
//temp = "*";
}
System.out.println(" ");
}
}
/**
* Main, just creates a Q7 instance and invokes Q7's
* only public method.
*/
public static void main(String[] args) {
Q7 trallala=new Q7();
trallala.doTriangle();
}
}
Last edited by sjalle; 07-26-2005 at 05:04 AM.
eschew obfuscation
-
thank you very much for the help, but i need the output in the JOptionPane format too... that's what bothers the most, i don't know how to do it...
-
I don't think you can get that output in the 'JOptionPane format', infact I'm not sure
what you mean by that. Do you mean draw the triangle in a panel in a frame ?
eschew obfuscation
-
 Originally Posted by sjalle
I don't think you can get that output in the 'JOptionPane format', infact I'm not sure
what you mean by that. Do you mean draw the triangle in a panel in a frame ?
sorry for the cofusion, yes, i need it in a panel
-
Yes it is possible to display the output using a JOptionPane, see this code (converted from old):
Code:
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Triangle {
public static void main(String[] args) {
int triangle;
triangle = Integer.parseInt(JOptionPane.showInputDialog(null,
"Ke in an integer triangle",null));
{
String display="";
for(int x = 0; x < triangle; x++) {
for(int y = 0; y <= x; y++) {
display+="*";
}
display+=" \n";
}
JOptionPane.showMessageDialog(new JFrame(),display);
}
}
}
I think this is what you want to do.
-
thank zodoz n sjalle, both of you have been much help for me. the program works fine. thank you again
-
Ok Zodoz, I wasn't aware of that. However, giving an assignment that requires the
students to do text "graphics" in a GUI isn't what I would call relevant education, but
then I have seen many examples of strange teachings presented by students in this
forum.
eschew obfuscation
Similar Threads
-
By Frank Oquendo in forum .NET
Replies: 116
Last Post: 06-14-2002, 06:04 AM
-
By Marie in forum Database
Replies: 0
Last Post: 11-20-2000, 06:37 AM
-
By Marie in forum VB Classic
Replies: 0
Last Post: 11-20-2000, 06:32 AM
-
Replies: 1
Last Post: 10-11-2000, 12:23 PM
-
By Courtney Brown in forum XML
Replies: 1
Last Post: 10-06-2000, 11:25 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|