各位JAVA高手怎样才能为已经设置好的窗体加载背景图片呢?急。。。。
import java.awt.Color;import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
class text1 extends JFrame implements ActionListener{
private static final int defaultCloseOperation = 0;
JMenuBar m;
JPanel p0,p1,p2,p3;
JTextField s1,s2,s3,t1;
JButton b,c;
JLabel c1,c2,c3;
JPasswordField t2;
Box box;
text1(String s){
super(s);
p0=new JPanel();
p1=new JPanel();
Box box=Box.createVerticalBox();
p2=new JPanel();
p3=new JPanel();
c3=new JLabel(" 名 豪 登 录 系 统 "); p0.add(c3);
c1=new JLabel("用户名");p1.add(c1);
///////////////////////////////////////////////////////
t1=new JTextField("",20);
t1.addActionListener(this); p1.add(t1);
c2=new JLabel("密 码"); p2.add(c2);
t2=new JPasswordField("",20);
t2.addActionListener(this);
p2.add(t2);
box.add(p0);
box.add(p1);
box.add(p2);
p3=new JPanel();
b=new JButton("登录");
c=new JButton("取消");
b.addActionListener(this);
c.addActionListener(this);
p3.add(b);
p3.add(c);
box.add(p3);
/////////////////////////////////////////////
///////////////////////////////////////////////////////字体设置区
c1.setFont(new Font("隶书", Font.BOLD, 70));
c2.setFont(new Font("隶书", Font.BOLD, 70));
c3.setFont(new Font("隶书", Font.BOLD, 90));
b.setFont(new Font("隶书", Font.BOLD, 50));
c.setFont(new Font("隶书", Font.BOLD, 50));
c1.setForeground(Color.blue);
c2.setForeground(Color.blue);
c3.setForeground(Color.red);
b.setForeground(Color.black);
c.setForeground(Color.black);
p0.setBackground(Color.cyan);
p1.setBackground(Color.cyan);
p2.setBackground(Color.cyan);
p3.setBackground(Color.cyan);
b.setBackground(Color.YELLOW);
c.setBackground(Color.YELLOW);
///////////////////////////////////////////////////////////
this.add(box);
this.setJMenuBar(m);
this.setBounds(0,0,1400,740);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE );//隐藏窗口
}
public static void main(String [] args){
text1 f=new text1("登录窗体");
}
public void actionPerformed(ActionEvent e) {
if((t1.getText().equals("123"))&&(t2.getText().equals("123"))&&e.getSource()==b)
{ caidan f=new caidan("点菜系统");dispose();}
else {cd1 f=new cd1("温馨提示");}
if(e.getSource()==c){dispose();}}} --------------------编程问答-------------------- --------------------编程问答-------------------- 这都过时好多年了 我劝你别研究了 我早就忘光了 --------------------编程问答-------------------- 重写JFrame的paint(Graphics g){}方法啊
public void paint(Graphics g){
g.drawImage(image,0,0,100,100,null);
}
当中image是你自己弄进来的图片,0,0, 100,100是图片显示的顶点和长宽 --------------------编程问答-------------------- 我试一试,谢谢你哈!! --------------------编程问答-------------------- 请问图片存的路劲是怎样的呢?
final JPanel p0 = new JPanel(){
public void paintComponent(Graphics g) {
ImageIcon icon = new ImageIcon("F://java工具//eclipse//JAVA工作站//CD//图片1.jpg");
Dimension dim=getSize();
g.drawImage(icon.getImage(), 0,0,dim.width, dim.height, null);
}
};
补充:Java , Java相关