Java 表單背景圖+設定按鈕樣式

來源:互聯網
上載者:User

標籤:tle   etc   getc   ini   方法   logs   res   back   .com   

這個方法應該是比較麻煩的做法,因為背景圖是放在表單的第二層,如果表單內容層有很多面板,就得挨個設定透明。話不多說,上代碼:

 1 import java.awt.Color; 2 import java.awt.Dimension; 3 import java.awt.FlowLayout; 4 import java.awt.event.WindowAdapter; 5 import java.awt.event.WindowEvent; 6  7 import javax.swing.ImageIcon; 8 import javax.swing.JButton; 9 import javax.swing.JFrame;10 import javax.swing.JLabel;11 import javax.swing.JPanel;12 13 public class Background extends JFrame {14     15     16     public static void main(String[] args) {17         new Background();18     }19     20     Background() {    21         this.setTitle("Background");22         this.setLayout(new FlowLayout());23         JButton jb = new JButton("測試");24         this.add(jb);25         26         //放置背景圖27         ImageIcon background = new ImageIcon(Background.class.getClassLoader().getResource("images/img.jpg"));28         JLabel bkLabel = new JLabel(background);29         bkLabel.setBounds(0, 0,background.getIconWidth(), background.getIconHeight());30         this.setSize(background.getIconWidth(), background.getIconHeight());31         this.getLayeredPane().add(bkLabel,new Integer(Integer.MIN_VALUE));32         JPanel ctPanel = (JPanel)this.getContentPane();33         ctPanel.setOpaque(false);34         35         //設定按鈕樣式36         jb.setPreferredSize(new Dimension(100,40));//設定大小37         jb.setBackground(new Color(118,238,0));//設定背景色38         jb.setForeground(Color.WHITE);//設定前景色彩39         jb.setFont(new  java.awt.Font("微軟楷體",  1,  20)); //設定字型樣式40         41         this.addWindowListener(new WindowAdapter() {42             public void windowClosing(WindowEvent arg0) {43                 System.exit(0);44             }            45         });46         this.setVisible(true);47     }48 49 }

 

 圖片放置:(建立的Java項目裡的)src > New > Folder > Folder name:images > finish,染回將圖片粘貼到images裡

Java 表單背景圖+設定按鈕樣式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.