簡體   English   中英

Java樣式-間距

[英]Java Styling - Spacing

因此,我是第一次學習Java,現在是我嘗試第一個項目的時候了。 我被困在“第一關”哈哈。

我遇到的問題是我實際上並不知道如何將J Items分開。

我有一個250,350窗口,用於使用JLabel的Log in表單,底部是JButton用戶名的JTextField和用於密碼的JLabel JPassword

我現在想做的是設置樣式,以使表單的頂部和底部之間的間距使表單居中,並在JLabelJTextField之間添加線條的高度空間。 (基本上是\\ n類型交易,但不起作用。)

希望這是有道理的,如果沒有的話,我表示歉意,我將嘗試改寫/添加代碼!

public Game() {
    this.setSize(250,350);
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("Sticket Cricket - Login");

    JPanel loginMenuPanel = new JPanel();

    loginButton = new JButton("Login");

    usernameField = new JTextField();
    usernameField.setColumns(10);

    passwordField = new JPasswordField();
    passwordField.setColumns(10);
    passwordField.requestFocus();

    usernameLabel = new JLabel("Username: ");

    passwordLabel = new JLabel("Password: ");

    this.add(loginMenuPanel);

    loginMenuPanel.add(usernameLabel);
    loginMenuPanel.add(usernameField);
    loginMenuPanel.add(passwordLabel);
    loginMenuPanel.add(passwordField);
    loginMenuPanel.add(loginButton);

    this.setVisible(true);
}

簡短答案:

創建一個JPanel,設置面板的布局 (一些示例GridLayoutBorderLayout ,在此處查看教程,其中對其中的更多內容進行了說明)

然后將您的組件相應地添加到此面板中

對於您正在尋找的布局,使用IDE來創建它可能會更容易,我發現Net Beans最容易做到這一點。

我的建議是為您創建一個具有2列2行的網格布局的JPanel,為此您將JLabels和Text字段添加為登錄名和密碼。

然后創建另一個JPanel(可能是BorderLayout或Flow Layout),並將上面的面板添加到該面板,然后將此父面板添加到框架。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM