標籤: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 表單背景圖+設定按鈕樣式