如何在Java Swing登录窗口中解决这些可视化问题?
我在Java Swing中绝对是新手,我遇到了问题。
我必须创建一个登录窗口,从这个immage(类似这样的东西,scilicet窗口必须显示2个文本字段,用户插入其用户名和密码和一个按钮来执行登录操作)的灵感:
好吧,我认为这很简单,我已经意识到以下课程:
package com.techub.crystalice.gui.login; import java.awt.BorderLayout; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import org.jdesktop.application.SingleFrameApplication; import com.techub.crystalice.gui.Constants; import com.techub.crystalice.gui.GUI; public class LoginFrame extends SingleFrameApplication { @Override protected void startup() { // TODO Auto-generated method stub System.out.println("DENTRO: LoginFrame() ---> startup()"); this.getMainFrame().setTitle("MyApp Login"); //this.getMainFrame().setSize(600, 350); // Setta le dimensioni del JFrame che rappresenta la finestra principale this.getMainFrame().pack(); Container mainContainer = this.getMainFrame().getContentPane(); // Recupera l'oggetto Container del JFrame // Imposta un layput manager di tipo GridLayout per il contenitore principale: 3 righe ed una singola colonna: mainContainer.setLayout(new GridLayout(3,1)); // Contenitore rappresentato da 6 righe a singola colonna contenente i campi testuali e di input del login: JPanel body = new JPanel(); body.setLayout(new GridLayout(5, 1)); JPanel usernNameLabelPabel = new JPanel(); usernNameLabelPabel.add(new JLabel("Username")); body.add(usernNameLabelPabel); JPanel userNameTextPanel = new JPanel(); JTextField userName = new JTextField(); userNameTextPanel.add(userName); body.add(userNameTextPanel); JPanel passwordLabelPanel = new JPanel(); passwordLabelPanel.add(new JLabel("Password")); body.add(passwordLabelPanel); JPanel passwordTextPanel = new JPanel(); JTextField password = new JTextField(); passwordTextPanel.add(password); body.add(passwordTextPanel); this.getMainFrame().add(body); // Aggiunge al JFrame principale il JPanel contenente il form di login show(this.getMainFrame()); JPanel footer = new JPanel(); footer.setLayout(new FlowLayout(FlowLayout.CENTER)); JButton loginButton = new JButton("Login"); footer.add(loginButton); this.getMainFrame().add(footer); // Aggiunge al JFrame principale il JPanel contenente il bottone di login } public static void main(String[] args) { System.out.println("DENTRO: LoginFrame() ---> main()"); launch(LoginFrame.class, args); } }
这个类使用名为JDesktop的litle框架,它涉及startup()方法的定义,但这是纯Swing代码。 唯一要说的是我通过以下代码行获取main * JFrame *:
this.getMainFrame()
这个例子看起来很糟糕,事实上我在登录表单可视化中遇到了一些美学问题,因为我得到了以下结果:
正如您所看到的,它存在许多错误:
- JLabel和JTextField分别垂直压缩到另一个
- 元素在上一个示例中居中且未与左对齐
- JTextField很简短
我可以尝试解决这些问题?
JPanel passwordLabelPanel = new JPanel(); passwordLabelPanel.add(new JLabel("Password")); body.add(passwordLabelPanel);
你已经在几个地方做过类似的代码,将一个组件( JLabel/JTextFeild
)放在另一个容器中,然后将它添加到根容器body
。 任何Panel都有FlowLayout
作为其默认布局,它遵循您要添加到其中的组件的首选大小。 您正在将password label
添加到passwordLabelPanel
,但未设置其首选大小。 与TextField
及其容器passwordTextPanel
相同的问题。 您可以将首选大小设置为此label
,将textFeild
为可能满足您的输出。 但事实是,你根本不需要这个外板。 只需将标签和文本字段直接添加到body
面板即可。
我可以尝试解决这些问题?
以上说明也是一种解决方案。 但从长远来看,除非你学习布局管理器,否则它们对你没有任何意义。
值得付出努力的最好,最简单,最有效的方法是从这里开始: 课程:在容器中布置组件 ,相信我,它将为您节省包括我们在内的大量时间。