JAVA GUI 開發---界面加載背景圖片

窗口加載一個圖片作爲背景,簡單就是這樣簡單。java

import java.awt.Graphics;ide

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;this

public class ImgJframeTest {
 private JFrame jframe;
 private JButton jbutton;
 private JPanel jpanel;
 
 public ImgJframeTest(){
  jframe = new JFrame();
  jbutton = new JButton();
 
  init();
 }
 
 private void init(){
  jpanel = new JPanel(){
   @Override
   protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    ImageIcon img = new ImageIcon(ImgJframeTest.class.getResource("\\img\\2.jpg"));
    img.paintIcon(this, g, 0, 0);
   }
  };
  jbutton.setText("我是個按鈕,嘿嘿!");
  jpanel.add(jbutton);
  jpanel.setOpaque(true);
  jframe.add(jpanel);
 }
 
 public void showMe(){
  jframe.setSize(600, 400);
  jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  jframe.setVisible(true);
 }圖片

 public static void main(String[] args) {
   new ImgJframeTest().showMe();
 }
}get

相關文章
相關標籤/搜索