java swing添加图片
2个回答
2015-06-23 · 百度知道合伙人官方认证企业
关注
展开全部
在java swing中需要为容器添加图片,或者背景图片。
提供两种简单的解决方案,一种利用JPanel,另一种利用JLabel
1.JPanel(源代码)
package oo;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Drawing {
JFrame jframe = new JFrame();
public static JPanel GImage = null;
public Drawing() {
initFrame();
}
// 初始化窗口
public void initFrame() {
// 利用JPanel添加背景图片
GImage = new JPanel() {
protected void paintComponent(Graphics g) {
ImageIcon icon = new ImageIcon("image\\benbenla.jpg");
Image img = 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);
}
public static void main(String[] args) {
new Drawing();
}
}
2.JLabel源代码
package swing.draw;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Drawing2 {
JLabel jlpic = new JLabel();
JFrame jframe = new JFrame();
public Drawing2() {
init1Frame();
}
public void init1Frame() {
ImageIcon icon = new ImageIcon("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);
}
public static void main(String args[]) {
new Drawing2();
}
}
提供两种简单的解决方案,一种利用JPanel,另一种利用JLabel
1.JPanel(源代码)
package oo;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Drawing {
JFrame jframe = new JFrame();
public static JPanel GImage = null;
public Drawing() {
initFrame();
}
// 初始化窗口
public void initFrame() {
// 利用JPanel添加背景图片
GImage = new JPanel() {
protected void paintComponent(Graphics g) {
ImageIcon icon = new ImageIcon("image\\benbenla.jpg");
Image img = 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);
}
public static void main(String[] args) {
new Drawing();
}
}
2.JLabel源代码
package swing.draw;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Drawing2 {
JLabel jlpic = new JLabel();
JFrame jframe = new JFrame();
public Drawing2() {
init1Frame();
}
public void init1Frame() {
ImageIcon icon = new ImageIcon("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);
}
public static void main(String args[]) {
new Drawing2();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询