绝对定位一个JButton(Absolute Positioning a JButton)

2019-10-30 16:05发布

我试图用定位空布局一个JButton,但它不会显示出来,如果我切换到网格布局,他们只在中间一字排开不管我怎么改变。 如何设置我的按钮的位置和大小?

在框架

package Run;

import javax.swing.*;

import ThreeD.Display;
import ThreeD.Launcher;
import TowerDefence.Window;


import java.awt.*;
import java.awt.image.BufferedImage;

public class Frame extends JFrame{

    public static String title = "Game";        

    /*public static int GetScreenWorkingWidth() {
        return java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width;
    }*/

    /*public static int GetScreenWorkingHeight() {
        return java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height;
    }*/

    //public static Dimension size = new Dimension(GetScreenWorkingWidth(), GetScreenWorkingHeight());
    public static Dimension size = new Dimension(1280, 774);


    public static void main(String args[]) {
        Frame frame = new Frame();

        System.out.println("Width of the Frame Size is "+size.width+" pixels");
        System.out.println("Height of the Frame Size is "+size.height+" pixels");
    }

    public Frame() {
        setTitle(title);
        setSize(size);
        setResizable(false);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        ThreeDLauncher();
    }

    public void ThreeDLauncher() {
        //setLayout(new GridLayout(1, 1, 0, 0));
        setLayout(null);

        Launcher launcher = new Launcher();
        add(launcher);

        setVisible(true);       
    }

    public void TowerDefence() {
        setLayout(new GridLayout(1, 1, 0, 0));

        Window window = new Window(this);
        add(window);

        setVisible(true);
    }

    public void ThreeD() {
        BufferedImage cursor = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
        Cursor blank = Toolkit.getDefaultToolkit().createCustomCursor(cursor, new Point(0, 0), "blank");

        getContentPane().setCursor(blank);

        Display display = new Display();
        add(display);

        setVisible(true);

        display.start();
    }

}

在启动

package ThreeD;

import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.UIManager;

public class Launcher extends JPanel{   
    private JButton play, options, help, mainMenu;
    private Rectangle rplay, roptions, rhelp, rmainMenu;

    public Launcher() {
        drawButtons();
    }

    private void drawButtons() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch(Exception e) {
            e.printStackTrace();
        }

        play = new JButton("Play");
        options = new JButton("Options");
        help = new JButton("Help");
        mainMenu = new JButton("Main Menu");

        rplay = new Rectangle(20, 50, 80, 40);
        roptions = new Rectangle(20, 50, 80, 40);
        rhelp = new Rectangle(20, 50, 80, 40);
        rmainMenu = new Rectangle(20, 50, 80, 40);

        play.setBounds(rplay);
        options.setBounds(roptions);
        help.setBounds(rhelp);
        mainMenu.setBounds(rmainMenu);

        add(play);
        add(options);
        add(help);
        add(mainMenu);
    }
}

Answer 1:

子容器不继承其父母的布局管理,所以在启动的构造,我建议:

public Launcher() {
    this.setLayout(null);
    //this.setLayout(new GroupLayout(2, 2)); // I strongly recommend you try this though and get rid of the setBounds and rects
    drawButtons();
}

编辑:布局管理布置其组件,但没有什么是组件中。 这就是为什么它是不够的,只是设置帧的布局。



Answer 2:

我的猜测是你不加入之前设置大小或布局JPanel启动。 所以按钮显示正常,但JPanel中是没有的。 尝试把在:

launcher.setBounds( new Rectangle( 0, 0, 200, 200 ) );

或曾经大小,你所需要的。

看看是否能工程



文章来源: Absolute Positioning a JButton