【问题标题】:GUI Layout of My Text Game我的文本游戏的 GUI 布局
【发布时间】:2017-12-07 05:55:44
【问题描述】:

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

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

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

这是我设置 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);

    }
}

【问题讨论】:

  • BorderLayout 保存两个主要组件,一个GridLayout 保存包裹在自己组件中并放置在BorderLayout.SOUTH 位置的“按钮”

标签: java user-interface layout jpanel jbutton


【解决方案1】:

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

【讨论】:

  • 你是完美的。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多