2017-06-09 7 views
0

Ich benutze eine Gridbaglayout für mein Layout, und ich frage mich, wie man es neu anordnen, so dass das Textfeld Textfeld direkt über das Passwort Textfeld und das Login geht Schaltfläche geht unter das Passwort Textfeld.Java Swing - Wie stapelt man Benutzername Textfeld und Passwort Textfeld

Derzeit meinen Code Ausgänge: enter image description here

Allerdings möchte ich etwas wie folgt aus:

enter image description here

Mein Code:

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class TestingJavaCode { 

    public TestingJavaCode() { 

     JFrame appFrame = new JFrame(); 
     JPanel loginPanel = new JPanel(); 
     appFrame.setSize(1200, 800); 
     loginPanel = new JPanel(new GridBagLayout()); 


     // Button for logging in, with respective GridBagConstraint 
     // setFocusable is set to false to take out the border around the text 
     JButton loginButton = new JButton("Login"); 
     GridBagConstraints lButtonC = new GridBagConstraints(); 
     loginButton.setFocusable(false); 


     // Username text-field and JLabel with respective GridBagConstraints 
     JTextField tfUsername = new JTextField(15); 
     GridBagConstraints tfUserC = new GridBagConstraints(); 
     JLabel txtUser = new JLabel("Username: "); 
     GridBagConstraints txtUserC = new GridBagConstraints(); 


     // Password text-field and JLabel with respective GridBagConstraints 
     JPasswordField tfPassword = new JPasswordField(15); 
     GridBagConstraints tfPassC = new GridBagConstraints(); 
     JLabel txtPassword = new JLabel("Password: "); 
     GridBagConstraints txtPassC = new GridBagConstraints(); 

     // Add all components to the JFrame 
     // Making sure to add the text before the text-fields 
     loginPanel.add(txtUser, txtUserC); 
     loginPanel.add(tfUsername, tfUserC); 
     loginPanel.add(txtPassword, txtPassC); 
     loginPanel.add(tfPassword, tfPassC); 
     loginPanel.add(loginButton, lButtonC); 

     // Show the frame 
     appFrame.add(loginPanel); 
     appFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     appFrame.setVisible(true); 
    } 


    public static void main(String[] args) { 
     new TestingJavaCode(); 
    } 

} 

EDIT: Ich bin offen für die Verwendung anderer Layout-Manager.

+0

warum fügen Sie die Etiketten dann 'loginPanel.add (txtUser, txtUserC);' –

+0

@ScaryWombat ich die Etiketten auch wollen. Ich möchte etwas wie das Bild, das ich gepostet habe, aber ich möchte die Etiketten auf der linken Seite der Textfelder, wie mein Code produziert. – Theo

+0

Nun, Sie müssen tatsächlich einige Einschränkungen festlegen, um dem Layout-Manager mitzuteilen, wo die Komponenten positioniert werden sollen. Sehen Sie sich die gridx/y-Einschränkungen an, die im Swing-Lernprogramm zu [Verwendung von GridBagLayout] (http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html) beschrieben sind. Das Tutorial enthält ein funktionierendes Beispiel, das Sie herunterladen und ändern können. – camickr

Antwort

4

Da Sie keine Gridx/Y-Einschränkungen verwenden möchten, warum verwenden Sie nicht einfach die Kombination BoxLayout und FlowLayout? Ich habe zwei neue Panels hinzugefügt. Eine für Benutzername und eine für Passwort und da FlowLayout das Standardlayout für Panel ist, können wir es einfach so lassen wie es ist.

FlowLayout ist der Standard-Layout-Manager für jedes JPanel. Es einfach legt Komponenten in einer einzelnen Zeile, beginnend eine neue Zeile, wenn seine Container nicht ausreichend breit ist.

Danach lässt eingestellt BoxLayout auf Ihre loginPanel und nur Benutzername und Passwort Panel hinzufügen.

Die BoxLayout-Klasse fügt Komponenten in eine einzelne Zeile oder Spalte ein. Es respektiert die angeforderten maximalen Größen der Komponenten und lässt Sie auch Komponenten ausrichten. Hier

ist die komplette aktualisierte Quelle:

import javax.swing.*; 

public class TestingJavaCode { 

    public TestingJavaCode() { 

     JFrame appFrame = new JFrame(); 
     JPanel loginPanel = new JPanel(); 
     appFrame.setSize(300, 130); 
     loginPanel.setLayout(new BoxLayout(loginPanel, BoxLayout.PAGE_AXIS)); 

     // Button for logging in, with respective GridBagConstraint 
     // setFocusable is set to false to take out the border around the text 
     JButton loginButton = new JButton("Login"); 
     loginButton.setFocusable(false); 

     JPanel usernamePanel = new JPanel(); 
     // Username text-field and JLabel with respective GridBagConstraints 
     JTextField tfUsername = new JTextField(15); 
     JLabel txtUser = new JLabel("Username: "); 

     usernamePanel.add(txtUser); 
     usernamePanel.add(tfUsername); 

     JPanel passwordPanel = new JPanel(); 
     // Password text-field and JLabel with respective GridBagConstraints 
     JPasswordField tfPassword = new JPasswordField(15); 
     JLabel txtPassword = new JLabel("Password: "); 

     passwordPanel.add(txtPassword); 
     passwordPanel.add(tfPassword); 

     // Add all components to the JFrame 
     // Making sure to add the text before the text-fields 
     loginPanel.add(usernamePanel); 

     loginPanel.add(usernamePanel); 
     loginPanel.add(passwordPanel); 
     loginPanel.add(loginButton); 
     // Show the frame 
     appFrame.add(loginPanel); 
     appFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     appFrame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     new TestingJavaCode(); 
    } 

} 
+0

Awesome! Das ist, was ich gesucht habe. Gibt es dieses Layout, wenn ich das Fenster vergrößere? Wenn ich das Fenster erweitere, wird der Abstand zwischen Benutzername, Passwort und Login immens groß. – Theo

Verwandte Themen