12 Replies - 906 Views - Last Post: 27 October 2013 - 07:38 PM Rate Topic: -----

#1 hallmark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 24-October 13

creating an object for JFrame

Posted 27 October 2013 - 03:07 PM

after I create my frame, how do I create my object and add my object to my frame?
Is This A Good Question/Topic? 0
  • +

Replies To: creating an object for JFrame

#2 ChrisNt  Icon User is offline

  • cute bug

Reputation: 262
  • View blog
  • Posts: 896
  • Joined: 31-July 13

Re: creating an object for JFrame

Posted 27 October 2013 - 03:11 PM

Use method add of your JFrame.
Was This Post Helpful? 1
  • +
  • -

#3 hallmark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 24-October 13

Re: creating an object for JFrame

Posted 27 October 2013 - 03:17 PM

would it look something like this? I feel like I may be missing something

import javax.swing.JFrame;
public class Grades {
 public static void main(String[] args) {
	 JFrame frame = new JFrame ("");
	 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	 frame.getContentPane().add(new Student());
	 frame.pack();
	 frame.setVisible(true);

}
}


*Edited: removed a lot of completly useless not appropriate {}

This post has been edited by pbl: 27 October 2013 - 07:04 PM

Was This Post Helpful? 0
  • +
  • -

#4 ChrisNt  Icon User is offline

  • cute bug

Reputation: 262
  • View blog
  • Posts: 896
  • Joined: 31-July 13

Re: creating an object for JFrame

Posted 27 October 2013 - 03:24 PM

Yes.
Was This Post Helpful? 1
  • +
  • -

#5 hallmark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 24-October 13

Re: creating an object for JFrame

Posted 27 October 2013 - 04:31 PM

also, I am to create a JPanel with GridLayout and add the JPanel. could you steer me in the right direction with that please?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JPanel;
public class Student extends JPanel{
private String firstName, lastName;
private int assignment1, assignment2, assignment3, assignment4, assignment5;
private int exam1, exam2;
private String grade;

private JTextField firstName, lastName, assignment1, assignment2, assignment3, assignment4, assignment5, exam1, exam2;
private JLabel gradeLabel;
public Student(){
	firstName = "";
    lastName = "";
    assignment1 = 0;
    assignment2 = 0;
    assignment3 = 0;
    assignment4 = 0;
    assignment5 = 0;
    exam1 = 0;
    exam2 = 0;
    
}

JPanel mainPanel = new JPanel(new GridLayout(2, 2, 0, 0));
}

Was This Post Helpful? 0
  • +
  • -

#6 ChrisNt  Icon User is offline

  • cute bug

Reputation: 262
  • View blog
  • Posts: 896
  • Joined: 31-July 13

Re: creating an object for JFrame

Posted 27 October 2013 - 04:56 PM

You cant have fields with the same name.
Also a class Student that extends JPanel makes no sense,you should have seperate classes.One class Student which you will use to create Student objects by providing the first ,last name and score.An assignment class with needed information.
One class panel which will extend JPanel and will contain your code about graphical user interface.

Im not sure why you create this..

JPanel mainPanel = new JPanel(new GridLayout(2, 2, 0, 0));

This post has been edited by ChrisNt: 27 October 2013 - 05:06 PM

Was This Post Helpful? 0
  • +
  • -

#7 manofax  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 20
  • Joined: 17-May 13

Re: creating an object for JFrame

Posted 27 October 2013 - 05:01 PM

Java allows you to use a whole bunch of layouts for your JFrames/JPanels. These allow you to organise your objects effectively and position your objects in a manner that you deem fit for your application. As I'm sure you would have guessed, the GridLayout sets up the Panel that you are applying it to to add objects in a row and column format...You have set your GridLayout to have 2 rows and 2 columns, meaning that the first and second objects you add to it will go into the first row and into the 1st and second columns respectively. If you were to add 2 more objects they would then go into the second row and in the 1st and second columns depending on the order you added them.

To help you out with more information you will have to provide more detail as to the outline of your assignment. Do you have multiple panels? Are you using a JFrame? -> If so where is the code?
Was This Post Helpful? 0
  • +
  • -

#8 hallmark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 24-October 13

Re: creating an object for JFrame

Posted 27 October 2013 - 05:12 PM

the way that the instructions are worded, it sounds as if Student is my class panel. :

Template for Grade class

//import for JFrame

//Grades class header


	/**
	 * main method 
	 **/
		 
		//Create frame
	 
	 	//Create Student object
	 
	 	//Add Student object to the frame
	 
	 	//Pack and set frame visible

	//end of main method

//end of Grades class

Template for Student class

//import statements for GUI


//Class header for Student that extends JPanel

	//Create instance variables for first name and last name
	//Create instance variables for five assignment scores
	//Create instance variables for two exam scores
	//Create instance variable for letter grade
	
	//Create JTextField objects for first name and last name
	//Create JTextField objects for five assignment scores
	//Create JTextField objects for two exam scores
	
	//Create JLabel object for displaying resulting letter grade

	
	/**
	 * Constructor for Student class
	 **/

		//Create a JPanel with GridLayout
		//Add the JPanel
		
		//Create a JLabel for first name and add it to the JPanel

		//Instantiate JTextField for first name and add it to the JPanel

		//Create a JLabel for last name and add it to the JPanel
		//Instantiate JTextField for last name and add it to the JPanel

		//Create a JLabel for assignment 1 and add it to the JPanel
//Instantiate JTextField for assignment 1 and add it to the JPanel
		
		//Create a JLabel for assignment 2 and add it to the JPanel
//Instantiate JTextField for assignment 2 and add it to the JPanel
		
		//Create a JLabel for assignment 3 and add it to the JPanel
//Instantiate JTextField for assignment 3 and add it to the JPanel
		
		//Create a JLabel for assignment 4 and add it to the JPanel
//Instantiate JTextField for assignment 4 and add it to the JPanel

		//Create a JLabel for assignment 5 and add it to the JPanel
//Instantiate JTextField for assignment 5 and add it to the JPanel
		
		//Create a JLabel for exam 1 and add it to the JPanel
		//Instantiate JTextField for exam 1 and add it to the JPanel
		
		//Create a JLabel for exam 2 and add it to the JPanel
		//Instantiate JTextField for exam 2 and add it to the JPanel
		
		//Create a JButton and add it to the JPanel
		//Create a ButtonListener class instance and add it to JButton
		
//Instantiate JLabel for displaying resulting letter grade and add it to JPanel
		
	//end of Student constructor
	
	/**
	 * Method for calculating letter grade
	 */
		//add assignment and exam scores
		
//write if else statement using sum of assignment of exam scores based on letter grade assignment rules
	
	//end of calcGrade
	
	/**
	 * Method for getting letter grade
	 */

	/**
	 * Method for getting student name
	 */
	
	/**
	 * Method for displaying Student information
	 */
	
	   //*****************************************************************
	   //  Represents a listener for button action events.
	   //*****************************************************************

			/**
			 * Method for actionPerformed
			 */

//get values from JTextFields and assign them to appropriate instance variables
			
				//calculate letter grade by call appropriate method
			
//display student grade received in the appropriate JLabel

			//end of actionPerformed method

	//end of ButtonListener

//end of Student class



here my grades class:
import javax.swing.JFrame;
public class Grades {
 public static void main(String[] args) {
	 JFrame frame = new JFrame ("");
	 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	 frame.getContentPane().add(new Student());
	 frame.pack();
	 frame.setVisible(true);

}
}

Was This Post Helpful? 0
  • +
  • -

#9 ChrisNt  Icon User is offline

  • cute bug

Reputation: 262
  • View blog
  • Posts: 896
  • Joined: 31-July 13

Re: creating an object for JFrame

Posted 27 October 2013 - 05:21 PM

I hope you dont pay for these instructions...anyway

First thing you need to change is the name of your fields,you cant have fields with the same name as i said.Then you need to instantiate your fields and add them to your panel.
Was This Post Helpful? 0
  • +
  • -

#10 hallmark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 24-October 13

Re: creating an object for JFrame

Posted 27 October 2013 - 05:41 PM

are you saying it should look like this?
public class Student extends JPanel{
//private String firstName, lastName;
//private int assignment1, assignment2, assignment3, assignment4, assignment5;
//private int exam1, exam2;
//private String grade;

private JTextField firstName, lastName, assignment1, assignment2, assignment3, assignment4, assignment5, exam1, exam2;
private JLabel gradeLabel;
public Student(){
	firstName = "";
    lastName = "";
    assignment1 = 0;
    assignment2 = 0;
    assignment3 = 0;
    assignment4 = 0;
    assignment5 = 0;
    exam1 = 0;
    exam2 = 0;

Was This Post Helpful? 0
  • +
  • -

#11 hallmark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 24-October 13

Re: creating an object for JFrame

Posted 27 October 2013 - 06:32 PM

so I added some stuff but im not sure if im on the right track.

import java.awt.*;
import java.awt.event.*;
import java.awt.GridLayout;

import javax.swing.*;
public class Student extends JPanel{
/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
//private String firstName1, lastName1;
//private int assignment11, assignment21, assignment31, assignment41, assignment51;
//private int exam11, exam21;
//private String grade;

private JTextField firstName, lastName, assignment1, assignment2, assignment3, assignment4, assignment5, exam1, exam2;
private JLabel gradeLabel;
public Student(){
	firstName1 = "";
    lastName1 = "";
    assignment11 = 0;
    assignment21 = 0;
    assignment31 = 0;
    assignment41 = 0;
    assignment51 = 0;
    exam11 = 0;
    exam21 = 0;
    
}

JPanel mainPanel = new JPanel(new GridLayout(2, 2, 0, 0));{

JLabel firstName = new JLabel("First Name <ENTER>");
Component textTitle;
JPanel.add(textTitle, GridLayout.North);

firstname= new JTextField ();
JPanel.add(firstname, GridLayout.North);

JLabel lastName = new JLabel("Last Name <ENTER>");
JPanel.add(textTitle, GridLayout.);

lastname= new JTextField ();
JPanel.add(lastname, GridLayout.North);

JLabel assignment1 = new JLabel("Assignment1 <ENTER>");
JPanel.add(textTitle, GridLayout.);

assignment1= new JTextField ();
JPanel.add(assignment1, GridLayout.North);

JLabel assignment2 = new JLabel("Assignment2 <ENTER>");
JPanel.add(textTitle, GridLayout.);

assignment2= new JTextField ();
JPanel.add(assignment2, GridLayout.North);

JLabel assignment3 = new JLabel("Assignment3 <ENTER>");
JPanel.add(textTitle, GridLayout.);

assignment3= new JTextField ();
JPanel.add(assignment3, GridLayout.North);

JLabel assignment4 = new JLabel("Assignment4 <ENTER>");
JPanel.add(textTitle, GridLayout.);

assignment4= new JTextField ();
JPanel.add(assignment4, GridLayout.North);

JLabel assignment5 = new JLabel("Assignment5 <ENTER>");
JPanel.add(textTitle, GridLayout.);

assignment5= new JTextField ();
JPanel.add(assignment5, GridLayout.North);

JLabel exam1 = new JLabel("exam1 <ENTER>");
JPanel.add(textTitle, GridLayout.);

exam1= new JTextField ();
JPanel.add(exam1, GridLayout.North);

JLabel exam2 = new JLabel("exam2 <ENTER>");
JPanel.add(textTitle, GridLayout.);

exam2= new JTextField ();
JPanel.add(exam2, GridLayout.North);

JButton push = new JButton ("Grade");
push.addActionListener (new ButtonListener());

}

	

}

Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: creating an object for JFrame

Posted 27 October 2013 - 07:08 PM

JTextField are objects which have methods

So

JTextFiled x = new JTextField();
x.setText("bla");
x.setBackground(Color.WHITE):

but not

x = 123;
Was This Post Helpful? 0
  • +
  • -

#13 hallmark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 24-October 13

Re: creating an object for JFrame

Posted 27 October 2013 - 07:38 PM

thanks. I adjusted those but I also have an error with my JPanel.add lines that states:
"cannot make a static reference to the nonstatic method add (Component, Object) from the type Container
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1