zoukankan      html  css  js  c++  java
  • Java——JFrame与JButton添加背景

      某些时候,我们会认为Swing的GUI实在是太难看了。我们可以通过修改背景,来达到一定的美化效果。

      

      一、JFrame设置背景

        在设置背景前,必须了解到JFrame分为4层,从下至上分别为RootPane、LayeredPane、ContentPane、GlassPane。其中GlassPane是默认透明的,ContentPane主要用来承载控件。

        我们设置背景,可以选择设置在RootPane或LayeredPane,这样就不会影响到ContentPane。

        以下以LayeredPane为例:

          1. 创建图像:

        ImageIcon icon = new ImageIcon("./img/mainFrame.png"); 
        Image img = icon.getImage().getScaledInstance(width, height, Image.SCALE_FAST); // 图像缩放为适合Frame大小
        JLabel jlabel = new JLabel(new ImageIcon(img));
        jlabel.setBounds(0, 0, width, height);

          

          2.将图像放置在LayeredPane中

        f.getLayeredPane().add(jlabel, new Integer(Integer.MIN_VALUE));

          LayeredPane也是分层的,根据整数大小有若干层。这里将JLabel放置在了LayeredPane最底层。

          3.将ContentPane和RootPane设置透明(可以消去边框)

           JPanel jp = (JPanel) f.getContentPane();
           JRootPane jp1 = (JRootPane) f.getRootPane();
           jp.setOpaque(false);
           jp1.setOpaque(false);

          4. 去除标题栏 : f.setUndecorated(true);

      二、JButton设置背景

          JButton同样用setIcon

            btn.setBorderPainted(false); // 消除边框
            btn.setContentAreaFilled(false); // 消除内容域,即中间的那一块

      

  • 相关阅读:
    php public,static,private,protected,final,const,abstract
    Thinkphp5 iis环境下安装报错400 500
    php 获取某文件内容
    stdClass object 数据获取方法
    php把数组、字符串 生成文件
    Thinkphp5 runtime路径设置data
    php脚本超时 结束执行代码
    bootstrapValidator 表单验证
    thinkphp 外部js语言包
    新浪微博UWP版-实现‘分享功能’的艰难路
  • 原文地址:https://www.cnblogs.com/xulf/p/4252863.html
Copyright © 2011-2022 走看看