2016-06-13 57 views
-1

現在我有一個'登錄'JFrame設置。有2個標籤和2個用戶名和密碼字段。他們爲中心,表現爲這樣:Java JLabel和JTextField定位

username: |text field| 
password: |text field| 

JPanel loginLabel = new JPanel(new GridLayout(0, 1, 0, 10)); 
     loginLabel.add(usernameLabel); 
     loginLabel.add(passwordLabel); 
     loginLabel.setBackground(new Color(82,80,80)); 

     JPanel loginField = new JPanel(new GridLayout(0, 1, 0, 10)); 
     loginField.add(usernameField); 
     loginField.add(passwordField); 
     loginField.setBackground(new Color(82,80,80)); 

     JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); 
     buttonPanel.add(createButton); 
     buttonPanel.add(loginButton); 
     buttonPanel.add(quitButton); 
     buttonPanel.setBackground(new Color(82,80,80)); 

     setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); 
     setBackground(new Color(82,80,80)); 
     add(loginLabel, BorderLayout.CENTER); 
     add(loginField, BorderLayout.LINE_END); 
     add(buttonPanel, BorderLayout.SOUTH); 

我試圖讓所有的標籤和領域爲中心,但他們似乎喜歡這個:

Username 
|Text Field| 

    Password 
|Text Field| 

任何人都可以在這方面幫助?

+0

您可以將用戶名和密碼在面板內部,然後你可以設置面板BorderLayout.By這樣做,你可以將用戶名北美和密碼的佈局SOUTH,那麼你將這兩個添加到你的JFrame。 – theVoid

+0

爲了更快地獲得更好的幫助,請發佈[MCVE]或[簡短,獨立,正確的示例](http://www.sscce.org/)。 –

+3

順便說一句 - 我會這樣做的方式:1)使用一個'JPasswordField'作爲密碼,2)在一個面板中同時顯示'GridBagLayout'中的標籤和文本和密碼字段。 3)在'JOptionPane'中顯示該面板,該面板已經具備了在用戶名/密碼字段下面提供所需按鈕的功能。 –

回答

0

您可以嘗試使用的setBounds(X,Y,長度,heigth),你必須setLayout的(空)

0

不要使用既不GridLayout也不FlowLayout。他們毫無用處。我推薦GroupLayoutMigLayout

我用內置的GroupLayout管理器創建了一個解決方案。這是一個現代靈活的佈局管理器。

package com.zetcode; 

import java.awt.Container; 
import java.awt.EventQueue; 
import javax.swing.GroupLayout; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import static javax.swing.JFrame.EXIT_ON_CLOSE; 
import javax.swing.JLabel; 
import javax.swing.JPasswordField; 
import javax.swing.JTextField; 

public class GroupLayoutPasswordEx extends JFrame { 

    public GroupLayoutPasswordEx() { 

     initUI(); 
    } 

    private void initUI() { 

     JLabel userLabel = new JLabel("User"); 
     JTextField userField = new JTextField(); 

     JLabel pswdLabel = new JLabel("Password"); 
     JPasswordField pswdField = new JPasswordField(); 

     createLayout(userLabel, userField, pswdLabel, pswdField); 

     setTitle("Login"); 
     setSize(300, 200); 
     setLocationRelativeTo(null); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
    } 

    private void createLayout(JComponent... arg) { 

     Container pane = getContentPane(); 
     GroupLayout gl = new GroupLayout(pane); 
     pane.setLayout(gl); 

     gl.setAutoCreateContainerGaps(true); 
     gl.setAutoCreateGaps(true); 

     gl.setHorizontalGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER) 
       .addComponent(arg[0]) 
       .addComponent(arg[1]) 
       .addComponent(arg[2]) 
       .addComponent(arg[3]) 
     ); 

     gl.setVerticalGroup(gl.createSequentialGroup() 
       .addComponent(arg[0]) 
       .addComponent(arg[1], GroupLayout.DEFAULT_SIZE, 
         GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) 
       .addComponent(arg[2]) 
       .addComponent(arg[3], GroupLayout.DEFAULT_SIZE, 
         GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) 
     ); 
    }  


    public static void main(String[] args) { 
     EventQueue.invokeLater(() -> { 
      GroupLayoutPasswordEx ex = new GroupLayoutPasswordEx(); 
      ex.setVisible(true); 
     }); 
    } 
} 

一旦你投入了一些時間到這位經理身上,這並不困難。 下面是截圖:

Login