1

私はJavaの初心者です。JFrame を拡張して使用する Form クラスを作成したいと思います。すべてが正常に機能し、画面のサイズと中央が適切に調整されます。コンポーネントを追加することはできません。何が欠けていますか。すべてのクラスをグーグルで検索しましたが、何も見つかりませんでした。

Main.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

問題はあなただと思います

setLayout(null);

コンポーネントを配置するためのレイアウト マネージャーはありません。

于 2010-01-15T12:55:09.973 に答える
2

LayoutManager を使用していません:

this.setLayout(null);

レイアウト マネージャーを使用しない場合の Swing での絶対配置については、この記事を参照してください。

于 2010-01-15T12:54:24.647 に答える
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 に答える
1

コンポーネントにはサイズがありません。幅と高さを指定するか、null レイアウトの代わりにレイアウト マネージャーを使用することをお勧めします。

于 2010-01-15T12:55:13.827 に答える
1

レイアウトを設定する必要があります。

this.setLayout(新しい BorderLayout());

于 2010-01-15T12:52:31.007 に答える
1

new BorderLayout()の代わりにレイアウトを設定する必要がありますnull。そうしないと、コンポーネントが 0 より大きいサイズにならないため、表示されません。

于 2010-01-15T12:53:01.023 に答える