如何在一个JFrame上面设置背景图片

林尹 2013-10-23 11:24:07
public class Dome extends javax.swing.JFrame {

JLabel topLabel;
JLabel huiyixinxi,yudingxinxi,yanshi,tiqianjiesu;
JButton exitButton,loginButton;



/** Creates new form Dome */
public Dome() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
//GEN-BEGIN:initComponents
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
this.setBounds(0, 0, 1024, 768);
this.setResizable(false);//
topLabel=new JLabel();
topLabel.setIcon(new ImageIcon("images/mitbg.bmp") );
add(topLabel);
topLabel.setLayout(null);

yudingxinxi = new JLabel("预定信息");
yudingxinxi.setForeground(Color.WHITE);
huiyixinxi = new JLabel("会议信息");
yanshi = new JLabel("会议延时");
tiqianjiesu = new JLabel("提前结束");
exitButton = new JButton("退 出");
loginButton = new JButton("登 录");

ButtonListener bListener = new ButtonListener();
// 按钮注册事件监听器
exitButton.addActionListener(bListener);
loginButton.addActionListener(bListener);

MyKeyListener keylis=new MyKeyListener();

yudingxinxi.setSize(45,20);
yudingxinxi.setLocation(40,280);
yudingxinxi.setIcon(new ImageIcon("images/huiyi.bmp"));
topLabel.add(yudingxinxi);

yanshi.setSize(50,20);
yanshi.setLocation(85,280);
topLabel.add(yanshi);

huiyixinxi.setSize(40,20);
huiyixinxi.setLocation(140,280);
topLabel.add(huiyixinxi);

tiqianjiesu.setSize(50,20);
tiqianjiesu.setLocation(195,280);
topLabel.add(tiqianjiesu);

exitButton.setSize(80,20);
exitButton.setLocation(260,280);
topLabel.add(exitButton);

loginButton.setSize(80,20);
loginButton.setLocation(350,280);
topLabel.add(loginButton);

setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
//setSize(500,350);
setVisible(true);

}// </editor-fold>
//GEN-END:initComponents

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Dome().setVisible(true);
}
});
}
private void checkNamePwd(){


}

private void exitWindow(){
System.exit(0);
}

class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// 获取事件源
Object source = e.getSource();
if(source == exitButton)
{
exitWindow();
}
else if(source == loginButton)
{
checkNamePwd();
}
}
}

class MyKeyListener extends KeyAdapter{
public void keyPressed(KeyEvent ke){
int code=ke.getKeyCode();
if(code==10){
checkNamePwd();
}
else if(code==27){
exitWindow();
}
}


//GEN-BEGIN:variables
// Variables declaration - do not modify
// End of variables declaration//GEN-END:variables
}
}



我这段代码就是不会把图片显示出来 求指点
...全文
594 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
teemai 2013-10-23
  • 打赏
  • 举报
回复

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class Dome extends javax.swing.JFrame {

	JLabel topLabel;
	JLabel huiyixinxi, yudingxinxi, yanshi, tiqianjiesu;
	JButton exitButton, loginButton;

	public void paint(Graphics g) {
		super.paint(g);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setBounds(0, 0, (int) screenSize.getWidth(),
				(int) screenSize.getHeight());
		g.drawImage(new ImageIcon(
				"C:/Users/eacfgjl/Desktop/clock.png").getImage(), 0, 0, getWidth(), getHeight(), this);// 这里换成你图片的路径
	}
	
	/** Creates new form Dome */
	public Dome() {
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {
		this.setBounds(0, 0, 1024, 768);
		this.setResizable(false);//
		topLabel = new JLabel();
		topLabel.setIcon(new ImageIcon("images/mitbg.bmp"));
		add(topLabel);
		topLabel.setLayout(null);

		yudingxinxi = new JLabel("预定信息");
		yudingxinxi.setForeground(Color.WHITE);
		huiyixinxi = new JLabel("会议信息");
		yanshi = new JLabel("会议延时");
		tiqianjiesu = new JLabel("提前结束");
		exitButton = new JButton("退 出");
		loginButton = new JButton("登 录");

		ButtonListener bListener = new ButtonListener();
		// 按钮注册事件监听器
		exitButton.addActionListener(bListener);
		loginButton.addActionListener(bListener);

		MyKeyListener keylis = new MyKeyListener();

		yudingxinxi.setSize(45, 20);
		yudingxinxi.setLocation(40, 280);
		yudingxinxi.setIcon(new ImageIcon("images/huiyi.bmp"));
		topLabel.add(yudingxinxi);

		yanshi.setSize(50, 20);
		yanshi.setLocation(85, 280);
		topLabel.add(yanshi);

		huiyixinxi.setSize(40, 20);
		huiyixinxi.setLocation(140, 280);
		topLabel.add(huiyixinxi);

		tiqianjiesu.setSize(50, 20);
		tiqianjiesu.setLocation(195, 280);
		topLabel.add(tiqianjiesu);

		exitButton.setSize(80, 20);
		exitButton.setLocation(260, 280);
		topLabel.add(exitButton);

		loginButton.setSize(80, 20);
		loginButton.setLocation(350, 280);
		topLabel.add(loginButton);

		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		// setSize(500,350);
		setVisible(true);

	}// </editor-fold>
		// GEN-END:initComponents

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new Dome().setVisible(true);
			}
		});
	}

	private void checkNamePwd() {

	}

	private void exitWindow() {
		System.exit(0);
	}

	class ButtonListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			// 获取事件源
			Object source = e.getSource();
			if (source == exitButton) {
				exitWindow();
			} else if (source == loginButton) {
				checkNamePwd();
			}
		}
	}

	class MyKeyListener extends KeyAdapter {
		public void keyPressed(KeyEvent ke) {
			int code = ke.getKeyCode();
			if (code == 10) {
				checkNamePwd();
			} else if (code == 27) {
				exitWindow();
			}
		}

		// GEN-BEGIN:variables
		// Variables declaration - do not modify
		// End of variables declaration//GEN-END:variables
	}
}
寻找IT之路 2013-10-23
  • 打赏
  • 举报
回复
引用
设置背景图的话是重写paint方法。。。 不是添加图片。。
失落夏天 2013-10-23
  • 打赏
  • 举报
回复
设置背景图的话是重写paint方法。。。 不是添加图片。。
飞舞的锄头 2013-10-23
  • 打赏
  • 举报
回复
加载顺序的问题,你把” topLabel.add(yudingxinxi);“放到最后加载试试
teemai 2013-10-23
  • 打赏
  • 举报
回复
引用 5 楼 xu123555033 的回复:
[quote=引用 4 楼 huxiweng 的回复:]

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class Dome extends javax.swing.JFrame {

	JLabel topLabel;
	JLabel huiyixinxi, yudingxinxi, yanshi, tiqianjiesu;
	JButton exitButton, loginButton;

	public void paint(Graphics g) {
		super.paint(g);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setBounds(0, 0, (int) screenSize.getWidth(),
				(int) screenSize.getHeight());
		g.drawImage(new ImageIcon(
				"C:/Users/eacfgjl/Desktop/clock.png").getImage(), 0, 0, getWidth(), getHeight(), this);// 这里换成你图片的路径
	}
	
	/** Creates new form Dome */
	public Dome() {
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {
		this.setBounds(0, 0, 1024, 768);
		this.setResizable(false);//
		topLabel = new JLabel();
		topLabel.setIcon(new ImageIcon("images/mitbg.bmp"));
		add(topLabel);
		topLabel.setLayout(null);

		yudingxinxi = new JLabel("预定信息");
		yudingxinxi.setForeground(Color.WHITE);
		huiyixinxi = new JLabel("会议信息");
		yanshi = new JLabel("会议延时");
		tiqianjiesu = new JLabel("提前结束");
		exitButton = new JButton("退 出");
		loginButton = new JButton("登 录");

		ButtonListener bListener = new ButtonListener();
		// 按钮注册事件监听器
		exitButton.addActionListener(bListener);
		loginButton.addActionListener(bListener);

		MyKeyListener keylis = new MyKeyListener();

		yudingxinxi.setSize(45, 20);
		yudingxinxi.setLocation(40, 280);
		yudingxinxi.setIcon(new ImageIcon("images/huiyi.bmp"));
		topLabel.add(yudingxinxi);

		yanshi.setSize(50, 20);
		yanshi.setLocation(85, 280);
		topLabel.add(yanshi);

		huiyixinxi.setSize(40, 20);
		huiyixinxi.setLocation(140, 280);
		topLabel.add(huiyixinxi);

		tiqianjiesu.setSize(50, 20);
		tiqianjiesu.setLocation(195, 280);
		topLabel.add(tiqianjiesu);

		exitButton.setSize(80, 20);
		exitButton.setLocation(260, 280);
		topLabel.add(exitButton);

		loginButton.setSize(80, 20);
		loginButton.setLocation(350, 280);
		topLabel.add(loginButton);

		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		// setSize(500,350);
		setVisible(true);

	}// </editor-fold>
		// GEN-END:initComponents

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new Dome().setVisible(true);
			}
		});
	}

	private void checkNamePwd() {

	}

	private void exitWindow() {
		System.exit(0);
	}

	class ButtonListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			// 获取事件源
			Object source = e.getSource();
			if (source == exitButton) {
				exitWindow();
			} else if (source == loginButton) {
				checkNamePwd();
			}
		}
	}

	class MyKeyListener extends KeyAdapter {
		public void keyPressed(KeyEvent ke) {
			int code = ke.getKeyCode();
			if (code == 10) {
				checkNamePwd();
			} else if (code == 27) {
				exitWindow();
			}
		}

		// GEN-BEGIN:variables
		// Variables declaration - do not modify
		// End of variables declaration//GEN-END:variables
	}
}
我想把这几个label 加上图片怎么办 每次加上去就会酒后把后面的覆盖掉 [/quote]

Image image = Toolkit.getDefaultToolkit().getImage("c:\\abc.jpg");
 
          Icon icon = new ImageIcon(image);
          jlabel.setIcon(icon);
teemai 2013-10-23
  • 打赏
  • 举报
回复

Image image = Toolkit.getDefaultToolkit().getImage("c:\\abc.jpg");
 
          Icon icon = new ImageIcon(image);
          jlabel.setIcon(icon);
林尹 2013-10-23
  • 打赏
  • 举报
回复
引用 4 楼 huxiweng 的回复:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class Dome extends javax.swing.JFrame {

JLabel topLabel;
JLabel huiyixinxi, yudingxinxi, yanshi, tiqianjiesu;
JButton exitButton, loginButton;

public void paint(Graphics g) {
super.paint(g);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(0, 0, (int) screenSize.getWidth(),
(int) screenSize.getHeight());
g.drawImage(new ImageIcon(
"C:/Users/eacfgjl/Desktop/clock.png").getImage(), 0, 0, getWidth(), getHeight(), this);// 这里换成你图片的路径
}

/** Creates new form Dome */
public Dome() {
initComponents();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
// GEN-BEGIN:initComponents
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
this.setBounds(0, 0, 1024, 768);
this.setResizable(false);//
topLabel = new JLabel();
topLabel.setIcon(new ImageIcon("images/mitbg.bmp"));
add(topLabel);
topLabel.setLayout(null);

yudingxinxi = new JLabel("预定信息");
yudingxinxi.setForeground(Color.WHITE);
huiyixinxi = new JLabel("会议信息");
yanshi = new JLabel("会议延时");
tiqianjiesu = new JLabel("提前结束");
exitButton = new JButton("退 出");
loginButton = new JButton("登 录");

ButtonListener bListener = new ButtonListener();
// 按钮注册事件监听器
exitButton.addActionListener(bListener);
loginButton.addActionListener(bListener);

MyKeyListener keylis = new MyKeyListener();

yudingxinxi.setSize(45, 20);
yudingxinxi.setLocation(40, 280);
yudingxinxi.setIcon(new ImageIcon("images/huiyi.bmp"));
topLabel.add(yudingxinxi);

yanshi.setSize(50, 20);
yanshi.setLocation(85, 280);
topLabel.add(yanshi);

huiyixinxi.setSize(40, 20);
huiyixinxi.setLocation(140, 280);
topLabel.add(huiyixinxi);

tiqianjiesu.setSize(50, 20);
tiqianjiesu.setLocation(195, 280);
topLabel.add(tiqianjiesu);

exitButton.setSize(80, 20);
exitButton.setLocation(260, 280);
topLabel.add(exitButton);

loginButton.setSize(80, 20);
loginButton.setLocation(350, 280);
topLabel.add(loginButton);

setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// setSize(500,350);
setVisible(true);

}// </editor-fold>
// GEN-END:initComponents

/**
* @param args
* the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Dome().setVisible(true);
}
});
}

private void checkNamePwd() {

}

private void exitWindow() {
System.exit(0);
}

class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// 获取事件源
Object source = e.getSource();
if (source == exitButton) {
exitWindow();
} else if (source == loginButton) {
checkNamePwd();
}
}
}

class MyKeyListener extends KeyAdapter {
public void keyPressed(KeyEvent ke) {
int code = ke.getKeyCode();
if (code == 10) {
checkNamePwd();
} else if (code == 27) {
exitWindow();
}
}

// GEN-BEGIN:variables
// Variables declaration - do not modify
// End of variables declaration//GEN-END:variables
}
}






我想把这几个label 加上图片怎么办 每次加上去就会酒后把后面的覆盖掉

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧