4 Replies - 1298 Views - Last Post: 25 October 2010 - 09:50 PM Rate Topic: -----

#1 xekret  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-October 10

Incompatible types and cannot find class

Posted 25 October 2010 - 08:30 PM

I have this assignment that is the intro to the GUI of Java but the instructor wants us to use different classes so that the main method is written in a class of its own so that it will show all the windows together. I am getting an error that it cannot find the first class and the other two says that they are incompatible types. here is my code

Main method
import java.awt.*;
import javax.swing.*;
import javax.swing.JFrame;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.BorderLayout;

public class Program9  {

    public static void main(String[] args) {
		FlowFLow frame = new FlowLayout();
		frame.setTitle("Program9");
		frame.pack();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
    }

    {
    	BorderFlow frame = new BorderLayout();
		frame.pack();
		frame.setTitle("Program9");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
    }

    {
    	BorderGrid frame = new GridLayout();
		frame.setSize(300,200);
		frame.setTitle("Program9");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		frame.setBackground(Color.blue);
    }
}


Flow class
 
import java.awt.*;
import javax.swing.*;
import javax.swing.JFrame;
import java.awt.FlowLayout;

public class FlowFlow extends JFrame {
	private JButton jbt1, jbt2, jbt3, jbt4, jbt5, jbt6;
	{
		//Panel P1 with 3 Buttons
		JPanel p1 = new JPanel();
		p1.setLayout(new FlowLayout());
		p1.setBackground(Color.blue);
		p1.add(jbt1 = new JButton("Button 1"));
	    p1.add(jbt2 = new JButton("Button 2"));
		p1.add(jbt3 = new JButton("Button 3"));

		//Panel P2 with 3 Buttons
		JPanel p2 = new JPanel();
			p1.setLayout(new FlowLayout());
		p2.add(jbt4 = new JButton("Button 4"));
		p2.add(jbt5 = new JButton("Button 5"));
		p2.add(jbt6 = new JButton("Button 6"));

		//adding P1 and P2 to frame
		setLayout(new FlowLayout(FlowLayout.LEFT, 10,20));
		add(p1);
		add(p2);



	}
}



Border class
import java.awt.*;
import javax.swing.*;
import javax.swing.JFrame;
import java.awt.BorderLayout;

public class BorderFlow extends JFrame {
	private JButton jbt1, jbt2, jbt3, jbt4, jbt5, jbt6;
	{
		//Panel P1 with 3 Buttons
		JPanel p1 = new JPanel();
		p1.setLayout(new FlowLayout());
		p1.add(jbt1 = new JButton("Button 1"));
	    p1.add(jbt2 = new JButton("Button 2"));
		p1.add(jbt3 = new JButton("Button 3"));
		p1.setBackground(Color.black);
		//Panel P2 with 3 Buttons
		JPanel p2 = new JPanel();
		p2.setLayout(new FlowLayout());
		p2.add(jbt4 = new JButton("Button 4"));
		p2.add(jbt5 = new JButton("Button 5"));
		p2.add(jbt6 = new JButton("Button 6"));

		//adding P1 and P2 to frame
		setLayout(new BorderLayout());
		add(p1, BorderLayout.SOUTH);
		add(p2, BorderLayout.CENTER);



	}
}



Grid class
 
import java.awt.*;
import javax.swing.*;
import javax.swing.JFrame;
import java.awt.GridLayout;

public class BorderGrid extends JFrame {
	private JButton jbt1, jbt2, jbt3, jbt4, jbt5, jbt6;
	{
		setLayout(new GridLayout(2, 2, 5, 5));

		JPanel p1 = new JPanel();
		p1.setLayout(new GridLayout(2, 2, 5, 5));
		p1.add(jbt1 = new JButton("Button 1"));
	    p1.add(jbt2 = new JButton("Button 2"));
		p1.add(jbt3 = new JButton("Button 3"));
		p1.setBackground(Color.black);

		JPanel p2 = new JPanel();
		p2.setLayout(new GridLayout(2, 2, 5, 5));
		p2.setBackground(new Color(255,100,0));
		p2.add(jbt4 = new JButton("Button 4"));
		p2.add(jbt5 = new JButton("Button 5"));
		p2.add(jbt6 = new JButton("Button 6"));

		setLayout(new BorderLayout());
		add(p1, BorderLayout.SOUTH);
		add(p2, BorderLayout.CENTER);
	}
}



Any help is appreciated since I am starting to learn a little more about programming but I am not an expert.

Is This A Good Question/Topic? 0
  • +

Replies To: Incompatible types and cannot find class

#2 TokoYami200  Icon User is offline

  • New D.I.C Head

Reputation: 13
  • View blog
  • Posts: 44
  • Joined: 18-October 10

Re: Incompatible types and cannot find class

Posted 25 October 2010 - 08:33 PM

I think you have to declare an object of Program9 since main is static there is no instance of Program9 so it doesn't know about the other functions etc.

Edit: Nevermind, that's not the problem. :o

This post has been edited by TokoYami200: 25 October 2010 - 08:35 PM

Was This Post Helpful? 0
  • +
  • -

#3 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Incompatible types and cannot find class

Posted 25 October 2010 - 09:25 PM

There is something wrong here:
FlowFLow frame = new FlowLayout();


FlowFlow is your class which extends JFrame and it does not have anything to do with the FlowLayout class which is implements the LayoutManager and serializable interfaces. So you cannot asign an object from the FlowLayout class to a variable of the type FlowFlow class. Maybe what you wish to do is:
FlowFlow frame = new FlowFlow();
frame.setLayout(new FlowLayout());


That make sense. Do the same for the others.

This post has been edited by guidojavier: 25 October 2010 - 09:26 PM

Was This Post Helpful? 1
  • +
  • -

#4 xekret  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 07-October 10

Re: Incompatible types and cannot find class

Posted 25 October 2010 - 09:41 PM

That fixed it, thank you very much but now I have another problem. The only window that appears when the program compiles is the first one which is Flow. It is not showing me the other two.
Was This Post Helpful? 0
  • +
  • -

#5 guido-granobles  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Incompatible types and cannot find class

Posted 25 October 2010 - 09:50 PM

This blocks should be inside of the main method:
{
    	BorderFlow frame = new BorderFlow();
    	frame.setLayout(new BorderLayout());
		frame.pack();
		frame.setTitle("Program9");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
    }


rename the frame variable for each window you want show. For example:
BorderFlow frame2 = new BorderFlow();
    	frame2.setLayout(new BorderLayout());
		frame2.pack();
		frame2.setTitle("Program9");
		frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame2.setLocationRelativeTo(null);
		frame2.setVisible(true);


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1