我想知道这样的Java代码怎么加代码能添加上背景图片
发布网友
发布时间:2022-05-16 20:30
我来回答
共2个回答
热心网友
时间:2024-03-01 18:09
package picture.test.shuiying;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class Mypanel2 extends JPanel{
private Image image;
private PictureFrame frame;
public Mypanel2(PictureFrame frame) {
super();
this.frame=frame;
image=new ImageIcon("images/back.png").getImage();
}
public Mypanel2() {
super();
// TODO Auto-generated constructor stub
}
@Override
public void paintComponent(Graphics g) {
// TODO Auto-generated method stub
super.paintComponents(g);
g.drawImage(image, 0, 0, this.frame.getWidth(), this.frame.getHeight(),null);
g.drawString("妈妈", 100, 100);
this.setOpaque(true);
}
package picture.test.shuiying;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class Mypanel2 extends JPanel{
private Image image;
private PictureFrame frame;
public Mypanel2(PictureFrame frame) {
super();
this.frame=frame;
image=new ImageIcon("images/back.png").getImage();
}
package picture.test.shuiying;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class Mypanel2 extends JPanel{
private Image image;
private PictureFrame frame;
public Mypanel2(PictureFrame frame) {
super();
this.frame=frame;
image=new ImageIcon("images/back.png").getImage();
}
public Mypanel2() {
super();
// TODO Auto-generated constructor stub
}
@Override
public void paintComponent(Graphics g) {
// TODO Auto-generated method stub
super.paintComponents(g);
g.drawImage(image, 0, 0, this.frame.getWidth(), this.frame.getHeight(),null);
g.drawString("妈妈", 100, 100);
this.setOpaque(true);
}
}
package picture.test.shuiying;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class PictureFrame extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
PictureFrame frame = new PictureFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public PictureFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 333, 461);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new Mypanel2(this);
JTextField txt=new JTextField("小丽");
contentPane.add(panel, BorderLayout.CENTER);
}
}
以前写过的代码希望能帮到你
热心网友
时间:2024-03-01 18:10
要想达到效果可以添加一个label,设置大小为整个界面,重叠设置到底部,把图片放到label中就行了