tags:

views:

405

answers:

6

I'm a starter at Java. I'd like to create a Form class that extends JFrame to use. Everything works ok, it sizes and centers well on the screen. I just can't add components to it. What am I missing. Googled every class up, couldn't find anything.

Main.java:

package pongLib;

import pongUI.*;

public class Main {

    public static void main(String[] args) {
 Form frm = new Form(600,500,"Pong");
    }

}

Form.java:

package pongUI;

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

public class Form extends JFrame {
    private int width;
    private int height;
    private int posx;
    private int posy;
    private String title;

    public Form(int width, int height, String title) {
 //call parent constructor
 super();

 //set size
 this.width=width;
 this.height=height;

 //set title
 this.title=title;

 //get position(x,y)
 Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
 Double x = (screen.getWidth()-this.width)/2;
 Double y = (screen.getHeight()-this.height)/2;
 posx = x.intValue();
 posy = y.intValue();

 //add components
 JLabel points1Label = new JLabel("The Rookie (aka You)");
 this.addComponent(points1Label, 10, 50);

 //set form properties
 this.setLayout(null);
 this.setSize(width, height);
 this.setLocation(posx, posy);
 this.setResizable(false);
 this.setTitle(title);
 this.setVisible(true);

    public void addComponent(Component c, int posx, int posy) {
 c.setLocation(posx, posy);
 this.getContentPane().add(c,BorderLayout.CENTER);
    }
}
+1  A: 

You have to set a layout.

this.setLayout(new BorderLayout());

elr
tried this, still not working
Illes Peter
removing setLayout so it will default to BorderLayout works, but setLocation has no effect now.
Illes Peter
You have to set the layout before you are adding components.
elr
+1  A: 

You should set the layout to new BorderLayout() instead of null. Otherwise, your components won't get a size > 0 and won't therefore be visible.

Aaron Digulla
+2  A: 

You are not using a LayoutManager:

this.setLayout(null);

See this article about absolute positioning in swing, when not using a layout manager.

Oded
+2  A: 

I think The problem is your

setLayout(null);

there is no layout manager to arrange the components.

Pierre
+1  A: 

Your components have no size to them. Give them a width and height, or better still use a layout manager instead of a null layout.

brindy
adding points1Label.setBounds(10, 50, 150, 20) works! But I'd rather have them size to the size of the text it holds.
Illes Peter
Try this: points1Label.setBounds(10, 50, points1Label.getPreferredSize().width, points1Label.getPreferredSize().height);
nanda
+1  A: 

This is it:

package test;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class Form extends JFrame {
    private final int width;
    private final int height;
    private final int posx;
    private final int posy;
    private final String title;

    public Form(int width, int height, String title) {
        // call parent constructor
        super();

        // set size
        this.width = width;
        this.height = height;

        // set title
        this.title = title;

        // get position(x,y)
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        Double x = (screen.getWidth() - this.width) / 2;
        Double y = (screen.getHeight() - this.height) / 2;
        posx = x.intValue();
        posy = y.intValue();

        // add components
        JLabel points1Label = new JLabel("The Rookie (aka You)");
        points1Label.setBounds(10, 50, points1Label.getPreferredSize().width,
            points1Label.getPreferredSize().height);
        this.getContentPane().add(points1Label);

        // set form properties
        this.setLayout(null);
        this.setSize(width, height);
        this.setLocation(posx, posy);
        this.setResizable(false);
        this.setTitle(title);
        this.setVisible(true);
    }

    public void addComponent(Component c, int posx, int posy) {
        c.setLocation(posx, posy);
        this.getContentPane().add(c, BorderLayout.CENTER);
    }
}
nanda
YEPP that's IT. Thanx a lot
Illes Peter