私はJava Swingの初心者で、問題があります。
この画像のようなものからインスピレーションを得て、ログイン ウィンドウを作成する必要があります (このようなものです。ウィンドウには、ユーザーがユーザー名とパスワードを挿入する 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("MyApplication Login");
this.getMainFrame().setSize(600, 350); // Setta le dimensioni del JFrame che rappresenta la finestra principale
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(6, 1));
body.add(new JLabel("Username"));
JTextField userName = new JTextField();
body.add(userName);
body.add(new JLabel("Password"));
JTextField password = new JTextField();
body.add(password);
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);
}
}
このクラスは、 startup()メソッドの定義を含むJDesktopという小さなフレームワークを使用しますが、これは純粋な Swing コードです。唯一のことは、次のコード行でメインの **JFrame ** を取得することです。
this.getMainFrame()
この例は機能しているように見えますが、次の結果が得られるため、ログイン フォームの視覚化に美学上の問題があります。
ご覧のとおり、これは非常に厄介であり、構造が例と同じである場合、次のような問題があります。
- JTextFieldの高さが小さすぎます
- 要素間に上、左、右の余白がありません
- フォントが小さすぎる
このエラーを何らかの方法で修正できますか? いくつかの提案を手伝ってもらえますか?私の窓の構造は大丈夫ですか?
TNX
アンドレア