2012-04-13 96 views
0

這就是它輸出:我將如何將這些控件與左側對齊?

This is what it outputs :

我要對齊「Imprimante:MonImprimante」最左邊 還有3個複選框。

您可以找到有關我想通過搜索***用CTRL-F對齊的事情代碼

這是我的代碼: `

package gui; 

import java.awt.BorderLayout; 
import java.awt.GridLayout; 

import javax.swing.JButton; 
import javax.swing.JCheckBox; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JRadioButton; 
import javax.swing.JSlider; 

public class ImprimanteWindow extends JFrame{ 

    //Declares the panels used to place the controls. 
private JPanel JPanelBtn; 
private JPanel JPanelChk; 
private JPanel JPanelRad; 
private JPanel JPanelDrpChk; 
private JPanel JPanelWrap; 
private JPanel JPanelSuperWrap; 
private String[] QltImpression = { "Haute", "Medium", "Bas"}; 

public ImprimanteWindow(){ 


    //Initialise the panels 
    JPanelBtn = new JPanel(new GridLayout(4,1, 10, 10)); 
    JPanelChk = new JPanel(new GridLayout(3,1)); 
    JPanelRad = new JPanel(new GridLayout(3,1)); 
    JPanelDrpChk = new JPanel(); 
    JPanelWrap = new JPanel(); //used to put the other panels inside 
    JPanelSuperWrap = new JPanel(); //used to put everything in 1 big panel 

      //Initialise the buttons and add them to the button Panel 
    JPanelBtn.add(new JButton("Ok")); 
    JPanelBtn.add(new JButton("Annuler")); 
    JPanelBtn.add(new JButton("Paramètre")); 
    JPanelBtn.add(new JButton("Aide")); 
    this.add("East", JPanelBtn); 

      //***I want to align this to the far left 
      //adds the check boxes to the checkbox panel 
    JPanelChk.add(new JCheckBox("Image")); 
    JPanelChk.add(new JCheckBox("Texte")); 
    JPanelChk.add(new JCheckBox("Code")); 

      ////adds the radio buttons to the radiobutton panel 
    JPanelRad.add(new JRadioButton("Selection")); 
    JPanelRad.add(new JRadioButton("Tous")); 
    JPanelRad.add(new JRadioButton("Applet")); 

      //***I want to align this to the far left 
      //adds the label to the top section of the panel 
    JPanelSuperWrap.add(new JLabel("Imprimante : MonImprimante")); 

      //adds the 2 panels and the slider to the middle section 
    JPanelWrap.add(JPanelChk); 
    JPanelWrap.add(JPanelRad); 
    JPanelWrap.add(new JSlider()); 
    JPanelSuperWrap.add(JPanelWrap);  

      //adds a label, a combobox and a checkbox to the bottom. 
    JPanelDrpChk.add(new JLabel("Qualite de l'impression :")); 
    JPanelDrpChk.add(new JComboBox(QltImpression)); 
    JPanelDrpChk.add(new JCheckBox("Imprimer dans un fichier")); 
    JPanelSuperWrap.add(JPanelDrpChk); 
    this.add(JPanelSuperWrap); 

    this.pack(); 

    this.setSize(500,170); 
    this.setVisible(true); 
    this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
    this.validate(); 
} 


public static void main(String args[]){ 
    ImprimanteWindow gui = new ImprimanteWindow(); 

} 
}` 

在放置控件任何幫助我的gui將會大受歡迎! 非常感謝你們!

+1

我想你會需要使用比'GridLayout'不同的佈局管理器,如果這個問題不回答後,我回來吃午飯的形式,我會張貼一些代碼。 – 2012-04-13 16:36:15

+0

請學習java命名約定並堅持使用它們。 – kleopatra 2012-04-14 10:24:48

回答

1

您使用硬設置佈局的嵌套面板使生活變得更難,然後使用不具有硬佈局的包裝。如果你打算在微觀層次上進行明確的控制並且有特定的顯示要求(比如你的標籤情況),你應該在JPanelWrapJPanelSuperWrap中有明確的佈局。

此外,使用更好地傳達您的意圖的佈局。爲什麼在BoxLayout更好地表達您的意圖時,您在嵌套組件中使用3x1 GridLayout?你這樣做的Swing等效HTML嵌套<table>佈局。因爲它們是簡單的列,所以可以使內部面板使用填充框,並使用2×2 GridBagLayout作爲JPanelSuperWrapJPanelBtn佔用2行。

你可能也想比什麼那些面板實際上是更好的名稱,你真的應該考慮封裝由此看來成從ImprimanteWindow實例子。換句話說,把JPanelBtn這樣的片段放到另一個語義上定義那個分組的類中,比如SelectionChoices,然後在那個類上定義一個返回JPanel的方法。

0

每次添加JCheckBox,JLabel或JRadioButton(從此處開始標記爲j),請嘗試調用j.setHorizontalTextAlignment(SwingConstants.LEFT);。然而,我懷疑這可能不起作用,因爲你正在使用GridLayout,這是一個相當不靈活的佈局。

這將是更好,IHMO,如果在JPanels這些組件使用GridBagLayout和使用GridBagConstraints告訴佈局如何使這些組件時,您將它們添加到各自的面板。

0

要做到這一點,你只要做到這一點

JPanel scope = new JPanel(); 
scope.setBorder(BorderFactory.createTitledBorder("Scope")); 
scope.setLayout(new BoxLayout(scope,BoxLayout.Y_AXIS)); 
scope.setAlignmentX(Component.LEFT_ALIGNMENT);