2017-07-03 116 views
0

您好我正在嘗試使用邊框佈局來處理基於文本的遊戲的GUI,而且我幾乎想要用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); 

    } 
} 
+0

'BorderLayout'舉行的兩個主要組成部分,一個'GridLayout'舉行裹在自己的分量和放置「按鈕」在'BorderLayout.SOUTH '位置 – MadProgrammer

回答

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

你很完美。謝謝! –