java - JComponent 未设置位置或文本

标签 java jframe jbutton jcomponent

在下面的代码中,我只想将位置和文本分配给 JFrame 上的按钮组件,但它总是将它们分配为屏幕的高度并挤压到左侧,而不显示我分配的文本。需要任何帮助吗?

import javax.swing.*;
import java.awt.*;

class Main
{
    public static final String SIMULATOR_NAME = "Flocking Simulator 2K16"; //Simulator window name.
    private static final String SPARROW_GRAPHIC = "Bird.png"; //Graphic image for sparrows.
    public static final int MAXIMUM_WIDTH = 800; //Simulator window width.
    public static final int MAXIMUM_HEIGHT = 600; //Simulator window height.

    private static final int DEFAULT_BIRDS = 50; //Default number of birds on program launch.
    private static final int MINIMUM_BIRDS = 5; //Minimum quantity of birds.
    private static final int MAXIMUM_BIRDS = 100; //Maximum quantity of birds.
    private static final double DEFAULT_VELOCITY = 0.25; //Move at 0.25 pixels per 1ms.
    private static final int MOVEMENT_DELAY = 5; //Update movement every 5ms.
    private static final int MOVEMENTS_UNTIL_NEXT_UPDATE = 5; //Update directions every 25ms.

    private static JFrame frame; //Frame used from the JFrame class to draw graphics onto.

    private static Flock sparrows; //The flock of sparrows used for the simulator.

    private static JButton addBird; //The button object for adding a bird.
    private static JButton removeBird; //The button object for adding a bird.

    public static void main(String [ ] args)
    {
        Initialize();
        GameLoop();
    }

    private static void Initialize()
    {
        frame = new JFrame(SIMULATOR_NAME + " - Number Of Birds: " + DEFAULT_BIRDS);
        frame.setSize(MAXIMUM_WIDTH, MAXIMUM_HEIGHT);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new GridLayout());
        LoadObjectGraphics();
        frame.setVisible(true);
    }

    private static void LoadObjectGraphics()
    {
        sparrows = new Flock(SPARROW_GRAPHIC, DEFAULT_BIRDS, new CartesianCoordinate(MAXIMUM_WIDTH, MAXIMUM_HEIGHT), DEFAULT_VELOCITY);

        for (int i = 0; i < DEFAULT_BIRDS; i++)
        {
            frame.add(sparrows.GetBird(i).BirdGraphic());
        }

        LoadUI();
    }

    private static void LoadUI()
    {
        addBird = new JButton("+");
        addBird.setBounds(50, 50, 50, 50);
        removeBird = new JButton("-");
        removeBird.setSize(48, 48);
        removeBird.setBounds(50, 50, 50, 50);
        frame.add(addBird);
        frame.add(removeBird);
    }

    private static void UpdateUI()
    {
        frame.setTitle(SIMULATOR_NAME + " - Number Of Birds: " + sparrows.GetMaximumBirds());
    }

    private static void GameLoop()
    {
        while (true)
        {
            //Change the directions of the birds
            sparrows.UpdateDirections();

            //Move the birds before updating directions of the birds again
            for (int i = 0; i < MOVEMENTS_UNTIL_NEXT_UPDATE; i++)
            {
                Utils.pause(MOVEMENT_DELAY); //Pause allowing program to not crash in an infinate loop
                sparrows.ProcessMovement(MOVEMENT_DELAY); //Move the birds each interval
                UpdateUI(); //Update the simulators UI
            }
        }
    }
}

As you can see, the dimensions and location of the buttons are incorrect

最佳答案

使用 GridLayout(最简单的方法)或 SpringLayout。

一个例子:

GridLayout layout = new GridLayout(/*Number of rows*/ , /*Number of columns*/);

在框架中添加此布局:

myFrame.setlayout(layout);

并在 GridLayout 中添加组件:

layout.add(myComponent);
layout.add(mySecondComponent);

等等...

这里有一份完整的 GridLayout 指南供您使用:

How to use GridLayout

以及 SpringLayout 的指南(SpringLayout 很难使用,但是尝试一下):

How to use SpringLayout

SpringLayout比GridLayout更好!

关于java - JComponent 未设置位置或文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36902896/

相关文章:

java - JButton 数组 ActionListener

java - 无法读取正则表达式

java - 尝试将多个面板对象添加到创建基本 JFrame 的不同类

java - 如何在 Java 中使用 Open Type 字体?

java - 我的巴恩斯利蕨类植物太瘦了

java - 手动构造的 BufferedImage 不会绘制到 JFrame 上

java - 为什么我的带图像的 jbutton 不显示图形 java

java - 单击按钮将 ImageIcon 更改为另一张图片

java - GZipInputStream .read() 将零插入缓冲区

java - 当我们使用 git merge 或分支来掌握时,重构是否会发生在所有类上