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);
    }

}
  • 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)

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.