1

我是Java的初学者。我想创建一个扩展 JFrame 以使用的 Form 类。一切正常,它在屏幕上的大小和中心都很好。我只是无法向其中添加组件。我错过了什么。google了每一堂课,什么都没有。

主.java:

package pongLib;

import pongUI.*;

public class Main {

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

}

表单.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);
    }
}
4

6 回答 6

2

您没有使用 LayoutManager:

this.setLayout(null);

当不使用布局管理器时,请参阅这篇关于摆动中绝对定位的文章。

于 2010-01-15T12:54:24.647 回答
2

我认为问题是你的

setLayout(null);

没有布局管理器来安排组件。

于 2010-01-15T12:55:09.973 回答
1

您必须设置布局。

this.setLayout(new BorderLayout());

于 2010-01-15T12:52:31.007 回答
1

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

于 2010-01-15T12:53:01.023 回答
1

您的组件没有大小。给它们一个宽度和高度,或者最好还是使用布局管理器而不是空布局。

于 2010-01-15T12:55:13.827 回答
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);
    }
}
于 2010-01-15T13:18:52.547 回答