简体   繁体   中英

how to remove space between jtextfield, jbutton and raisedbevel border

I wants to remove blank space or border space between JButton and JTextfield added inside raisedbevel border I tried this code but still gap between them

import java.awt.*; 
import javax.swing.*;
import javax.swing.border.Border;

public class Main {
  public static void main(String args[]) {
    JFrame f = new JFrame("JPasswordField  ");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel p=new JPanel(),pp=new JPanel();
    JButton b=new JButton("o");

b.setBorder(null);
b.setBorderPainted(false);
b.setMargin(new Insets(0,0,0,0));
 Border emptyBorder = BorderFactory.createEmptyBorder();
    b.setBorder(emptyBorder);
    Border  raisedbevel=BorderFactory.createRaisedBevelBorder();


    pp.setBorder(raisedbevel);
    JTextField t=new JTextField(20);
    t .setBorder(javax.swing.BorderFactory.createEmptyBorder());
    t.setPreferredSize(new Dimension(100, 25));
    b.setPreferredSize(new Dimension(25, 25));
    pp.setBackground(Color.black);
    pp.add(t);

     pp.add(b);


     p.add(pp);
    f.add(p);
    f.setSize(400, 100);
    f.setVisible(true);


  }
}

I wants to remove that black space completely Thanks.

JPanel use a FlowLayout by default, which uses a padding of 5 , vertically and horitzontially, by default.

If you want to continue using FlowLayout , you could specify the padding you want using something like...

pp = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0);

or use another layout manager, like GridBagLayout

I'd also discourage you from using setPreferredSize generally and rely on Border 's and layout's to affect how a component might be "extra" sized beyond its default preferred size

Have a look at Laying Out Components Within a Container , How to Use FlowLayout , How to Use GridBagLayout and Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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