Q: Using JFrame

How to program using JFrame

Page 1 of 1

3 Replies - 2179 Views - Last Post: 25 February 2010 - 02:27 AM Rate Topic: -----

#1 suzie0013  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-February 10

Q: Using JFrame

Posted 24 February 2010 - 08:09 AM

Hi.. I'm a new programmer.
We have this project.. FLAMES.. I was able to run my program without using JFrame..(you can see the codes below).
Just that I don't know how to use JFrame... I need to run the program using the window with the buttons and textfields.. I know how to create the window with the buttons and everything..but I don't know how could I "bind" my program to the window that I've created...

public class Main {

/**
* @param args the command line arguments
*/

static Scanner console = new Scanner(System.in);
public static StringUtil obj = new StringUtil();
public static void main(String[] args) {
playGame();

}
public static void playGame()
{
String wantRead;
int want = 0;

wantRead = JOptionPane.showInputDialog("F L A M E S\nMenu\n1-Play\n0-Exit");
want = Integer.parseInt(wantRead);
if(want == 1)
{
gameProp();
}
else
{
JOptionPane.showMessageDialog(null, "Till next time! Thanks :D");
System.exit(0);
}


}

public static String trim(String sentence)
{
sentence = sentence.toLowerCase();
String newName = "";
char[] nameIs = sentence.toCharArray();
int i;

for(i = 0; i<sentence.length(); i++)
{
if(obj.isAlpha(nameIs[i]))
newName += nameIs[i];
}
return newName;
}

public static int compareNames(String name1, String name2)
{
char[] nameOne = name1.toCharArray();
char[] nameTwo = name2.toCharArray();
int i, j, counter = 0;
for(i = 0; i < name1.length(); i++)
{
for(j = 0; j <name2.length(); j++)
{
if(nameOne[i] == nameTwo[j])
{
counter++;


break;
}
}

}
String output = " "+counter;
JOptionPane.showMessageDialog(null, output);
return counter;
}

public static void match(int count, String name1, String name2)
{
String output = "";
if(count%6 != 0)
count = count%6;
else
count = 6;

if(count == 1)
output = name1 + " & " + name2 + "\n\nFRIENDS";
else if ( count == 2)
output = name1 + " & " + name2 + "\n\nLOVERS";
else if ( count == 3)
output = name1 + " & " + name2 + "\n\nANGRY";
else if ( count == 4)
output = name1 + " & " + name2 + "\n\nMARRIAGE";
else if ( count == 5)
output = name1 + " & " + name2 + "\n\nENEMIES";
else if ( count == 6)
output = name1 + " & " + name2 + "\n\nSWEETHEARTS";



JOptionPane.showMessageDialog(null, output);

}

public static void gameProp()
{
String first = JOptionPane.showInputDialog("Please enter full name of person #1: ");
String second = JOptionPane.showInputDialog("Please enter full name of person #2: ");
String new1st = trim(first);
String new2nd = trim(second);
String output = first+"\n"+second;
JOptionPane.showMessageDialog(null, output);

int ctrName = compareNames(new1st, new2nd);
match(ctrName, first, second);
}
}

public class StringUtil {

public boolean isAlpha(char x)
{
if((x=='a')||(x=='b')||(x=='c')||(x=='d')||(x=='e'))
return true;
else if((x=='f')||(x=='g')||(x=='h')||(x=='i')||(x=='j'))
return true;
else if((x=='k')||(x=='l')||(x=='m')||(x=='n')||(x=='o'))
return true;
else if((x=='p')||(x=='q')||(x=='r')||(x=='s')||(x=='t'))
return true;
else if((x=='u')||(x=='v')||(x=='w')||(x=='x')||(x=='y')||(x=='z'))
return true;
else
return false;

}

}


PS. I just don't have any idea..how these things could be done using the window :(

THANKS IN ADVANCE TO ALL WHO ARE WILLING TO HELP :D

Is This A Good Question/Topic? 0
  • +

Replies To: Q: Using JFrame

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Q: Using JFrame

Posted 24 February 2010 - 08:19 AM

Well, better start with reading some basic tutorials on how working with swing's components.
this is sun's tutorial specifically about JFrame and containers:
http://java.sun.com/...s/toplevel.html

and this is the full swing tutorial of sun:
http://java.sun.com/...torial/uiswing/
you can find all about JComponents as JButtons, JLabels etc.

once you get some ideas, and code, feel free to post again if you encounter any problem or errors.
Good Luck! :)
Was This Post Helpful? 1
  • +
  • -

#3 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: Q: Using JFrame

Posted 24 February 2010 - 08:23 AM

public boolean isAlpha(char x)
    {
        if((x=='a')||(x=='b')||(x=='c')||(x=='d')||(x=='e'))
        return true;
        else if((x=='f')||(x=='g')||(x=='h')||(x=='i')||(x=='j'))
        return true;
        else if((x=='k')||(x=='l')||(x=='m')||(x=='n')||(x=='o'))
        return true;
        else if((x=='p')||(x=='q')||(x=='r')||(x=='s')||(x=='t'))
        return true;
        else if((x=='u')||(x=='v')||(x=='w')||(x=='x')||(x=='y')||(x=='z'))
        return true;
        else
        return false;
        
    }

You can use the method Character.isLetter(char);
Was This Post Helpful? 1
  • +
  • -

#4 suzie0013  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-February 10

Re: Q: Using JFrame

Posted 25 February 2010 - 02:27 AM

View Postjapanir, on 24 February 2010 - 07:19 AM, said:

Well, better start with reading some basic tutorials on how working with swing's components.
this is sun's tutorial specifically about JFrame and containers:
http://java.sun.com/...s/toplevel.html

and this is the full swing tutorial of sun:
http://java.sun.com/...torial/uiswing/
you can find all about JComponents as JButtons, JLabels etc.

once you get some ideas, and code, feel free to post again if you encounter any problem or errors.
Good Luck! :)


View Postanonymouscodder, on 24 February 2010 - 07:23 AM, said:

You can use the method Character.isLetter(char);



wow..thanks to your help.. i was able to run my program using jframe... Attached Image
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1