组件布局学习/方法的参数中含有接口

  • 子类调用父类方法,方法参数为接口的使用例子:
    jpanel.setLayout(new GridLayout(0, 2, 5, 10));//调用JPanel从类 Container中继承的方法public void setLayout(LayoutManager mgr)( setLayout功能:设置此容器的布局管理器。)
    LayoutManager是接口 :public interface LayoutManager(功能:定义知道如何布置 Container 类的接口。)
    LayoutManager接口中有已实现类,其中包含类 GridLayout(public class GridLayout)
  • 组件上也可以添加组件
  • 接口中除了已实现类,另请参见也要看,如下图这里写图片描述
JButton p1 = new JButton();        p1.setBorder(BorderFactory.createLineBorder(Color.red, 3));//此句等同于p1.setBorder(new LineBorder(Color.red, 3));
p1.add(new JLabel("线边框"));//**组件上也可以添加组件**
panel.add(p1);

  • 例子代码
public class CopyOfMainInterface {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Swing边框演示");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(0, 2, 5, 10));
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        JButton p1 = new JButton();
        JButton p11 =new JButton();
        p1.setBorder(BorderFactory.createLineBorder(Color.red, 3));
        p1.setLayout(new GridLayout(2 , 2, 1,1));
        p1.add(new JLabel("线边框"));
        p1.add(p11);
        panel.add(p1);

        JPanel p2 = new JPanel();
        p2.setBorder(BorderFactory.createEtchedBorder());
        p2.add(new JLabel("蚀刻边框"));
        panel.add(p2);

        JPanel p3 = new JPanel();
        p3.setBorder(BorderFactory.createRaisedBevelBorder());
        p3.add(new JLabel("斜面边框(凸)"));
        panel.add(p3);

        JPanel p4 = new JPanel();
        p4.setBorder(BorderFactory.createLoweredBevelBorder());
        p4.add(new JLabel("斜面边框(凹)"));
        panel.add(p4);

        JPanel p5 = new JPanel();
        p5.setBorder(BorderFactory.createTitledBorder("标题"));
        p5.add(new JLabel("标题边框"));
        panel.add(p5);

        JPanel p6 = new JPanel();
        TitledBorder tb = BorderFactory.createTitledBorder("标题");
        tb.setTitleJustification(TitledBorder.RIGHT);
        p6.setBorder(tb);
        p6.add(new JLabel("标签边框(右)"));
        panel.add(p6);

        JPanel p7 = new JPanel();
        p7.setBorder(BorderFactory.createMatteBorder(1, 5, 1, 1, Color.yellow));
        p7.add(new JLabel("花色边框"));
        panel.add(p7);

        JPanel p8 = new JPanel();
        javax.swing.border.Border b1 = BorderFactory.createLineBorder(Color.blue, 2);
        javax.swing.border.Border b2 = BorderFactory.createEtchedBorder();
        p8.setBorder(BorderFactory.createCompoundBorder(b1, b2));
        p8.add(new JLabel("组合边框"));
        panel.add(p8);

        JScrollPane scrollPane = new JScrollPane(panel);
        scrollPane.setPreferredSize(new Dimension(300, 300));
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setVisible(true);
    }
}

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值