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); // 消除内容域,即中间的那一块

      

  • 相关阅读:
    [C++] split string by string
    工作三个月心得经验
    Ubuntu Command-Line: Enable Unlimited Scrolling in the Terminal
    What is the PPA and How to do with it ?
    WCF vs ASMX WebService
    The ShortCuts in the ADT (to be continued)
    when does the View.ondraw method get called
    Browsing Storage Resources with Server Explorer
    Get start with Android development
    C++ Frequently asking question
  • 原文地址:https://www.cnblogs.com/xulf/p/4252863.html
Copyright © 2011-2022 走看看