hey all, the noob is back. and with more questions that you guys will probably make fun of me for but...whatever.

I am creating a GUI design where there are four labels (it doesn't need functionality) with an OK, HELP and CANCEL button on the right side. i haven't started on the buttons yet because I've got a error already.

the error is "invalid method declaration" on the line 18 LabelFrame() part. but it's weird because this is a part where i'm still copying everything from the book (because the buttons are the real exercise). any suggestions. or do i need to finish the test program to get it to work?

Okay...Go ahead

import java.awt.FlowLayout; // specifies how components will be arranged
import javax.swing.JFrame; // provides basic window features
import javax.swing.JLabel; // displays text, images
import javax.swing.SwingConstants;


public class GUIEx extends JFrame
{
  private JLabel label1; // just text (snap to grid)
  private JLabel label2; // just text (show grid)
  private JLabel label3; // just text (X:)
  private JLabel label4; // just text (Y:)

  //Label  frames
  public LabelFrame()
  {
    super( "Align" );
    setLayout( new FlowLayout() ); // set frame layout

    // label1
    label1 = new JLabel( "Snap to Grid");
    label1.setToolTipText( "Snaps onto Grid Lines");
    add(label1); // adds the label to the JFrame
    label1.setVerticalTextPosition(SwingConstants.LEFT);
    label1.setHorizontalTextPosition(SwingConstants.TOP);

    // label2
    label1 = new JLabel( "Show Grid");
    label1.setToolTipText( "Shows the gridlines in your workspace");
    add(label2); // adds the label to the JFrame
    label1.setVerticalTextPosition(SwingConstants.LEFT);
    label1.setHorizontalTextPosition(SwingConstants.BOTTOM);

    // label3
    label1 = new JLabel( "X: ");
    label1.setToolTipText( "The X axis");
    add(label3); // adds the label to the JFrame
    label1.setVerticalTextPosition(SwingConstants.CENTER);
    label1.setHorizontalTextPosition(SwingConstants.TOP);

    // label4
    label1 = new JLabel( "Y: ");
    label1.setToolTipText( "The Y axis");
    add(label4); // adds the label to the JFrame
    label1.setVerticalTextPosition(SwingConstants.CENTER);
    label1.setHorizontalTextPosition(SwingConstants.BOTTOM);
  } // ends JLabel constructors
} // ends class

The class is called GUIEx and you have a method called LabelFrame() in which you call super().
Meaning that super(), should called only inside the constructor and LabelFrame is not a constructor since the class's name is GUIEx

okay, i've been following along a little on the chap 11 pages and it leads you to write a testing program. both will compile for me but then not run...not sure exactly where to go & i've gone thru the index a couple times (2 double check myself).

where am i screwing up?

import java.awt.FlowLayout; // specifies how components will be arranged
import javax.swing.JFrame; // provides basic window features
import javax.swing.JLabel; // displays text, images
import javax.swing.SwingConstants;


public class LabelFrame extends JFrame
{
  private JLabel label1; // just text (snap to grid)
  private JLabel label2; // just text (show grid)
  private JLabel label3; // just text (X:)
  private JLabel label4; // just text (Y:)


  //Label  frames
  public LabelFrame()
  {
    super( "Align" );
    setLayout( new FlowLayout() ); // set frame layout

    // label1
    label1 = new JLabel( "Snap to Grid");
    label1.setToolTipText( "Snaps onto Grid Lines");
    add(label1); // adds the label to the JFrame
    label1.setVerticalTextPosition(SwingConstants.LEFT);
    label1.setHorizontalTextPosition(SwingConstants.TOP);

    // label2
    label1 = new JLabel( "Show Grid");
    label1.setToolTipText( "Shows the gridlines in your workspace");
    add(label2); // adds the label to the JFrame
    label1.setVerticalTextPosition(SwingConstants.LEFT);
    label1.setHorizontalTextPosition(SwingConstants.BOTTOM);

    // label3
    label1 = new JLabel( "X: ");
    label1.setToolTipText( "The X axis");
    add(label3); // adds the label to the JFrame
    label1.setVerticalTextPosition(SwingConstants.CENTER);
    label1.setHorizontalTextPosition(SwingConstants.TOP);

    // label4
    label1 = new JLabel( "Y: ");
    label1.setToolTipText( "The Y axis");
    add(label4); // adds the label to the JFrame
    label1.setVerticalTextPosition(SwingConstants.CENTER);
    label1.setHorizontalTextPosition(SwingConstants.BOTTOM);
  } // ends JLabel constructors
} // ends class
import javax.swing.JOptionPane;

public class LTester
{
  public static void main( String args[])
  {
    LabelFrame labelFrame = new LabelFrame(); // create a frame for you
    labelFrame.setSize( 375, 180); // make the box the size you want
    labelFrame.setVisible(true); //display frame
   } // end main
} // end class LTester

I believe you have these SwingConstants backwards in the two lines below. I ran your code through NetBeans and it gave me exceptions on these lines:

label1.setVerticalTextPosition(SwingConstants.LEFT);
    label1.setHorizontalTextPosition(SwingConstants.TOP);

Swapping the positions of LEFT and TOP in the above lines got me farther in the code. TOP goes with Vertical and LEFT with HORIZONTAL, not the way you have it. Try swapping them for label1 and the other labels.

thanx. will do now

I believe you have these SwingConstants backwards in the two lines below. I ran your code through NetBeans and it gave me exceptions on these lines:

label1.setVerticalTextPosition(SwingConstants.LEFT);
    label1.setHorizontalTextPosition(SwingConstants.TOP);

Swapping the positions of LEFT and TOP in the above lines got me farther in the code. TOP goes with Vertical and LEFT with HORIZONTAL, not the way you have it. Try swapping them for label1 and the other labels.

This article has been dead for over six months. Start a new discussion instead.