2010-01-15 58 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