2012-04-13 3 views
0

は:遠く 左だけでなく、3つのチェックボックスに「MonImprimante Imprimante」をこれらのコントロールを左側に並べるにはどうすればよいですか?これは、それが出力するものである

This is what it outputs :

私が整列します。

に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

いくつかのコードを投稿してください。 –

+0

Java命名規則を学び、それらに固執してください。 – kleopatra

答えて

1

ハード設定されたレイアウトを持つネストされたパネルとそうでないラッパーを使用すると、人生がより困難になります。マイクロレベルで明示的な制御を行い、特定の表示要件(ラベルの場合など)を使用する場合は、JPanelWrapJPanelSuperWrapの明示的なレイアウトが必要です。

また、あなたの意図をよりよく伝えるレイアウトを使用してください。 BoxLayoutがあなたの意図をよりよく表している場合、なぜ3x1 GridLayoutを入れ子にしたコンポーネントで使用していますか?あなたは、HTMLネストされた<table>レイアウトのSwing同等物をそのようにしています。インナーパネルは、単純な列なので、フィラー付きのボックスを使用することができ、JPanelSuperWrapの2x2 GridBagLayoutを使用し、JPanelBtnの2行を使用します。

また、これらのパネルが実際にどのようなものであるかよりも、より良い名前が必要な場合があります。実際には、このビューをImprimanteWindowからインスタンス化されたサブコンポーネントにカプセル化することを検討する必要があります。言い換えれば、JPanelBtnのような部分をとり、SelectionChoicesのように意味的にそのグループが何であるかを定義する別のクラスに入れて、そのクラスにJPanelを返すメソッドを定義します。

0

JCheckBox、JLabel、またはJRadioButton(ここからjと表示されます)を追加するたびに、j.setHorizontalTextAlignment(SwingConstants.LEFT);を呼び出してみてください。しかし、これはかなり柔軟性のないレイアウトであるGridLayoutを使用しているため、これはうまくいかない可能性があります。

これらのコンポーネントにを使用する場合はを使用する方が良いでしょう。GridBagConstraintsを使用して、それらのコンポーネントをそれぞれのパネルに追加するときの配置方法をレイアウトに伝えます。

0

これを行うには、あなただけやる、私はこの質問が回答されていない場合、私は、私は意志フォーム昼食取り戻す後あなたは、GridLayout` `とは異なるレイアウトマネージャを使用する必要があるだろうと思い、この

JPanel scope = new JPanel(); 
scope.setBorder(BorderFactory.createTitledBorder("Scope")); 
scope.setLayout(new BoxLayout(scope,BoxLayout.Y_AXIS)); 
scope.setAlignmentX(Component.LEFT_ALIGNMENT); 
関連する問題