我是 Java Swing 的新手,但我遇到了一个问题。
我必须创建一个登录窗口,灵感来自这样的图像(像这样,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 的小框架,其中涉及startup() 方法的定义,但这是纯 Swing 代码。唯一要说的是我通过以下代码行获取了主 *JFrame *:
this.getMainFrame()
这个例子似乎效果不好,事实上,我在登录表单可视化中遇到了一些美观问题,因为我得到了以下结果:
正如您所看到的,它存在许多错误:
- JLabel 和 JTextField 相互垂直压缩
- 在上一个示例中,元素居中且未向左对齐
- JTextField 太短
我可以尝试什么来解决这些问题?
JPanel passwordLabelPanel = new JPanel();
passwordLabelPanel.add(new JLabel("Password"));
body.add(passwordLabelPanel);
您在几个地方做了类似的代码,放置了一个组件(JLabel/JTextFeild
) 在另一个容器中,然后将其添加到根容器body
。任何面板都有FlowLayout
作为其默认布局,它遵循您要添加到其中的组件的首选大小。您正在添加您的password label
to passwordLabelPanel
但没有设置它的首选尺寸。您遇到的同样问题TextField
它是容器passwordTextPanel
。您可以为此设置首选尺寸label
and textFeild
得到一些可能令您满意的输出。但事实是,您根本不需要这个外面板。只需将标签和文本字段直接添加到body
panel.
我可以尝试什么来解决这些问题?
上面的解释也是一种解决方法。但从长远来看,除非您学习布局管理器,否则它们对您来说没有任何意义。
最好、最简单、有效、值得付出努力的方法就是从这里开始:课程:在容器内布置组件 http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html,相信我,它会为您(包括我们)节省大量时间。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)