2013-12-23 46 views
2

我正在開發一個程序。它的GUI有兩個主要部分,JFrame的左邊和JFrame的右邊。 (目前右半部分是空白的,因爲我還沒有開始工作)。關於GUI的佈局

左側部分看起來不太好。所有的按鈕和文本框都被拉伸。我希望他們擁有標準按鈕的高度,與本網站上的按鈕相似。 (你知道,標準的Windows按鈕)。

我該怎麼做? (我不想簡單地打包()整個事情,因爲窗口的右半部分會有一個大的方形JPanel,所以pack()ing意味着窗口仍然是方形的而左半邊的按鈕仍然會被拉長和拉下)。

這裏有一個畫面:

enter image description here

下面是到目前爲止的代碼:

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

public class GUI extends JFrame { 

    JButton rect,oval,tri,free,addPoint; 
    JLabel xLabel,yLabel; 
    JTextField xTextField,yTextField; 
    JPanel leftPanel,rightPanel,optionsPanel,pointsPanel; 

    public GUI(){ 
     initUI(); 
    } 

    private void initUI(){ 

     setLayout(new GridLayout(1,2,5,5)); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setTitle("Graphics Generator"); 
     setSize(500,500); 

     rect = new JButton("Rectangle"); 
     oval = new JButton("Oval"); 
     tri = new JButton("Triangle"); 
     free = new JButton("Free Shape"); 
     addPoint = new JButton("Add point"); 

     xLabel = new JLabel("X: "); 
     yLabel = new JLabel("Y: "); 

     xTextField = new JTextField(2); 
     yTextField = new JTextField(2); 

     leftPanel = new JPanel(); 
     rightPanel = new JPanel(); 
     optionsPanel = new JPanel(); 
     pointsPanel = new JPanel(); 

     add(leftPanel); 
     add(rightPanel); 

     leftPanel.setLayout(new GridLayout(2,1,5,5)); 
     leftPanel.add(optionsPanel); 

     optionsPanel.setLayout(new GridLayout(1,4,2,2)); 

     optionsPanel.add(rect); 
     optionsPanel.add(oval); 
     optionsPanel.add(tri); 
     optionsPanel.add(free); 

     leftPanel.add(pointsPanel); 

     pointsPanel.setLayout(new GridLayout(1,5,2,2)); 

     pointsPanel.add(xLabel); 
     pointsPanel.add(xTextField); 
     pointsPanel.add(yLabel); 
     pointsPanel.add(yTextField); 
     pointsPanel.add(addPoint); 

     setVisible(true); 

    } 

    public static void main(String[] args) { 

     GUI gui = new GUI(); 

    } 

} 
+0

問題也許是你所選擇的佈局。嘗試使用不同的佈局並檢查結果? – ujvl

+1

你應該添加一個草圖,說明你想要完成的gui的樣子。只是一個線框素描,沒有什麼奇特的。 – Paaske

回答

0
Try this 


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

public class GUI extends JFrame { 

    JButton rect,oval,tri,free,addPoint; 
    JLabel xLabel,yLabel; 
    JTextField xTextField,yTextField; 
    JPanel leftPanel,rightPanel,optionsPanel,pointsPanel; 

    public GUI(){ 
     initUI(); 
    } 

    private void initUI(){ 

     setLayout(new GridLayout(1,2,5,5)); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setTitle("Graphics Generator"); 
     setSize(500,500); 

     rect = new JButton("Rectangle"); 
     oval = new JButton("Oval"); 
     tri = new JButton("Triangle"); 
     free = new JButton("Free Shape"); 
     addPoint = new JButton("Add point"); 

     JPnel p=new JPanel(); 
p.add(rect); 
p.add(oval); 
p.add(tri); 
p.add(free); 
p.add(addPoint); 
     xLabel = new JLabel("X: "); 
     yLabel = new JLabel("Y: "); 

     xTextField = new JTextField(2); 
     yTextField = new JTextField(2); 

     leftPanel = new JPanel(); 
     rightPanel = new JPanel(); 
     optionsPanel = new JPanel(); 
     pointsPanel = new JPanel(); 

     add(leftPanel); 
     add(rightPanel); 

     leftPanel.setLayout(new GridLayout(2,1,5,5)); 
     leftPanel.add(optionsPanel); 

     optionsPanel.setLayout(new GridLayout(1,4,2,2)); 

     optionsPanel.add(p); 
     //optionsPanel.add(oval); 
     //optionsPanel.add(tri); 
     //optionsPanel.add(free); 

     leftPanel.add(pointsPanel); 

     pointsPanel.setLayout(new GridLayout(1,5,2,2)); 

     pointsPanel.add(xLabel); 
     pointsPanel.add(xTextField); 
     pointsPanel.add(yLabel); 
     pointsPanel.add(yTextField); 
     pointsPanel.add(addPoint); 

     setVisible(true); 

    } 

    public static void main(String[] args) { 

     GUI gui = new GUI(); 

    } 

} 


Like this for JLabels and JTextFields 
0

請嘗試FlowLayout它是適合您的需求。

只要改變setLayout(new FlowLayout());

輸出:

Output

0

您有效果,因爲你用GridLayout,該調整組件全細胞(垂直/水平)。您需要使用另一個LayoutManager或佈局組合。

例如,我已經改變了你的代碼GridBagLayout

private void initUI(){ 

    setDefaultCloseOperation(EXIT_ON_CLOSE); 
    setTitle("Graphics Generator"); 
    setLayout(new GridBagLayout()); 

    rect = new JButton("Rectangle"); 
    oval = new JButton("Oval"); 
    tri = new JButton("Triangle"); 
    free = new JButton("Free Shape"); 
    addPoint = new JButton("Add point"); 

    xLabel = new JLabel("X: "); 
    yLabel = new JLabel("Y: "); 

    xTextField = new JTextField(2); 
    yTextField = new JTextField(2); 

    leftPanel = new JPanel(); 
    leftPanel.setBorder(BorderFactory.createLineBorder(Color.RED)); 
    rightPanel = new JPanel(); 
    rightPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE)); 
    optionsPanel = new JPanel(new GridBagLayout()); 
    pointsPanel = new JPanel(new GridBagLayout()); 

    GridBagConstraints cMain = new GridBagConstraints(); 
    cMain.insets = new Insets(5, 5, 5, 5); 
    cMain.gridx = 0; 
    cMain.gridy = 0; 
    cMain.anchor = GridBagConstraints.NORTHWEST; 
    add(leftPanel,cMain); 
    cMain.fill = GridBagConstraints.BOTH; 
    cMain.gridx++; 
    cMain.weighty = 1; 
    cMain.weightx = 1; 
    add(rightPanel,cMain); 

    leftPanel.setLayout(new GridBagLayout()); 

    GridBagConstraints c = new GridBagConstraints(); 
    c.insets = new Insets(5, 5, 5, 5); 
    c.gridx = 0; 
    c.gridy = 0; 
    c.anchor = GridBagConstraints.WEST; 
    leftPanel.add(optionsPanel,c); 
    c.gridy++; 
    leftPanel.add(pointsPanel,c); 

    c.gridy = 0; 
    optionsPanel.add(rect,c); 
    c.gridx++; 
    optionsPanel.add(oval,c); 
    c.gridx++; 
    optionsPanel.add(tri,c); 
    c.gridx++; 
    optionsPanel.add(free,c); 

    c.gridx = 0; 
    c.gridy = 1; 
    pointsPanel.add(xLabel,c); 
    c.gridx++; 
    pointsPanel.add(xTextField,c); 
    c.gridx++; 
    pointsPanel.add(yLabel,c); 
    c.gridx++; 
    pointsPanel.add(yTextField,c); 
    c.gridx++; 
    pointsPanel.add(addPoint,c); 

    setSize(500,500); 
    setVisible(true); 
} 

enter image description here