哪个 Swing 布局管理器可以获得我想要的布局? [英] Which Swing layout manager to get my desired layout?
问题描述
我正在尝试按照这个模型制作一个基本的登录菜单:
I am trying to make a basic login menu following this mock up :
我决定将整个菜单放入一个 JPanel 中,以便在连接成功后切换到另一个面板.
I decided to put this whole menu into a JPanel so I can switch to another panel once the connexion is successful.
所以我决定使用Borderlayout在北区有标题,在南区有连接按钮.
So I decided to use a Borderlayout to have the title in north area and the connect button in the south area .
我将边框布局的中心作为面板本身.我决定让它成为一个网格布局,既包含标签(登录名、密码),又包含用户输入 ID 的文本字段.
I made the center of the borderlayout a panel itself . I decided to make it a gridlayout to both have the labels(login,password) but also the textfield in which the user will put his id.
结果非常难看,与我预期的相差甚远:
The result is very ugly and very far from what I expected :
这是菜单的代码:
public class EcranAccueil extends JPanel {
private JLabel labelTitre;
private JPanel PanelConnexion;
private JButton boutonConnexion;
private JLabel labelLogin;
private JLabel labelMotDepasse;
private JTextField loginUser;
private JTextField MotDepasseUser;
EcranAccueil(EcranGestion EcranPrincipale){
PanelConnexion = new JPanel();
this.setLayout(new BorderLayout());
PanelConnexion.setLayout(new GridLayout(2,2));
loginUser = new JTextField("User");
loginUser.setMinimumSize(new Dimension(20,20));
loginUser.setMaximumSize(new Dimension(20,20));
MotDepasseUser = new JTextField("Password");
boutonConnexion = new JButton("Connect");
boutonConnexion.setMinimumSize(new Dimension(200,200));
boutonConnexion.setMaximumSize(new Dimension(200,200));
labelTitre= new JLabel("ApplicationName");
labelLogin= new JLabel("Login");
labelMotDepasse = new JLabel("Password");
PanelConnexion.add(labelLogin);
PanelConnexion.add(loginUser);
PanelConnexion.add(labelMotDepasse);
PanelConnexion.add(MotDepasseUser);
this.add(labelTitre, BorderLayout.NORTH);
this.add(PanelConnexion, BorderLayout.CENTER);
this.add(boutonConnexion, BorderLayout.SOUTH);
} }
我尝试使用 gridboxlayout 但我完全没有使用它并且它没有编译.有没有人有意见或建议?
I tried to use a gridboxlayout but I completely failed at using it and it did not compile. Does anyone have advices or suggestion?
推荐答案
解决复杂计算任务的常见策略是将它们分解为小的、定义明确的可管理任务.分而治之.这也适用于 gui:将设计分解为小而易于布局的容器.在这种情况下,例如首先将设计分为 3 个区域:
A common strategy to solve complex computing tasks, is to break them into small, well defined manageable tasks. Divide and conquer. This also applies to gui: break the design into small, easy to layout containers. In this case, for example start by dividing the design into 3 areas:
每个这样的区域都由一个嵌套面板实现.正如您在代码中看到的那样,mainPanel
被进一步划分为两个嵌套的面板,以简化和改进布局:
Each such area is implemented by a nested panel.
As you can see in the code, mainPanel
is further divided into two nested panels, to ease and improve layout:
class EcranAccueil extends JPanel {
EcranAccueil(){
//Set layout (JPanel uses Flowlayout by default)
setLayout(new BorderLayout(5,5));
// a nested panel for application label
JPanel topPanel = new JPanel();
add(topPanel, BorderLayout.NORTH);
topPanel.setLayout(new FlowLayout(FlowLayout.LEADING));//set
JLabel labelTitre= new JLabel("ApplicationName");
topPanel.add(labelTitre);
// a nested panel for login and password, having two rows
JPanel mainPanel = new JPanel(new GridLayout(2, 1));
add(mainPanel, BorderLayout.CENTER);
JPanel loginPanel = new JPanel();
loginPanel.setLayout(new FlowLayout(FlowLayout.TRAILING));
mainPanel.add(loginPanel);
JLabel labelLogin = new JLabel("Login");
loginPanel.add(labelLogin);
JTextField loginUser = new JTextField("User");
loginUser.setColumns(10);
loginPanel.add(loginUser);
JPanel passwordPanel = new JPanel();
passwordPanel.setLayout(new FlowLayout(FlowLayout.TRAILING));
mainPanel.add(passwordPanel);
JLabel labelMotDepasse = new JLabel("Password");
passwordPanel.add(labelMotDepasse);
JTextField motDepasseUser = new JTextField("Password");
motDepasseUser.setColumns(10);
passwordPanel.add(motDepasseUser);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
add(buttonPanel,BorderLayout.SOUTH);
JButton boutonConnexion = new JButton("Connect");
buttonPanel.add(boutonConnexion);
}
}
一旦你有了基本的想法,就可以进一步改进布局及其响应能力.
Once you get the basic idea, the layout and its responsiveness can be further improved.
More examples of applying this strategy: 1 2 and 3
这篇关于哪个 Swing 布局管理器可以获得我想要的布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!