Ⅰ java swing如果做一個背景圖片。如何在圖片上顯示控制項
如果是背景圖片的話,
重寫paintComponent(Graphics g)這個方法,可以生成
protected void paintComponent(Graphics g) {
g.drawImage(圖片對象.getImage(), 0, 0, this);
super.paintComponent(g);
}
.
如果還在要在圖片上顯示控制項,可以考慮JLayeredPane
在這個上面分層放控制項。
用label放一張圖片,與窗體大小一樣。放在最底層,控制項放在上面一層。
Ⅱ Java Swing 怎麼自定義界面背景圖片
在java swing中需要為容器添加自定義圖片或者背景圖片。提兄大供兩種簡單的解棗塵氏決方案,一種利用JPanel,另一種利用JLabel,代碼如下:
1、JPanel(源代碼)
packageoo;
importjava.awt.Graphics;
importjava.awt.Image;
importjava.io.File;
importjavax.swing.ImageIcon;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
publicclassDrawing{
JFramejframe=newJFrame();
publicstaticJPanelGImage=null;
publicDrawing(){
initFrame();
}
//初始化窗口
publicvoidinitFrame(){
//利用JPanel添加背景圖片
GImage=newJPanel(){
protectedvoidpaintComponent(Graphicsg){
ImageIconicon=newImageIcon("image\benbenla.jpg");
Imageimg=icon.getImage();
g.drawImage(img,0,0,icon.getIconWidth(),
icon.getIconHeight(),icon.getImageObserver());
jframe.setSize(icon.getIconWidth(),icon.getIconHeight());
}
};
jframe.setTitle("測試背景圖片");
jframe.add(GImage);
jframe.pack();
jframe.setVisible(true);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
publicstaticvoidmain(String[]args){
newDrawing();
}
}
2、JLabel源代碼
packageswing.draw;
importjava.awt.Image;
importjavax.swing.ImageIcon;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
/**利用JLabel來構建圖片*/
publicclassDrawing2{
JLabeljlpic=newJLabel();
JFramejframe=newJFrame();
publicDrawing2(){
init1Frame();
}
publicvoidinit1Frame(){
ImageIconicon=newImageIcon("image\benbenla.jpg");
凳散icon.setImage(icon.getImage().getScaledInstance(icon.getIconWidth(),
icon.getIconHeight(),Image.SCALE_DEFAULT));
System.out.println(icon.getIconHeight()+""+icon.getIconWidth());
jlpic.setBounds(0,0,1366,768);
jlpic.setHorizontalAlignment(0);
jlpic.setIcon(icon);
jframe.setSize(1366,768);
jframe.add(jlpic);
jframe.pack();
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
}
publicstaticvoidmain(Stringargs[]){
newDrawing2();
}
}
添加控制項:
jlpic.setIcon(icon);
Containerc=newContainer();
JLabeluser=newJLabel("用戶:");
JLabelpassword=newJLabel("密碼:");
JTextFieldtxf_userName=newJTextField();
JPasswordFieldpwd_password=newJPasswordField()
{
publicvoidpaste(){
UIManager.getLookAndFeel().provideErrorFeedback(this);
}
};
user.setBounds(200,25,50,25);
password.setBounds(200,52,50,25);
txf_userName.setBounds(300,25,150,25);
pwd_password.setBounds(300,52,150,25);
c.add(user);
c.add(txf_userName);
c.add(password);
c.add(pwd_password);
c.add(jlpic);
jframe.setSize(1366,768);
//jframe.add();
jframe.add(c);
Ⅲ JAVA代碼添加背景圖片。更改背景顏色!awt或swing!很簡單!越簡單越好!
設置顏色:
把pan-pan6的Background設為Null,這樣他們就變為透明的了,以後只需要改變inquestFrame的顏色就可以改變全部的顏色,代碼如下:
//設置整體布局
JPanelpan=newJPanel();
pan.setLayout(newGridLayout(6,1));
pan.add(pan1);
pan.add(pan2);
pan.add(pan3);
pan.add(pan4);
pan.add(pan5);
pan.add(pan6);
pan.setBackground(null);
pan1.setBackground(null);
pan2.setBackground(null);
pan3.setBackground(null);
pan4.setBackground(null);
pan5.setBackground(null);
pan6.setBackground(null);
r1.setBackground(null);
r2.setBackground(null);
inquestFrame.setBackground(Color.LIGHT_GRAY);
inquestFrame.setContentPane(pan);
inquestFrame.setSize(300,250);
inquestFrame.setVisible(true);
設置圖片需要改的比較多,自己比較那不一樣吧:
importjava.awt.Graphics;
importjava.awt.GridLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.net.URL;
importjavax.swing.ButtonGroup;
importjavax.swing.ImageIcon;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
importjavax.swing.JRadioButton;
importjavax.swing.JTextField;
publicclassInquest{
JFrameinquestFrame;
JLabellab1,lab2,lab3,lab4,lab5,lab6;
JTextFieldtext1,text2,text4,text5,text6;
JRadioButtonr1,r2;
JButtoninqButton;
publicInquest(){
inquestFrame=newJFrame("教育經歷管理系統-查詢窗口");
MyPanelpan1=newMyPanel();
lab1=newJLabel("請輸入編碼:");
text1=newJTextField(10);
inqButton=newJButton("查詢");
inqButton.addActionListener(newButtonHandler());
pan1.add(lab1);
pan1.add(text1);
pan1.add(inqButton);
MyPanelpan2=newMyPanel();
lab2=newJLabel("姓名:");
text2=newJTextField(10);
pan2.add(lab2);
pan2.add(text2);
MyPanelpan3=newMyPanel();
lab3=newJLabel("性別:");
r1=newJRadioButton("男");
r2=newJRadioButton("女");
//把兩個單選按鈕放到一個群組中,保持二選一
ButtonGroupgroup=newButtonGroup();
group.add(r1);
group.add(r2);
pan3.add(lab3);
pan3.add(r1);
pan3.add(r2);
MyPanelpan4=newMyPanel();
lab4=newJLabel("單位:");
text4=newJTextField(10);
pan4.add(lab4);
pan4.add(text4);
MyPanelpan5=newMyPanel();
lab5=newJLabel("職位:");
text5=newJTextField(10);
pan5.add(lab5);
pan5.add(text5);
MyPanelpan6=newMyPanel();
lab6=newJLabel("年齡:");
text6=newJTextField(10);
pan6.add(lab6);
pan6.add(text6);
//設置整體布局
MyPanelpan=newMyPanel();
pan.setLayout(newGridLayout(6,1));
pan.add(pan1);
pan.add(pan2);
pan.add(pan3);
pan.add(pan4);
pan.add(pan5);
pan.add(pan6);
pan.setBackground(null);
pan1.setBackground(null);
pan2.setBackground(null);
pan3.setBackground(null);
pan4.setBackground(null);
pan5.setBackground(null);
pan6.setBackground(null);
r1.setBackground(null);
r2.setBackground(null);
MyPanelpan0=newMyPanel();
pan0.setPreferredSize(pan.getPreferredSize());
pan0.setLayout(null);
pan.setBounds(0,0,pan.getPreferredSize().width,pan.getPreferredSize().height);
URLurl=Inquest.class.getResource("Winter.jpg");
ImageIconimg=newImageIcon(url);
JLabellab=newJLabel(img);
lab.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
pan0.add(pan);
pan0.add(lab);
inquestFrame.setContentPane(pan0);
inquestFrame.pack();
inquestFrame.setVisible(true);
}
{
publicvoidpaint(Graphicsg){
paintChildren(g);
}
}
publicvoidsetBack(){
((JPanel)inquestFrame.getContentPane()).setOpaque(false);
//Winter.jpg這個圖片的位置要跟當前這個類是同一個包下
URLurl=Inquest.class.getResource("Winter.jpg");
ImageIconimg=newImageIcon(url);
JLabelbackground=newJLabel(img);
inquestFrame.getLayeredPane().add(background,newInteger(Integer.MIN_VALUE));
background.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
}
{
publicvoidactionPerformed(ActionEvente){
//生成SQL語句
Strings="select*frominformationwhereid="+"'"+text1.getText()+"'";
Stringdriver="sun.jdbc.odbc.JdbcOdbcDriver";
Stringurl="jdbc:odbc:test";
//DBdb=newDB(driver,url,"sa","");
//ResultSetrs=db.query(s);
//try{
//while(rs.next()){
//text2.setText(rs.getString(2));
//Stringsex=rs.getString(3);
//if(sex.trim().equals("男"))
//r1.setSelected(true);
//else
//r2.setSelected(true);
//
//text4.setText(rs.getString(4));
//text5.setText(rs.getString(5));
//text6.setText(rs.getString(6));
//}
//}catch(Exceptione2){
//e2.printStackTrace();
//}
}
}
publicstaticvoidmain(Stringargs[]){
newInquest();
}
}