2013-12-23 48 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