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 jav
2、ax.swing.border.EmptyBorder; 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. */
3、public static void main(String[] args) { try { UIManager .setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); } catch (Throwable e) { e.printStackTrace(); } EventQueue.invokeLater(new Runnable() {
4、 public void run() { try { ShowEmptyBorder frame = new ShowEmptyBorder(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } });
5、} /** * Create the frame. */ public ShowEmptyBorder() { setTitle("力天教育——实现按钮控件的留白"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 328, 336); contentPane = new JPanel(); contentPane.setBorder(new EmptyBord
6、er(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JButton button = new JButton("演示按钮"); button.setBorder(new EmptyBorder(40, 0, 0, 0));// 顶部留白:40pix button.setBounds(19, 106, 109, 64); contentPane.add(button);
7、 JButton button_1 = new JButton("演示按钮"); button_1.setBorder(new EmptyBorder(0, 40, 0, 0));// 左侧留白:40pix button_1.setBounds(177, 14, 109, 64); contentPane.add(button_1); JButton button_2 = new JButton("演示按钮"); button_2.setBorder(
8、new EmptyBorder(0, 0, 0, 40));// 右侧留白:40pix button_2.setBounds(19, 14, 109, 64); contentPane.add(button_2); JButton button_3 = new JButton("演示按钮"); button_3.setBorder(new EmptyBorder(0, 0, 40, 0));// 底部留白:40pix button_3.setBounds(177, 106, 109,
9、64); contentPane.add(button_3); JButton button_4 = new JButton("演示按钮"); button_4.setBorder(new EmptyBorder(0, 0, 40, 40));// 右侧和底部留白:40pix button_4.setBounds(19, 201, 109, 64); contentPane.add(button_4); JButton button_5 = new
10、 JButton("演示按钮"); button_5.setBorder(new EmptyBorder(40, 40, 0, 0));// 左侧和顶部留白:40pix button_5.setBounds(177, 198, 109, 64); contentPane.add(button_5); JLabel lblpix = new JLabel("右则留白40pix"); lblpix.setHorizontalAlignment(SwingConstants.CENTER);
11、 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, 18); contentPane.add(lb
12、lpix_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"
13、); lblpix_1.setHorizontalAlignment(SwingConstants.CENTER); lblpix_1.setBounds(177, 76, 109, 18); contentPane.add(lblpix_1); JLabel lblpix_2 = new JLabel("顶部留白40pix"); lblpix_2.setHorizontalAlignment(SwingConstants.CENTER); lblpix_2.setB
14、ounds(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:1043111352 中南大学自考(株洲报名中心) 选择我们让你成功! 融入中南大学实现人生理想!0731-28103311 13367410111 Q Q:2548462099






