2010-01-15 53 views
1

我是Java的入门者。我想创建一个扩展JFrame使用的Form类。一切正常,它的大小和屏幕上居中。我只是不能添加组件。我错过了什么。谷歌搜索每一个类,找不到任何东西。Form.java扩展JFrame

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

这是它:

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

YEPP即IT。非常感谢 – 2010-01-15 13:37:26

1

你必须设置一个布局。

this.setLayout(new BorderLayout());

+0

试过了,仍然没有工作 – 2010-01-15 12:58:30

+0

删除setLayout的所以它会默认为BorderLayout的工作,但现在setLocation没有效果。 – 2010-01-15 13:03:02

+0

您必须在添加组件之前设置布局。 – rakete 2010-01-15 13:39:05

1

您应该将布局设置为new BorderLayout()而不是null。否则,您的组件不会获得大于0的大小,因此不会显示。

2

您没有使用布局管理:

this.setLayout(null); 

this article在左右摇摆绝对定位,不使用布局管理器时。

2

我认为这个问题是您

setLayout(null); 

没有布局管理器来安排的组成部分。

1

您的组件对他们没有尺寸。给他们一个宽度和高度,或者更好的是使用布局管理器而不是空布局。

+0

加入points1Label.setBounds(10,50,150,20)的作品!但我宁愿让它们的大小与其保存的文本大小相同。 – 2010-01-15 12:59:59

+0

试试这个:points1Label.setBounds(10,50,points1Label.getPreferredSize()。width, points1Label.getPreferredSize()。height); – nanda 2010-01-15 13:20:43