[英]How to solve these visualizations problems in this Java Swing login windows?
我絕對不是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()
該示例似乎工作不佳,實際上我在登錄表單可視化中存在一些美學問題,因為我獲得了以下結果:
如您所見,它存在許多錯誤:
我該如何解決這些問題?
JPanel passwordLabelPanel = new JPanel();
passwordLabelPanel.add(new JLabel("Password"));
body.add(passwordLabelPanel);
您已經在幾個地方完成了類似的代碼,將一個組件( JLabel/JTextFeild
)放在另一個容器中,然后將其添加到根容器body
。 任何面板都將FlowLayout
作為其默認布局,它遵循要添加到其的組件的首選大小。 您正在將password label
添加到passwordLabelPanel
,但未設置其首選大小。 與TextField
及其容器passwordTextPanel
相同的問題。 您可以為此label
和textFeild
設置首選大小,以獲得一些可能令您滿意的輸出。 但事實是,您根本不需要此外部面板。 只需將標簽和文本字段直接添加到body
面板即可。
我該如何解決這些問題?
上面的解釋也是一種解決方案。 但是從長遠來看,除非您學習布局管理器,否則它們對您沒有任何意義。
值得嘗試的最好,最簡單,有效的方法是從這里開始: 課程:在容器中布置組件 ,相信我,它將為您和我們節省很多時間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.