We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,390 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Trying to create FlowLayout, unknown error.

The GUIFlowLayout is giving me some trouble, the GUIFrame works fine.
I have copied this from the Java programming book which I am using (Introduction to JAVA, eight edition, by Y. Daniel Liang).

I get red error lines under 'setLayout', all of the 'add' words, and all the 'setTitle, setSize' and so on.

I don't know how to solve this, as all of the previous examples in the book have been flawless so far.

All of the errors say: "The method (method) is undefined for the type GUIFlowLayout.

import javax.swing.*;
import java.awt.FlowLayout;

public class GUIFlowLayout extends GUIFrame {
    public GUIFlowLayout() {
        setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20));

        add(new JLabel("First Name"));
        add(new JTextField(8));
        add(new JLabel("MI"));
        add(new JTextField(1));
        add(new JLabel("Last Name"));
        add(new JTextField(8));
    }

    public static void main(String[] args) {
        GUIFlowLayout frame = new GUIFlowLayout();

        frame.setTitle("ShowFlowLayout");
        frame.setSize(200, 200);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

The extended class GUIFrame:

import javax.swing. *;

public class GUIFrame {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Frame");
        frame.setSize(400, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

}
2
Contributors
2
Replies
1 Hour
Discussion Span
7 Months Ago
Last Updated
3
Views
Question
Answered
abders
Newbie Poster
19 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
  • why bothering with compilier, better could be to post to forum, isn't it .......

  • there isnt created any of two JFrames,

  • there are two main methods remove one of them

  • don't extend JFrame, create that as local variable, otherwise part of important methods isn't accesible directly, more in describtions composition versus inheritance

  • dirty hack to change

from

public class GUIFlowLayout extends GUIFrame {

to

public class GUIFlowLayout extends JFrame {

then GUI could be displayed correctly

  • have look at Initial Thread

  • change FlowLayout to SpringLayout

  • replace frame.setSize(200, 200); with frame.pack(); and move that before JFrame#setVisible(true)

mKorbel
Nearly a Posting Virtuoso
1,215 posts since Feb 2011
Reputation Points: 482
Solved Threads: 239
Skill Endorsements: 12
Question Answered as of 7 Months Ago by mKorbel

Thanks, changing the GUIFrame to JFrame helped, though I do not know why..

It looks better the fame.pack(); also, thanks for that tip.

I don't know what the Springlayout would do, since I couldn't implement it correctly, but maybe I'll find about that in the future. For now I'm going to try GridLayout and then BorderLayout.

Thanks again for the help!

abders
Newbie Poster
19 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0637 seconds using 2.72MB