1、实现按钮控件边框留白关键技术:简单的 EmptyBorder 边框EmptyBorder 边框的最简单的应用,就是通过 4 个整型参数指定新建边框的四边留白public EmptyBorder(int top,int left,int botton,int right)top:顶部留白left 左边留白botton 底部留白right 右边留白package llt.swing08;import java.awt.EventQueue;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.border.Em
2、ptyBorder;import javax.swing.JButton;import javax.swing.UIManager;import javax.swing.JLabel;import javax.swing.SwingConstants;public class ShowEmptyBorder extends JFrame private JPanel contentPane;/* Launch the application.*/public static void main(String args) try UIManager.setLookAndFeel(“com.sun.
3、java.swing.plaf.nimbus.NimbusLookAndFeel“); catch (Throwable e) e.printStackTrace();EventQueue.invokeLater(new Runnable() public void run() try ShowEmptyBorder frame = new ShowEmptyBorder();frame.setVisible(true); catch (Exception e) e.printStackTrace(););/* Create the frame.*/public ShowEmptyBorder
4、() setTitle(“力天教育 实现按钮控件的留白“);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 328, 336);contentPane = new JPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5);setContentPane(contentPane);contentPane.setLayout(null);JButton button = new JButton(“演示按钮“);button.setBorder(new Em
5、ptyBorder(40, 0, 0, 0);/ 顶部留白:40pixbutton.setBounds(19, 106, 109, 64);contentPane.add(button);JButton button_1 = new JButton(“演示按钮 “);button_1.setBorder(new EmptyBorder(0, 40, 0, 0);/ 左侧留白:40pixbutton_1.setBounds(177, 14, 109, 64);contentPane.add(button_1);JButton button_2 = new JButton(“演示按钮 “);but
6、ton_2.setBorder(new EmptyBorder(0, 0, 0, 40);/ 右侧留白:40pixbutton_2.setBounds(19, 14, 109, 64);contentPane.add(button_2);JButton button_3 = new JButton(“演示按钮 “);button_3.setBorder(new EmptyBorder(0, 0, 40, 0);/ 底部留白:40pixbutton_3.setBounds(177, 106, 109, 64);contentPane.add(button_3);JButton button_4
7、= new JButton(“演示按钮 “);button_4.setBorder(new EmptyBorder(0, 0, 40, 40);/ 右侧和底部留白:40pixbutton_4.setBounds(19, 201, 109, 64);contentPane.add(button_4);JButton button_5 = new JButton(“演示按钮 “);button_5.setBorder(new EmptyBorder(40, 40, 0, 0);/ 左侧和顶部留白:40pixbutton_5.setBounds(177, 198, 109, 64);contentP
8、ane.add(button_5);JLabel lblpix = new JLabel(“右则留白 40pix“);lblpix.setHorizontalAlignment(SwingConstants.CENTER);lblpix.setBounds(19, 76, 109, 18);contentPane.add(lblpix);JLabel lblpix_4 = new JLabel(“右则和底部留白40pix“);lblpix_4.setHorizontalAlignment(SwingConstants.CENTER);lblpix_4.setBounds(6, 263, 134
9、, 18);contentPane.add(lblpix_4);JLabel lblpix_5 = new JLabel(“左则和底部留白40pix“);lblpix_5.setHorizontalAlignment(SwingConstants.CENTER);lblpix_5.setBounds(161, 263, 145, 18);contentPane.add(lblpix_5);JLabel lblpix_1 = new JLabel(“左则留白40pix“);lblpix_1.setHorizontalAlignment(SwingConstants.CENTER);lblpix_
10、1.setBounds(177, 76, 109, 18);contentPane.add(lblpix_1);JLabel lblpix_2 = new JLabel(“顶部留白40pix“);lblpix_2.setHorizontalAlignment(SwingConstants.CENTER);lblpix_2.setBounds(19, 171, 109, 18);contentPane.add(lblpix_2);JLabel lblpix_3 = new JLabel(“底部留白40pix“);lblpix_3.setHorizontalAlignment(SwingConstants.CENTER);lblpix_3.setBounds(177, 171, 109, 18);contentPane.add(lblpix_3);株洲科技 IT部 0731-28103311 13367410111 技术交流 QQ:1043111352http:/ 中南大学自考(株洲报名中心)选择我们让你成功!融入中南大学实现人生理想!0731-28103311 13367410111Q Q:2548462099