简体   繁体   English

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

[英]How can I remove space between label and textfield

I want output as shown in image 1 but I am getting output as shown in image 2 from my code kindly show where I am wrong. 我想要的输出如图1所示,但是我从代码中获取的输出如图2所示,请善意显示我的错误所在。 How can I remove the space between jLabel1 and jTextfield1 and jLabel2 and jPasswordField1 ? 如何删除jLabel1jTextfield1jLabel2jPasswordField1之间的空间?

Below the image code is posted kindly point the mistake as I am learning how to use GridBagConstraints . 在我正在学习如何使用GridBagConstraints下,在图像代码下方张贴了请指出错误。

Img1 图1

在此处输入图片说明

Img2 图2

在此处输入图片说明

Here is the code 这是代码

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

I would set no absolute sizes. 我不会设置绝对大小。 Since you want an empty border around the login fields, I'd do that by nesting JPanels. 由于您要在登录字段周围留空边框,因此可以通过嵌套JPanels来实现。 I also like creating a utility method (which should be static I suppose since it does not change class state) to help me create my GridBagConstraints, something like... 我还喜欢创建一个实用程序方法(我想应该是静态的,因为它不会更改类状态)来帮助我创建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();
         }
      });
   }
}

Use this in your constructor. 在构造函数中使用它。 If you need to know more about spacing, look into Insets from 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);

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM