如何删除标签和文本字段之间的空格

时间:2014-11-05 21:33:57

标签: java swing layout-manager gridbaglayout

我想要输出如图1所示,但我得到的输出如图2所示从我的代码中显示我错了。如何删除jLabel1jTextfield1以及jLabel2jPasswordField1之间的空格?

在图片代码下方发布,请指出错误,因为我正在学习如何使用GridBagConstraints

IMG1

enter image description here

IMG2

enter image description here

这是代码

public login()
    {
        jPanelC=new JPanel();
        jlabel1=new JLabel("User ID");
        jlabel2=new JLabel("Password");
        jlabel3=new JLabel("Password");
        jid=new JTextField(15);
        jid.setPreferredSize(new Dimension(100, 100));
        jpass=new JPasswordField(15);
        jpass.setPreferredSize(new Dimension(100, 100));
        jbutton1=new JButton("Login");
        jbutton1.setPreferredSize(new Dimension(100, 100));

        jPanelC.setBackground(new java.awt.Color(204, 204, 255));
        jPanelC.setBorder(empty);
        int eb=5;
        jPanelC.setBorder(BorderFactory.createCompoundBorder(
        BorderFactory.createEmptyBorder(eb, eb, eb, eb), // outer border
        BorderFactory.createLoweredBevelBorder()));      // inner border
        jPanelC.setSize(300, 150);

        jPanelC.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        jPanelC.add(jlabel1,c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx =1;
        c.gridy = 0;
        c.weightx=2;
        jPanelC.add(jid,c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 1;
        jPanelC.add(jlabel2,c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 1;
        jPanelC.add(jpass,c);

        c.fill = GridBagConstraints.HORIZONTAL; 
        c.gridx = 0;
        c.gridy = 2;      
        c.gridheight=1;
        c.gridwidth = 3;

       jPanelC.add(jbutton1,c);

        jPanelE=new JPanel();
        jPanelW=new JPanel();
        jPanelN=new JPanel();
        jPanelS=new JPanel();
        add(jPanelE,BorderLayout.EAST);
        add(jPanelW,BorderLayout.WEST);
        add(jPanelN,BorderLayout.NORTH);
        add(jPanelS,BorderLayout.SOUTH);       
        add(jPanelC,BorderLayout.CENTER);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Login Console");
        setBackground(new java.awt.Color(255, 255, 255));
        setSize(400, 300);
        this.setVisible(true);       
    }
    public static void main(String[] args)
    {
        login log=new login();
    }
}

2 个答案:

答案 0 :(得分:3)

我不会设置绝对大小。由于你想在登录字段周围有一个空边框,我会通过嵌套JPanel来做到这一点。我也喜欢创建一个实用工具方法(我认为它应该是静态的,因为它不会改变类状态)来帮助我创建我的GridBagConstraints,类似于......

import java.awt.Dialog.ModalityType;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.*;

public class Login2 extends JPanel {
   private static final String TITLE = "Login For Authorized User";
   private static final int COLS = 15;
   private static final int PREF_W = 300;
   private static final int PREF_H = 200;
   private JTextField field1 = new JTextField();
   private JPasswordField passField = new JPasswordField();

   public Login2() {
      field1.setColumns(COLS);
      passField.setColumns(COLS);

      JPanel inPanel = new JPanel(new GridBagLayout());
      JLabel titleLabel = new JLabel(TITLE, SwingConstants.CENTER);
      titleLabel.setFont(titleLabel.getFont().deriveFont(Font.BOLD));
      inPanel.add(titleLabel, createGbc(0, 0, 2, 1));
      inPanel.add(new JLabel(" "), createGbc(0, 1, 2, 1));
      inPanel.add(new JLabel("User ID:"), createGbc(0, 2, 1, 1));
      inPanel.add(field1, createGbc(1, 2, 1, 1));
      inPanel.add(new JLabel("Password:"), createGbc(0, 3, 1, 1));
      inPanel.add(passField, createGbc(1, 3, 1, 1));
      inPanel.add(new JLabel(" "), createGbc(0, 4, 1, 1));
      inPanel.add(new JButton("Login"), createGbc(1, 4, 1, 1));

      int ebT = 15;
      int ebL = 15;
      int ebB = 15;
      int ebR = 15;
      inPanel.setBorder(BorderFactory.createEmptyBorder(ebT, ebL, ebB, ebR));
      add(inPanel);
   }


   private static GridBagConstraints createGbc(int x, int y, int w, int h) {
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridx = x;
      gbc.gridy = y;
      gbc.gridwidth = w;
      gbc.gridheight = h;
      gbc.weightx = 1.0;
      gbc.weighty = 1.0;

      if (w == 1 && x == 0) {
         gbc.insets = new Insets(5, 5, 5, 10);
      } else {
         gbc.insets = new Insets(5, 5, 5, 5);
      }
      gbc.fill = GridBagConstraints.HORIZONTAL;
      return gbc;
   }

   private static void createAndShowGui() {
      Login2 loginPanel = new Login2();

      JFrame frame = new JFrame("Main GUI");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      // frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      // frame.setVisible(true);

      JDialog dialog = new JDialog(frame, "Login", ModalityType.APPLICATION_MODAL);
      dialog.add(loginPanel);
      dialog.setResizable(false);
      dialog.pack();
      dialog.setLocationByPlatform(true);
      dialog.setVisible(true);

      frame.dispose();
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

答案 1 :(得分:2)

在构造函数中使用它。如果您需要了解有关间距的更多信息,请查看here

中的Insets
    jPanelC = new JPanel();
    jlabel1 = new JLabel("User ID");
    jlabel2 = new JLabel("Password");
    jid = new JTextField(15);
    // jid.setPreferredSize(new Dimension(100, 100));
    jpass = new JPasswordField(15);
    // jpass.setPreferredSize(new Dimension(100, 100));
    jbutton1 = new JButton("Login");
    // jbutton1.setPreferredSize(new Dimension(100, 100));

    jPanelC.setBackground(new java.awt.Color(204, 204, 255));
    jPanelC.setBorder(empty);
    int eb = 5;
    jPanelC.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb), // outer
                                                                                                            // border
            BorderFactory.createLoweredBevelBorder())); // inner border
    jPanelC.setSize(300, 150);

    jPanelC.setLayout(new GridBagLayout());

    panelU = new JPanel(new GridBagLayout());
    panelU.setBackground(new java.awt.Color(204, 204, 255));

    GridBagConstraints c = new GridBagConstraints();

    c.fill = GridBagConstraints.NONE;
    c.anchor = GridBagConstraints.CENTER;
    c.gridx = 0;
    c.gridy = 0;
    c.weightx = 1.0;
    c.weighty = 1.0;
    c.insets = new Insets(5, 5, 5, 5);
    panelU.add(jlabel1, c);

    c.fill = GridBagConstraints.NONE;
    c.anchor = GridBagConstraints.CENTER;
    c.gridx = 2;
    c.gridy = 0;
    c.weightx = 1.0;
    c.weighty = 1.0;
    c.insets = new Insets(5, 5, 5, 5);
    panelU.add(jid, c);

    c.fill = GridBagConstraints.NONE;
    c.anchor = GridBagConstraints.CENTER;
    c.gridx = 0;
    c.gridy = 1;
    c.weightx = 1.0;
    c.weighty = 1.0;
    c.insets = new Insets(5, 5, 5, 5);
    panelU.add(jlabel2, c);

    c.fill = GridBagConstraints.NONE;
    c.anchor = GridBagConstraints.CENTER;
    c.gridx = 2;
    c.gridy = 1;
    c.weightx = 1.0;
    c.weighty = 1.0;
    c.insets = new Insets(5, 5, 5, 5);
    panelU.add(jpass, c);

    c.fill = GridBagConstraints.CENTER;
    c.anchor = GridBagConstraints.CENTER;
    c.gridx = 0;
    c.gridy = 2;
    c.insets = new Insets(5, 5, 5, 5);
    c.gridheight = 1;
    c.gridwidth = 2;

    GridBagConstraints c1 = new GridBagConstraints();

    c1.gridx = 0;
    c1.gridy = 0;
    c1.gridwidth = 1;
    c1.gridheight = 1;
    c1.weightx = 0.0;
    c1.weighty = 0.0;
    c1.insets = new Insets(5, 5, 5, 5);
    c1.fill = GridBagConstraints.NONE;
    c1.anchor = GridBagConstraints.CENTER;

    jPanelC.add(panelU, c1);

    c1.gridx = 0;
    c1.gridy = 1;
    c1.gridwidth = 1;
    c1.gridheight = 1;
    c1.weightx = 0.0;
    c1.weighty = 0.0;
    c1.insets = new Insets(5, 5, 5, 5);
    c1.fill = GridBagConstraints.NONE;
    c1.fill = GridBagConstraints.CENTER;

    jPanelC.add(jbutton1, c1);

    jPanelE = new JPanel();
    jPanelW = new JPanel();
    jPanelN = new JPanel();
    jPanelS = new JPanel();
    add(jPanelE, BorderLayout.EAST);
    add(jPanelW, BorderLayout.WEST);
    add(jPanelN, BorderLayout.NORTH);
    add(jPanelS, BorderLayout.SOUTH);
    add(jPanelC, BorderLayout.CENTER);

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Login Console");
    setBackground(new java.awt.Color(255, 255, 255));
    setSize(400, 300);
    this.setVisible(true);

enter image description here