我的文本游戏的GUI布局

时间:2017-07-03 22:11:02

标签: java user-interface layout jpanel jbutton

您好我正在尝试使用边框布局处理基于文本的游戏的GUI,而我几乎已经尝试使用GUI实现的目标。

这就是我想要的。几乎所有东西都下来了...除了按钮。我希望它在我的草图中看起来像它。

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

enter image description here

这是我的设置GUI的课程:

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);

    }
}

1 个答案:

答案 0 :(得分:0)

对于您的设计,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);
相关问题