哪个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:将设计分为较小的,易于布局的容器. 在这种情况下,例如,首先将设计分为三个区域:
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屋!