Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Creating a class file

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Creating a class file

    Alright, I'll start off by saying hi to everyone in the community, and this is my first post on this message board so if I do not do a good job explaining my problem then you know why. Basically I have to have a program completed by Tuesday that consists of three ".java" files. They are "Lab6.java" "ChangeMaker.java" and "Flags.java" The "Lab6.java" uses the two class files "ChangeMaker.java" and "Flags.java" to make a program. Basically right now I am stuck on the "ChangeMaker.java" file because I tried creating methods within the file to make it a little more manageable in viewing the code. I am working within the Eclipse IDE on MacOSX 10.6.1
    Here is the code for the "ChangeMaker.java" file.
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
     
    public class ChangeMaker extends JPanel implements ActionListener 
    {
    	// ** Declare widgets and panels needed for the ChangeMaker interface
    	ImageIcon imgi1, imgi2, imgi3, imgi4, imgi5;
    	JButton jbDc, jbCf;
    	JLabel jLwelcome, jLcoi, jLap, jLDollars, jLQuarters, jLDimes, jLNickels, jLPennies, jLmakeChange;
    	JLabel jlimgi1, jlimgi2, jlimgi3, jlimgi4, jlimgi5;
    	JPanel jp1, jp2, jp3, jp4, jp6, jp7, jp8, jp9, jp10, jp11, jp12;
    	JTextField tf1, tf2, tf3, tf4, tf5, tf6, tf7;
     
    	//Class constructor
    	public ChangeMaker(Color c1, Color c2, Image DollarImage, Image QuarterImage, Image DimeImage, Image NickelImage, Image PennyImage)
    	public void init()
    	{
    		initVar();
    		initJP();
    		buildGUI();
    	}
    	public void initVar()
    	{
    		//initialize class variables: ImageIcons and JLabels
    		imgi1 = new ImageIcon(DollarImage);
    		jlimgi1 = new JLabel(imgi1);
    		imgi2 = new ImageIcon(QuarterImage);
    		jlimgi2 = new JLabel(imgi2);
    		imgi3 = new ImageIcon(DimeImage);
    		jlimgi3 = new JLabel(imgi3);
    		imgi4 = new ImageIcon(NickelImage);
    		jlimgi4 = new JLabel(imgi4);
    		imgi5 = new ImageIcon(PennyImage);
    		jlimgi5 = new JLabel(imgi5);
     
    		//initialize class variables: Labels
    		jLwelcome = new JLabel("Welcome to the ChangeMaker");
    		jLwelcome.setFont(new Font("Times", Font.BOLD+Font.ITALIC, 20));
    		jLwelcome.setForeground(Color.BLUE);
    		jLcoi = new JLabel("Cost of Item:");
    		jLap = new JLabel("Amount Paid");
    		jLDollars = new JLabel("(Dollars)");
    		jLQuarters = new JLabel("(Quarters)");
    		jLDimes = new JLabel("(Dimes)");
    		jLNickels = new JLabel("(Nickels)");
    		jLPennies = new JLabel("(Pennies)");
    		jLmakeChange = new JLabel("Make Change this way =======>");
     
    		//initialize class variables: Buttons
    		jbDc = new JButton("Determine Change");
    		jbCf = new JButton("Clear Fields");
     
    		//initialize class variables: JTextFields
    		tf1 = new JTextField(""); //width ie 10 depends layout manager
    		tf2 = new JTextField("");
    		tf3 = new JTextField("");
    		tf4 = new JTextField("");
    		tf5 = new JTextField("");
    		tf6 = new JTextField("");
    		tf7 = new JTextField("");
    	}	
    	public void initJP()
    	{
    		//initialize class variables: Panels created for NORTH
    		jp1 = new JPanel(new BorderLayout()); //main JPanel
    		jp1.setBackground(Color.PINK);
    		jp2 = new JPanel(new GridLayout(2,1));
    		jp2.setBackground(Color.PINK);
    		jp3 = new JPanel(new GridLayout(1,4));
    		jp3.setBackground(Color.PINK);
    		jp4 = new JPanel(new GridLayout(1,1));
    		jp4.setBackground(Color.PINK);
    		jp6 = new JPanel(new FlowLayout());
    		jp6.setBackground(Color.PINK);
    		jp7 = new JPanel(new FlowLayout());
    		jp7.setBackground(Color.PINK);
    		//initialize class variables: Panels created for CENTER
    		jp8 = new JPanel(new GridLayout(5,3));
    		jp8.setBackground(Color.PINK);
    		jp9 = new JPanel(new FlowLayout());
    		jp9.setBackground(Color.PINK);
    		jp10 = new JPanel(new GridLayout(1,2));
    		jp10.setBackground(Color.PINK);
    		jp11 = new JPanel(new FlowLayout());
    		jp11.setBackground(Color.PINK);
    		jp12 = new JPanel(new GridLayout(2,1));
    		jp12.setBackground(Color.PINK);
    	}	
    	public void buildGUI()
    	{	
    		//build GUI
    		add(jp1);
    			jp1.add(jp2,BorderLayout.NORTH);
    				jp2.add(jp6);
    					jp6.add(jLwelcome);
    				jp2.add(jp7); // jp7 is flow
    					jp7.add(jp3); //jp7 1x4 grid
    						jp3.add(jLcoi);
    						jp3.add(tf1);
    						jp3.add(jLap);
    						jp3.add(tf2);
    					jp7.add(jp4); //jp4 1x1 grid
    						jp4.add(jbDc);
    			jp1.add(jp9,BorderLayout.CENTER);
    				jp9.add(jp10); //jp10 2x1 grid
    					jp10.add(jp11);
    						jp11.add(jp12);
    							jp12.add(jLmakeChange);
    							jp12.add(jbCf);
    					jp10.add(jp8); //jp8 5x3 grid
    						jp8.add(jlimgi1);
    						jp8.add(jLDollars);
    						jp8.add(tf3);
    						jp8.add(jlimgi2); //second row
    						jp8.add(jLQuarters);
    						jp8.add(tf4);
    						jp8.add(jlimgi3); //third row
    						jp8.add(jLDimes);
    						jp8.add(tf5);
    						jp8.add(jlimgi4); // forth row
    						jp8.add(jLNickels);
    						jp8.add(tf6);
    						jp8.add(jlimgi5); // fifth row
    						jp8.add(jLPennies);
    						jp8.add(tf7);
    	}
    	public void actionPerformed(ActionEvent e)
    	{
    		// When a  button is pressed, determine which button and take the appropriate actions
     
    		validate();
    	} //end actionPerformed method
    } // end ChangeMaker class

    Basically I wanted to create methods "initVar()" "initJP()" and "buildGUI()" to help divide the code up and make it a little more manageable within eclipse, but now the Java applet won't even run because eclipse is saying the ImageIcons can not be resolved. Basically I want to know how I should structure this code a little better. Any help/suggestions would be awesome.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Creating a class file

    You don't need your own constructor for a JApplet. The reason it's not working is because you didn't pass it the references to the necessary files:
    public void initVar(Image DollarImage, Image QuarterImage, Image DimeImage, Image NickelImage, Image PennyImage)

    However, now you're going to run into some issues because you don't have references to these images from init(), which you'll have to hard-code in the ImageIcon references.

  3. #3
    Junior Member
    Join Date
    Nov 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating a class file

    Well, I kind of solved the problem with the images by declaring the images "static" in the "Lab6.java" file. This is what the code looks like now for the "ChangeMaker.java" file.

    public class ChangeMaker extends JPanel implements ActionListener 
    {
    	// ** Declare widgets and panels needed for the ChangeMaker interface
    	ImageIcon imgi1, imgi2, imgi3, imgi4, imgi5;
    	JButton jbDc, jbCf;
    	JLabel jLwelcome, jLcoi, jLap, jLDollars, jLQuarters, jLDimes, jLNickels, jLPennies, jLmakeChange;
    	JLabel jlimgi1, jlimgi2, jlimgi3, jlimgi4, jlimgi5;
    	JPanel jp1, jp2, jp3, jp4, jp6, jp7, jp8, jp9, jp10, jp11, jp12;
    	JTextField tf1, tf2, tf3, tf4, tf5, tf6, tf7;
     
    	//Class constructor
    	public ChangeMaker(Color c1, Color c2, Image DollarImage, Image QuarterImage, Image DimeImage, Image NickelImage, Image PennyImage)
    	{
    		initVar();
    		initJP();
    		buildGUI();
    	}
    	public void initVar()
    	{
    		//initialize class variables: ImageIcons and JLabels
    		imgi1 = new ImageIcon(Lab6.DollarImage);
    		jlimgi1 = new JLabel(imgi1);
    		imgi2 = new ImageIcon(Lab6.QuarterImage);
    		jlimgi2 = new JLabel(imgi2);
    		imgi3 = new ImageIcon(Lab6.DimeImage);
    		jlimgi3 = new JLabel(imgi3);
    		imgi4 = new ImageIcon(Lab6.NickelImage);
    		jlimgi4 = new JLabel(imgi4);
    		imgi5 = new ImageIcon(Lab6.PennyImage);
    		jlimgi5 = new JLabel(imgi5);
     
    		//initialize class variables: Labels
    		jLwelcome = new JLabel("Welcome to the ChangeMaker");
    		jLwelcome.setFont(new Font("Times", Font.BOLD+Font.ITALIC, 20));
    		jLwelcome.setForeground(Color.BLUE);
    		jLcoi = new JLabel("Cost of Item:");
    		jLap = new JLabel("Amount Paid");
    		jLDollars = new JLabel("(Dollars)");
    		jLQuarters = new JLabel("(Quarters)");
    		jLDimes = new JLabel("(Dimes)");
    		jLNickels = new JLabel("(Nickels)");
    		jLPennies = new JLabel("(Pennies)");
    		jLmakeChange = new JLabel("Make Change this way =======>");
     
    		//initialize class variables: Buttons
    		jbDc = new JButton("Determine Change");
    		jbCf = new JButton("Clear Fields");
     
    		//initialize class variables: JTextFields
    		tf1 = new JTextField(""); //width ie 10 depends layout manager
    		tf2 = new JTextField("");
    		tf3 = new JTextField("");
    		tf4 = new JTextField("");
    		tf5 = new JTextField("");
    		tf6 = new JTextField("");
    		tf7 = new JTextField("");
    	}	
    	public void initJP()
    	{
    		//initialize class variables: Panels created for NORTH
    		jp1 = new JPanel(new BorderLayout()); //main JPanel
    		jp1.setBackground(Color.PINK);
    		jp2 = new JPanel(new GridLayout(2,1));
    		jp2.setBackground(Color.PINK);
    		jp3 = new JPanel(new GridLayout(1,4));
    		jp3.setBackground(Color.PINK);
    		jp4 = new JPanel(new GridLayout(1,1));
    		jp4.setBackground(Color.PINK);
    		jp6 = new JPanel(new FlowLayout());
    		jp6.setBackground(Color.PINK);
    		jp7 = new JPanel(new FlowLayout());
    		jp7.setBackground(Color.PINK);
    		//initialize class variables: Panels created for CENTER
    		jp8 = new JPanel(new GridLayout(5,3));
    		jp8.setBackground(Color.PINK);
    		jp9 = new JPanel(new FlowLayout());
    		jp9.setBackground(Color.PINK);
    		jp10 = new JPanel(new GridLayout(1,2));
    		jp10.setBackground(Color.PINK);
    		jp11 = new JPanel(new FlowLayout());
    		jp11.setBackground(Color.PINK);
    		jp12 = new JPanel(new GridLayout(2,1));
    		jp12.setBackground(Color.PINK);
    	}	
    	public void buildGUI()
    	{	
    		//build GUI
    		add(jp1);
    			jp1.add(jp2,BorderLayout.NORTH);
    				jp2.add(jp6);
    					jp6.add(jLwelcome);
    				jp2.add(jp7); // jp7 is flow
    					jp7.add(jp3); //jp7 1x4 grid
    						jp3.add(jLcoi);
    						jp3.add(tf1);
    						jp3.add(jLap);
    						jp3.add(tf2);
    					jp7.add(jp4); //jp4 1x1 grid
    						jp4.add(jbDc);
    			jp1.add(jp9,BorderLayout.CENTER);
    				jp9.add(jp10); //jp10 2x1 grid
    					jp10.add(jp11);
    						jp11.add(jp12);
    							jp12.add(jLmakeChange);
    							jp12.add(jbCf);
    					jp10.add(jp8); //jp8 5x3 grid
    						jp8.add(jlimgi1);
    						jp8.add(jLDollars);
    						jp8.add(tf3);
    						jp8.add(jlimgi2); //second row
    						jp8.add(jLQuarters);
    						jp8.add(tf4);
    						jp8.add(jlimgi3); //third row
    						jp8.add(jLDimes);
    						jp8.add(tf5);
    						jp8.add(jlimgi4); // forth row
    						jp8.add(jLNickels);
    						jp8.add(tf6);
    						jp8.add(jlimgi5); // fifth row
    						jp8.add(jLPennies);
    						jp8.add(tf7);
    	}
    	public void actionPerformed(ActionEvent e)
    	{
    		// When a  button is pressed, determine which button and take the appropriate actions
    		validate();
    	}
    } // end ChangeMaker class

Similar Threads

  1. Creating variables from a text file
    By Larz0rz in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 4th, 2009, 11:57 PM
  2. creating a gui
    By rsala004 in forum AWT / Java Swing
    Replies: 2
    Last Post: July 21st, 2009, 02:17 AM
  3. Truncated class file error
    By Koâk in forum Exceptions
    Replies: 4
    Last Post: June 23rd, 2009, 11:23 AM
  4. Object creation from a input file and storing in an Array list
    By LabX in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 14th, 2009, 03:52 AM
  5. Reading a file line by line using the Scanner class
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM