8 Replies - 1853 Views - Last Post: 09 March 2012 - 02:52 PM Rate Topic: -----

#1 Logik22   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 171
  • Joined: 12-December 11

GUI does not display

Posted 09 March 2012 - 02:23 PM

I cannot get my GUI to display. I feel like I'm missing one line of code but I've looked over several tutorials and I cannot see what is different.

//Import libraries
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MPG extends JFrame {
	
		
	public void GUI() {
		
		//Create Frame
		JFrame frame = new JFrame ("MPG");
		
		//Container
		Container pane = this.getContentPane();

		//Create new grid layout
		GridLayout gridLayout = new GridLayout(0,2);
		setLayout(gridLayout);
		//set size
		this.setSize(500,500);
		
		
		
		//Create and Add labels		
		this.add(new JLabel("Octane: "));
		this.add(new JLabel("Miles: "));
		this.add(new JLabel("Gallons filled: "));
		this.add(new JLabel("Miles per Gallon: "));
		//Add drop down box
		String[] octaneString = {"<87", "87", "88", "89", "90", "91", "92", "93", "94+", "Other"};
		JComboBox octaneList = new JComboBox(octaneString);
		//Sets default to 92 Octane
		octaneList.setSelectedIndex(6);
		
		//octaneList.addActionListener(this);
		
	}
	



public static void main(String[] args) {
	
	MPG app = new MPG();
	app.setVisible(true);
}
}



Is This A Good Question/Topic? 0
  • +

Replies To: GUI does not display

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12317
  • View blog
  • Posts: 45,416
  • Joined: 27-December 08

Re: GUI does not display

Posted 09 March 2012 - 02:26 PM

It doesn't make sense that you extend JFrame and create a JFrame in your constructor. When you extend JFrame, MPG is a JFrame. So rather than creating a new JFrame, treat this as a JFrame.
Was This Post Helpful? 0
  • +
  • -

#3 Logik22   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 171
  • Joined: 12-December 11

Re: GUI does not display

Posted 09 March 2012 - 02:31 PM

View Postmacosxnerd101, on 09 March 2012 - 02:26 PM, said:

It doesn't make sense that you extend JFrame and create a JFrame in your constructor. When you extend JFrame, MPG is a JFrame. So rather than creating a new JFrame, treat this as a JFrame.



Ahhh I see. That makes sense.

I still can't get it to appear.

//Import libraries
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MPG extends JFrame {
		
	public void GUI() {
	
		//set title
		this.setTitle("Fuel Efficiency Tracker");
		
		//Container
		Container pane = this.getContentPane();

		//Create new grid layout
		GridLayout gridLayout = new GridLayout(0,2);
		this.setLayout(gridLayout);
		//set size
		this.setSize(500,500);
		
		//Create and Add labels		
		this.add(new JLabel("Octane: "));
		this.add(new JLabel("Miles: "));
		this.add(new JLabel("Gallons filled: "));
		this.add(new JLabel("Miles per Gallon: "));
		//Add drop down box
		String[] octaneString = {"<87", "87", "88", "89", "90", "91", "92", "93", "94+", "Other"};
		JComboBox octaneList = new JComboBox(octaneString);
		//Sets default to 92 Octane
		octaneList.setSelectedIndex(6);
		
		//octaneList.addActionListener(this);
		
	}

public static void main(String[] args) {
	
	MPG app = new MPG();
	app.setVisible(true);
}
}


Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: GUI does not display

Posted 09 March 2012 - 02:36 PM

If you intended line 8 to be a constructor (and I recommend that's what you intended), it should be:

public MPG()

You'll have better luck with that.
Was This Post Helpful? 0
  • +
  • -

#5 Logik22   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 171
  • Joined: 12-December 11

Re: GUI does not display

Posted 09 March 2012 - 02:42 PM

View PostGregBrannon, on 09 March 2012 - 02:36 PM, said:

If you intended line 8 to be a constructor (and I recommend that's what you intended), it should be:

public MPG()

You'll have better luck with that.



I have changed it. I was following this tutorial:
http://docs.oracle.c...LayoutDemo.java

I needed to modify that a bit and I thought it should be "public void GUI()" because they put "public void addComponentsToPane(final Container pane)" and it appeared that "addComponentsToPane()" and "GUI()" were trying to accomplish the same thing.


//Import libraries
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MPG extends JFrame {
		
	public GUI() {
	
		//set title
		this.setTitle("Fuel Efficiency Tracker");
		
		//Container
		Container pane = this.getContentPane();

		//Create new grid layout
		GridLayout gridLayout = new GridLayout(0,2);
		this.setLayout(gridLayout);
		//set size
		this.setSize(500,500);
		
		//Create and Add labels		
		this.add(new JLabel("Octane: "));
		this.add(new JLabel("Miles: "));
		this.add(new JLabel("Gallons filled: "));
		this.add(new JLabel("Miles per Gallon: "));
		//Add drop down box
		String[] octaneString = {"<87", "87", "88", "89", "90", "91", "92", "93", "94+", "Other"};
		JComboBox octaneList = new JComboBox(octaneString);
		//Sets default to 92 Octane
		octaneList.setSelectedIndex(6);
		
		//octaneList.addActionListener(this);
		
	}

public static void main(String[] args) {
	
	MPG app = new MPG();
	app.setVisible(true);
}
}



View PostLogik22, on 09 March 2012 - 02:41 PM, said:

View PostGregBrannon, on 09 March 2012 - 02:36 PM, said:

If you intended line 8 to be a constructor (and I recommend that's what you intended), it should be:

public MPG()

You'll have better luck with that.



I have changed it. I was following this tutorial:
http://docs.oracle.c...LayoutDemo.java

I needed to modify that a bit and I thought it should be "public void GUI()" because they put "public void addComponentsToPane(final Container pane)" and it appeared that "addComponentsToPane()" and "GUI()" were trying to accomplish the same thing.


//Import libraries
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MPG extends JFrame {
		
	public GUI() {
	
		//set title
		this.setTitle("Fuel Efficiency Tracker");
		
		//Container
		Container pane = this.getContentPane();

		//Create new grid layout
		GridLayout gridLayout = new GridLayout(0,2);
		this.setLayout(gridLayout);
		//set size
		this.setSize(500,500);
		
		//Create and Add labels		
		this.add(new JLabel("Octane: "));
		this.add(new JLabel("Miles: "));
		this.add(new JLabel("Gallons filled: "));
		this.add(new JLabel("Miles per Gallon: "));
		//Add drop down box
		String[] octaneString = {"<87", "87", "88", "89", "90", "91", "92", "93", "94+", "Other"};
		JComboBox octaneList = new JComboBox(octaneString);
		//Sets default to 92 Octane
		octaneList.setSelectedIndex(6);
		
		//octaneList.addActionListener(this);
		
	}

public static void main(String[] args) {
	
	MPG app = new MPG();
	app.setVisible(true);
}
}




Sorry, I can't find the edit button :/

I meant to add that I receive a "Return type for the method is missing" error and the GUI still does not display.
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: GUI does not display

Posted 09 March 2012 - 02:48 PM

I know you're just starting, so it's not immediately obvious to you, but you have to ask yourself (and ultimately understand), "What's happening when the following code executes?"

MPG app = new MPG();
app.setVisible(true);

If you don't have a constructor called MPG(), what is "new MPG()" going to do? Any idea? Well, you know one thing: The results are not what you'd like. Why?

If you change the code as I suggested, it will display as you've programmed. Again, why?
Was This Post Helpful? 0
  • +
  • -

#7 Logik22   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 171
  • Joined: 12-December 11

Re: GUI does not display

Posted 09 March 2012 - 02:48 PM

View PostGregBrannon, on 09 March 2012 - 02:36 PM, said:

If you intended line 8 to be a constructor (and I recommend that's what you intended), it should be:

public MPG()

You'll have better luck with that.



Sorry, I misread your suggestion. I still had it as "public GUI()".
Was This Post Helpful? 0
  • +
  • -

#8 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: GUI does not display

Posted 09 March 2012 - 02:50 PM

Ahhh. Hopefully now it's becoming clearer.
Was This Post Helpful? 0
  • +
  • -

#9 Logik22   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 171
  • Joined: 12-December 11

Re: GUI does not display

Posted 09 March 2012 - 02:52 PM

View PostGregBrannon, on 09 March 2012 - 02:50 PM, said:

Ahhh. Hopefully now it's becoming clearer.



Much clearer. Thank you.

My logic was that if I did

MPG app = new MPG();
	app.setVisible(true);


It would call the Class called MPG which in turn would call GUI(). I'm sure I will run into a few more questions but I feel much better about it now that I can actually see a result. Thanks again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1