布置JLabel和JTextFields

时间:2015-05-15 21:18:22

标签: java swing jpanel jtextfield layout-manager

当我运行此程序时,两个文本字段紧挨着显示;如果可能的话,我怎样才能将密码字段放在用户名字段下?

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());
    }
}

2 个答案:

答案 0 :(得分:2)

我自己,我使用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;
   }

}

答案 1 :(得分:0)

您可以尝试使用BoxLayout:

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

但它也会在文本字段的行中设置标签。