JAVA中怎么在一个jpanel中放多张图片
2个回答
展开全部
package picture.test.shuiying;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class Mypanel extends JFrame{
//private PictureFrame pictureFrame;
Image image;
private JLabel contentpane;
public Mypanel() throws HeadlessException {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//this.pictureFrame = pictureFrame;
contentpane=new JLabel(new ImageIcon(createimage()));
this.getContentPane().add(contentpane);
this.setSize(400, 500);
this.setVisible(true);
}
public Image createimage()
{
BufferedImage bufferedImage=new BufferedImage(100, 200,BufferedImage.TYPE_INT_ARGB );
//第一张
Image image1=new ImageIcon("images/back.png").getImage();
//第二张
Image image2=new ImageIcon("images/addStu.png").getImage();
//得到画图工具
Graphics graphics=bufferedImage.getGraphics();
graphics.drawImage(image1, 0, 0, 100, 200, null);
graphics.drawImage(image2, 20, 20, 50, 50, null);
graphics.setColor(Color.BLUE);
graphics.drawString("luten", 50, 50);
return bufferedImage;
}
}
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 Mypanel(this);
JTextField txt=new JTextField("小丽");
contentPane.add(panel, BorderLayout.CENTER);
}
}
以前写过的代码希望能帮带你
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class Mypanel extends JFrame{
//private PictureFrame pictureFrame;
Image image;
private JLabel contentpane;
public Mypanel() throws HeadlessException {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//this.pictureFrame = pictureFrame;
contentpane=new JLabel(new ImageIcon(createimage()));
this.getContentPane().add(contentpane);
this.setSize(400, 500);
this.setVisible(true);
}
public Image createimage()
{
BufferedImage bufferedImage=new BufferedImage(100, 200,BufferedImage.TYPE_INT_ARGB );
//第一张
Image image1=new ImageIcon("images/back.png").getImage();
//第二张
Image image2=new ImageIcon("images/addStu.png").getImage();
//得到画图工具
Graphics graphics=bufferedImage.getGraphics();
graphics.drawImage(image1, 0, 0, 100, 200, null);
graphics.drawImage(image2, 20, 20, 50, 50, null);
graphics.setColor(Color.BLUE);
graphics.drawString("luten", 50, 50);
return bufferedImage;
}
}
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 Mypanel(this);
JTextField txt=new JTextField("小丽");
contentPane.add(panel, BorderLayout.CENTER);
}
}
以前写过的代码希望能帮带你
追问
JPanel panel = new Mypanel(this);这句出错啊
追答
package picture.test.shuiying;后面的别写了在mypnel类中直接写一主函数调用new mypanel()
2014-06-02
展开全部
在里面再套几个jpanel或者别的容器
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询