这是它的输出:
我想对齐最左边的“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 上的任何帮助都将得到极大的帮助!非常感谢你们!