生活随笔
收集整理的這篇文章主要介紹了
java使用教程——组件及事件处理——窗口(设置窗口的颜色和背景)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
用JFrame時,設置背景顏色需使用JFrame.getContentPane().setBackground(Color.red)
Container con=this.getContentPane();//得到內容窗格
con.setBackground(Color.blue);
而使用Frame時則可以直接使用setBackground(Color.red),且需要設置窗體默認關閉事件,否則運行窗口無法正常關閉。
將按鈕添加到面板,再將面板添加到框架中,要通過面板來調用setBackground()方法來設置框架的背景顏色,直接使用myFrame.setBackground(Color.GREEN);是不會起作用的。原因是JFrame一旦創建,其中已包含一個內容面板,此時myFrame.setBackground無論設置成什么顏色,都將被頂層面板所覆蓋。因此,要改變背景顏色,就要改變面板的背景顏色。
import javax.swing.*;
import java.awt.*;
public class win
{public static void main(String args
[]){JFrame window1
=new JFrame("第一個窗口");JFrame window2
=new JFrame("第二個窗口");Container con
=window1
.getContentPane();con
.setBackground(Color.BLACK
);window1
.setBounds(60, 100, 188, 108);window2
.setBounds(260, 100, 188, 108);window1
.setVisible(true);window1
.setResizable(false);window1
.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE
);window2
.setVisible(true);window2
.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE
);}}
給窗口添加背景:(使用電腦上的圖片設置背景)
路徑的書寫:
C:/Users/86156/OneDrive/圖片/手機上的高清圖片/你的名字.png
import java.awt.*;
import javax.swing.*;public class BgTest extends JFrame{public BgTest(){init();this.setTitle("背景圖設置");this.setSize(200, 300);this.setLocation(600,300);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE
);this.setResizable(true);this.setVisible(true);}private void init() {JLabel lbl1
= new JLabel("用戶名:");JLabel lbl2
= new JLabel("密 碼:");JTextField txt
= new JTextField(10);JPasswordField pasw
= new JPasswordField(10);pasw
.setEchoChar('*');JButton btn1
= new JButton("登錄");JButton btn2
= new JButton("取消");ImageIcon img
= new ImageIcon("C:/Users/86156/OneDrive/圖片/手機上的高清圖片/你的名字.png");JLabel imgLabel
= new JLabel(img
);this.getLayeredPane().add(imgLabel
, new Integer(Integer.MIN_VALUE
));imgLabel
.setBounds(0, 0, img
.getIconWidth(), img
.getIconHeight());Container contain
= this.getContentPane();((JPanel) contain
).setOpaque(false); contain
.setLayout(new FlowLayout());contain
.add(lbl1
);contain
.add(txt
);contain
.add(lbl2
);contain
.add(pasw
);contain
.add(btn1
);contain
.add(btn2
);}public static void main(String[] args
) {new BgTest(); }
}
Image直屬java
.awt包,抽象類。
BufferImage為image的直接子類,增加了緩沖功能。
BufferedImage生成的圖片在內存里有一個圖像緩沖區,利用這個緩沖區我們可以很方便的操作這個圖片,通常用來做圖片修改操作如大小變換、圖片變灰、設置圖片透明或不透明等。加載圖片到內存:
BufferedImage image
= ImageIO.read(new FileInputStream(imgPath
));ImageIcon直屬javax
.swing包
類定義:
public class ImageIcon extends Object implements Icon, Serializable, Accessible。
imageicon與image的關系是:imageicon利用image繪制icon。不過,
Image一般尺寸較大,不適合用作icon(大圖片用作icon時只顯示圖片的一部分),需要經過處理:
ImageIcon imageIcon
= new ImageIcon(new File(path)
);
Image image
= imageIcon
.getImage();
image
= image
.getScaledInstance(30,20,Image.SCALE_FAST
);
ImageIcon icon
= new ImageIcon(image
);
窗口的大小和圖片大小一樣
主要思路:
1.把圖片添加到標簽里(把標簽的大小設為和圖片大小相同),把標簽放在分層面板的最底層;
2.把窗口面板設為內容面板并設為透明、流動布局。
3.之后把組件和面板添加到窗口面板就可以;
大白話翻譯:
把JFrame想象成一個桌子,桌子上面有個木板,組件和面板就是木板上的物品,我們就是把桌子上的木板換為玻璃板,然后把背景圖放在玻璃板下面,把物品放在玻璃板上面;
package lfs;import java.awt.FlowLayout;import javax.swing.*;public class ImageDemo {public static void main(String[] args
) {JFrame jf
=new JFrame("背景圖片測試");ImageIcon bg
=new ImageIcon("C:/Users/86156/OneDrive/圖片/手機上的高清圖片/動漫圖片.jpg");JLabel label
=new JLabel(bg
);label
.setSize(bg
.getIconWidth(),bg
.getIconHeight());jf
.getLayeredPane().add(label
,new Integer(Integer.MIN_VALUE
));JPanel pan
=(JPanel)jf
.getContentPane();pan
.setOpaque(false);pan
.setLayout(new FlowLayout());JButton btn
=new JButton("測試按鈕");pan
.add(btn
);jf
.setSize(bg
.getIconWidth(),bg
.getIconHeight());jf
.setLocationRelativeTo(null);jf
.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE
);jf
.setVisible(true);}}
總結
以上是生活随笔為你收集整理的java使用教程——组件及事件处理——窗口(设置窗口的颜色和背景)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。