Form.java extends JFrame

Ich bin ein starter in Java. Ich möchte ein Formular erstellen, die Klasse, die Sie erweitert JFrame zu verwenden. Alles funktioniert ok, es Größen und Zentren sowie auf dem Bildschirm. Ich kann einfach keine Komponenten hinzufügen, um es. Was vermisse ich. Gegoogelt jede Klasse, konnte nichts finden.

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);
    }
}
InformationsquelleAutor Illes Peter | 2010-01-15
Schreibe einen Kommentar