【问题标题】:Laying out JLabels and JTextFields布置 JLabels 和 JTextFields
【发布时间】:2015-05-15 21:18:22
【问题描述】:

当我运行这个程序时,两个文本字段紧挨着出现;如果可能的话,我怎样才能到达密码字段在用户名字段下的位置?

public static void main(String[] args) {
    JTextField username = new JTextField(10);
    JPasswordField password = new JPasswordField(10);

    final JPanel login = new JPanel();

    login.add(new JLabel("Username:"));
    login.add(username);
    login.add(Box.createHorizontalStrut(50)); 
    login.add(new JLabel("Password:"));
    login.add(password);

    int result = JOptionPane.showConfirmDialog(null, login, 
       "Login Form", JOptionPane.OK_CANCEL_OPTION);

    if (result == JOptionPane.OK_OPTION) {
        System.out.println("Username: " + username.getText());
        System.out.println("Password: " + password.getText());
    }
}

【问题讨论】:

  • 了解和使用布局管理器,例如我在 this answer 中使用的 GridBagLayout。

标签: java swing jpanel jtextfield layout-manager


【解决方案1】:

我自己,我会使用 GridBagLayout,因为使用此布局,您可以指定要在网格中放置组件的位置、它们如何填充网格单元格、当 GUI 扩展时它们如何扩展等。如何使用它在GridBagLayout tutorial 和其他教程和examples on this site 中有很好的解释。

例如:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.*;

public class SimpleLogin {
   public static void main(String[] args) {
      SimpleLoginPanel simpleLoginPanel = new SimpleLoginPanel();
      int result = JOptionPane.showConfirmDialog(null, simpleLoginPanel,
            "Login Form", JOptionPane.OK_CANCEL_OPTION);

      if (result == JOptionPane.OK_OPTION) {
         System.out.println("Username: " + simpleLoginPanel.getUserName());

         // the code below is dangerous as it translates a char[] to a String
         // making the password potentially discoverable by outside programs.
         System.out.println("Password: " + new String(simpleLoginPanel.getPassword()));

      }
   }
}

// create a JPanel to hold our components
@SuppressWarnings("serial")
class SimpleLoginPanel extends JPanel {
   // insets provide blank space around the gridbag layout cells
   private static final Insets INSETS = new Insets(5, 5, 5, 5);
   private JTextField username = new JTextField(10);
   private JPasswordField password = new JPasswordField(10);

   public SimpleLoginPanel() {
      setLayout(new GridBagLayout());
      add(new JLabel("User Name:"), createGbc(0, 0));
      add(username, createGbc(1, 0));
      add(new JLabel("Password:"), createGbc(0, 1));
      add(password, createGbc(1, 1));
   }

   // so that outside classes can extract the data in the password field
   public char[] getPassword() {
      return password.getPassword();
   }

   // so that outside classes can extract the username's text
   public String getUserName() {
      return username.getText();
   }

   // the main method of this example, one that creates the GridBagConstraints
   // depending on the x, y position of the component. 
   // this one is written for a two column grid.
   private GridBagConstraints createGbc(int x, int y) {
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridx = x;
      gbc.gridy = y;
      gbc.gridwidth = 1;
      gbc.gridheight = 1;
      gbc.insets = INSETS;
      if (x == 0) {
         // if the left column, then anchor it to the left
         // and fill completely since it's a JLabel
         gbc.fill = GridBagConstraints.BOTH;
         gbc.anchor = GridBagConstraints.LINE_START;
      } else {
         // if the right column, anchor to the right and 
         // only fill horizontally since its a JTextField.
         gbc.fill = GridBagConstraints.HORIZONTAL;
         gbc.anchor = GridBagConstraints.LINE_END;
      }
      return gbc;
   }

}

【讨论】:

    【解决方案2】:

    你可以尝试使用BoxLayout:

    login.setLayout(new BoxLayout(login, BoxLayout.PAGE_AXIS));
    

    不过,它还在带有文本字段的行中设置了一个标签。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-04
      • 2017-02-10
      相关资源
      最近更新 更多