2012-10-15 66 views
1

我對java swing有點新,我正在嘗試通過手工來實踐。在下面的例子中,我有一個設置,我有一個GridLayout以我想要的方式顯示字段。但我需要重新調整文本字段的大小,因爲我不希望它們的大小都一樣。我需要一些更大或更小。有沒有辦法做到這一點?還有一種方法可以刪除標籤之後的一些填充?在網格佈局在GridLayout中重新調整文本字段的大小

public void setupFrame() { 
    pnlTop.setLayout(new GridLayout(3, 4, 10, 10)); 

    lblClosetLoc.setText("Closet location:"); 
    lblPhone1.setText("Phone 1:"); 
    lblJackPaired.setText("Jack paired:"); 
    lblPhone2.setText("Phone 2:"); 
    lblCubicle.setText("Cubicle:"); 
    lblJackType.setText("Jack type:");  
    txtClosetLoc.setEditable(false); 
    txtClosetLoc.setText("");  
    txtPhone1.setEditable(false); 
    txtPhone1.setText("");  
    txtJackPaired.setEditable(false); 
    txtJackPaired.setText("");  
    txtPhone2.setEditable(false); 
    txtPhone2.setText("");  
    txtCubicle.setEditable(false); 
    txtCubicle.setText("");  
    txtJackType.setEditable(false); 
    txtJackType.setText("");   

    pnlTop.add(lblClosetLoc); 
    pnlTop.add(txtClosetLoc); 
    pnlTop.add(lblPhone1); 
    pnlTop.add(txtPhone1); 
    pnlTop.add(lblJackPaired); 
    pnlTop.add(txtJackPaired); 
    pnlTop.add(lblPhone2); 
    pnlTop.add(txtPhone2); 
    pnlTop.add(lblCubicle); 
    pnlTop.add(txtCubicle); 
    pnlTop.add(lblJackType); 
    pnlTop.add(txtJackType); 

    getContentPane().add(pnlTop);  

    setTitle("Test"); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setLocation(300, 300); 
    pack(); 
} 
+0

使用'GridBagLayout'或'MigLayout'。 – Stephan

回答

2

大小調整文本框

GridLayout基本性質是調整其子在方向和fills/schrinks所有可用的面積從其父來到

還有一種方法可以刪除標籤之後的一些填充?

pading由.... 10, 10)pnlTop.setLayout(new GridLayout(3, 4, 10, 10));

我需要一些是更大或更小限定。有沒有辦法做到這一點?

來看看GridBagLayoutSpringLayout

+0

我正在瀏覽SpringLayout,它似乎是我正在尋找的!謝謝! :) – Caveman42

+0

@ user1732377很高興如果幫助你,不要忘記爲JLabel(SwingConstant)設置對齊方式,順便說一句,適用於所有JTextComponents – mKorbel

0

任何有興趣,這是我得到了什麼之後我做SpringLayout的形式。如果我在某處出錯,請糾正我:

public void setupFrame() { 
    SpringLayout layout = new SpringLayout(); 

    pnlTop.setLayout(layout); 

    lblClosetLoc.setText("Closet location:"); 
    lblPhone1.setText("Phone 1:"); 
    lblJackPaired.setText("Jack paired:"); 
    lblPhone2.setText("Phone 2:"); 
    lblCubicle.setText("Cubicle:"); 
    lblJackType.setText("Jack type:"); 

    txtClosetLoc.setEditable(false); 
    txtClosetLoc.setText(""); 

    txtPhone1.setEditable(false); 
    txtPhone1.setText(""); 

    txtJackPaired.setEditable(false); 
    txtJackPaired.setText(""); 

    txtPhone2.setEditable(false); 
    txtPhone2.setText(""); 

    txtCubicle.setEditable(false); 
    txtCubicle.setText(""); 

    txtJackType.setEditable(false); 
    txtJackType.setText(""); 


    pnlTop.add(lblClosetLoc); 
    pnlTop.add(txtClosetLoc); 
    pnlTop.add(lblPhone1); 
    pnlTop.add(txtPhone1); 
    pnlTop.add(lblJackPaired); 
    pnlTop.add(txtJackPaired); 
    pnlTop.add(lblPhone2); 
    pnlTop.add(txtPhone2); 
    pnlTop.add(lblCubicle); 
    pnlTop.add(txtCubicle); 
    pnlTop.add(lblJackType); 
    pnlTop.add(txtJackType); 

    layout.putConstraint(layout.WEST, lblClosetLoc, 5, layout.WEST, pnlTop); 
    layout.putConstraint(layout.NORTH, lblClosetLoc, 1, layout.NORTH, txtClosetLoc); 

    layout.putConstraint(layout.WEST, txtClosetLoc, 5, layout.EAST, lblClosetLoc); 
    layout.putConstraint(layout.NORTH, txtClosetLoc, 5, layout.NORTH, pnlTop); 

    layout.putConstraint(layout.WEST, lblPhone1, 10, layout.EAST, txtCubicle); 
    layout.putConstraint(layout.NORTH, lblPhone1, 1, layout.NORTH, txtPhone1); 

    layout.putConstraint(layout.WEST, txtPhone1, 5, layout.EAST, lblJackType); 
    layout.putConstraint(layout.NORTH, txtPhone1, 5, layout.NORTH, pnlTop); 

    layout.putConstraint(layout.WEST, lblJackPaired, 5, layout.WEST, pnlTop); 
    layout.putConstraint(layout.NORTH, lblJackPaired, 1, layout.NORTH, txtJackPaired); 

    layout.putConstraint(layout.WEST, txtJackPaired, 22, layout.EAST, lblJackPaired); 
    layout.putConstraint(layout.NORTH, txtJackPaired, 5, layout.SOUTH, txtClosetLoc); 

    layout.putConstraint(layout.WEST, lblPhone2, 10, layout.EAST, txtCubicle); 
    layout.putConstraint(layout.NORTH, lblPhone2, 1, layout.NORTH, txtPhone2); 

    layout.putConstraint(layout.WEST, txtPhone2, 5, layout.EAST, lblJackType); 
    layout.putConstraint(layout.NORTH, txtPhone2, 5, layout.SOUTH, txtPhone1); 

    layout.putConstraint(layout.WEST, lblCubicle, 5, layout.WEST, pnlTop); 
    layout.putConstraint(layout.NORTH, lblCubicle, 1, layout.NORTH, txtCubicle); 

    layout.putConstraint(layout.WEST, txtCubicle, 47, layout.EAST, lblCubicle); 
    layout.putConstraint(layout.NORTH, txtCubicle, 5, layout.SOUTH, txtJackPaired); 

    layout.putConstraint(layout.WEST, lblJackType, 10, layout.EAST, txtCubicle); 
    layout.putConstraint(layout.NORTH, lblJackType, 1, layout.NORTH, txtJackType); 

    layout.putConstraint(layout.WEST, txtJackType, 5, layout.EAST, lblJackType); 
    layout.putConstraint(layout.NORTH, txtJackType, 5, layout.SOUTH, txtPhone2); 

    layout.putConstraint(layout.EAST, pnlTop, 5, layout.EAST, txtPhone1); 
    layout.putConstraint(layout.SOUTH, pnlTop, 5, layout.SOUTH, txtJackType); 

    getContentPane().add(pnlTop, BorderLayout.CENTER); 


    setTitle("Test"); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setResizable(false); 
    setLocation(300,300); 
    pack(); 
}