gpt4 book ai didi

java - 我如何将这些控件向左对齐?

转载 作者:行者123 更新时间:2023-12-01 15:26:01 29 4
gpt4 key购买 nike

这就是它的输出:

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();

}
}`

任何将控件放置在我的图形用户界面上的帮助将不胜感激!非常感谢你们!

最佳答案

使用具有硬集布局的嵌套面板和不具有硬集布局的包装器会让生活变得更加困难。如果您要在微观层面进行显式控制并具有特定的显示要求(例如标签大小写),则应该在JPanelWrap中有显式布局。和JPanelSuperWrap .

此外,使用可以更好地传达您的意图的布局。你为什么使用 3x1 GridLayout在嵌套组件中,当 BoxLayout能更好地表达你的意图吗?您所做的 Swing 相当于 HTML 嵌套 <table>就这样布局。您可以使内部面板使用带有填充物的框,因为它们是简单的列,并使用 2x2 GridBagLayout JPanelSuperWrap ,与 JPanelBtn占用 2 行。

您可能还需要比这些面板实际名称更好的名称,并且您确实应该考虑将此 View 封装到从 ImprimanteWindow 实例化的子组件中。换句话说,取像JPanelBtn这样的片段。并将其放入另一个类中,该类在语义上定义了该分组是什么,例如 SelectionChoices ,然后在该类上定义一个返回 JPanel 的方法。

关于java - 我如何将这些控件向左对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10144617/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com