2012-04-13 97 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);