Java – how to prevent borderlayout east from embracing one side of the screen?

If I add a component like jbuttons in the east or west, how can I prevent it from being on one side of the screen? I want some space between JButton and the edge of the screen

Solution

Call setborder on your JButton as follows:

setBorder( new EmptyBorder( 3,3,3 ) )
import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.border.EmptyBorder;

public class ALineBorder {

    public static void main(String args[]) {
        JFrame frame = new JFrame("Line Borders");
        frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        JButton button1 = new JButton("Button1");
        button1.setBorder( new EmptyBorder( 8,8,8 ) );
        JButton button2 = new JButton("Button2");
        JButton button3 = new JButton("Button3");
        button3.setBorder( new EmptyBorder( 16,16,16 ) );
        Container contentPane = frame.getContentPane();
        contentPane.add(button1,BorderLayout.WEST);
        contentPane.add(button2,BorderLayout.CENTER);
        contentPane.add(button3,BorderLayout.EAST);
        frame.pack();
        frame.setSize(300,frame.getHeight());
        frame.setVisible(true);
    }

}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>