我的文字游戏的 GUI 布局

GUI Layout of My Text Game

您好,我正在尝试使用带边框布局的基于文本的游戏的 GUI,我几乎已经实现了我想要通过 GUI 实现的目标。

这就是我想要的。我几乎什么都下来了……除了纽扣。我希望它看起来像我的素描中那样。

我可以就如何实现这一目标获得任何帮助吗?

这是我设置 GUI 的 class:

package Story1;

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

public class textGUI extends JFrame{
    private JButton button;
    private JTextArea plot;
    private JLabel label;

public textGUI()
{
    createView();
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setMinimumSize(new Dimension(400,200));
    setSize(400,200);
    setLocationRelativeTo(null);
    setResizable(true);
    setVisible(true);
}
public void createView()
{
    Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    // PANEL
    JPanel panel = new JPanel();
    panel.setBorder(new EmptyBorder(10,10,10,10));
    panel.setLayout(new BorderLayout());
    getContentPane().add(panel);
    //NORTH
    JPanel panelnorth = new JPanel(new BorderLayout());
    panel.add(panelnorth, BorderLayout.NORTH);
    panelnorth.add(new JLabel("Chapter 1"));
    //

   //TEXTFIELD CENTER
    JTextArea plot = new JTextArea();
    plot.setLineWrap(true);
    plot.setWrapStyleWord(true);
    plot.setEditable(true);
    JScrollPane scrollPane = new JScrollPane(plot);
    panel.add(scrollPane);
    //S
    //SOUTH
    JPanel south = new JPanel();
    panel.add(south, BorderLayout.SOUTH);
    JButton button1 = new JButton("HI");
    JButton button2 = new JButton("Okay");
    JButton button3 = new JButton("Bye");
    button1.setPreferredSize(new Dimension(screenSize.width, 10));
    south.add(button1);
    south.add(button2);
    south.add(button3);

    }
}

对于您的设计,GridLayout 可能是最佳选择。在这种情况下,GridLayout 应该使用构造函数 GridLayout(int rows, int cols).

将行设置为 0(这意味着任意数量的行)并将列设置为 1(意味着只有 1 列)

扩展您的源代码以进行演示:

JPanel south = new JPanel();
south.setLayout(new GridLayout(0, 1));
panel.add(south, BorderLayout.SOUTH);
JButton button1 = new JButton("HI");
JButton button2 = new JButton("Okay");
JButton button3 = new JButton("Bye");
south.add(button1);
south.add(button2);
south.add(button3);