鱼C论坛

 找回密码
 立即注册
查看: 2509|回复: 4

[已解决]Java的GUI怎么让图片设置大小放在指定的位置

[复制链接]
发表于 2022-5-1 11:05:06 | 显示全部楼层 |阅读模式
50鱼币
求问:Java的GUI怎么让图片设置大小放在指定的位置

我在做GUI设计的时候,想让图片放到Jpanel中,在将这个Jpanel放到Jframe中,但是就是一直实现不了

要不是图片没加载,要不就是图片加载完,缩成一点点
微信截图_20220501105745.png
微信截图_20220501105817.png

直接放到Jframe又是全图的
微信截图_20220501105907.png
微信截图_20220501105516.png

代码如下:
LoginFrame 主类:
  1. package storeFrame;

  2. import java.awt.Color;
  3. import java.awt.Image;
  4. import java.net.URL;

  5. import javax.swing.ImageIcon;
  6. import javax.swing.JFrame;
  7. import javax.swing.JLabel;
  8. import javax.swing.JPanel;

  9. import StorePanel.BackgroundPanel;

  10. public class LoginFrame extends JFrame{
  11.         private URL url = null;// 声明图片的URL
  12.     private Image image=null;// 声明图像对象
  13.         private BackgroundPanel bgPanel = null;
  14.        
  15.        
  16.         public LoginFrame() {
  17.                 super("aaaa");
  18.                 JPanel pane = new JPanel();
  19.                 this.add(getPanel());
  20.                 this.setSize(700,500);
  21.                 this.setLocation(300, 200);
  22. //                this.setUndecorated(true);
  23.                 this.setVisible(true);
  24.         }
  25.        
  26.         private BackgroundPanel getPanel() {
  27.                 if(bgPanel==null) {
  28.                         url = LoginFrame.class.getResource("/img/flowerBook.jpg");
  29.                         image = new ImageIcon(url).getImage();
  30.                         bgPanel = new BackgroundPanel(image);
  31.                 }
  32.                 return bgPanel;
  33.         }
  34.         public static void main(String[] args) {
  35.                 new LoginFrame();
  36.         }
  37. }
复制代码


BackgroundPanel 类
  1. package StorePanel;

  2. import java.awt.Graphics;
  3. import java.awt.Graphics2D;
  4. import java.awt.Image;
  5. import javax.swing.JPanel;

  6. /**
  7. * 可以添加背景图片的面板
  8. */
  9. public class BackgroundPanel extends JPanel {
  10.         private static final long serialVersionUID = 1L;// 序列化编号
  11.         private Image image; // 定义图像对象

  12.         /**
  13.          * 面板构造方法
  14.          *
  15.          * @param image
  16.          *            -背景图片对象
  17.          */
  18.         public BackgroundPanel(Image image) {
  19.                 super(); // 调用超类的构造方法
  20.                 this.image = image; // 为图像对象赋值
  21.                 initialize();
  22.         }

  23.         /**
  24.          * 重写绘制组件方法
  25.          */
  26.         protected void paintComponent(Graphics g) {
  27.                 super.paintComponent(g); // 调用父类的方法
  28.                 Graphics2D g2 = (Graphics2D) g; // 创建Graphics2D对象
  29.                 if (image != null) {
  30.                         int width = getWidth(); // 获得面板的宽度
  31.                         int height = getHeight(); // 获得面板的高度
  32.                         // 绘制图像
  33.                         g2.drawImage(image, 0, 0, width, height, this);
  34.                 }
  35.         }

  36.         /**
  37.          * 初始化面板大小
  38.          */
  39.         private void initialize() {
  40.                 this.setSize(300, 200);
  41.         }
  42. }
复制代码



我想让Jpanel能像前端div一样设置大小位置,放到指定地方,不知道行不行

谢谢各位
最佳答案
2022-5-1 11:05:07
加一段代码
  1. setLayout(null);//使用绝对布局
  2. JPanel p1 = getPanel();
  3. p1.setBounds(x,y,width,height);
  4. add(p1);
  5. //x和y是坐标,后面是尺寸
复制代码

最佳答案

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-5-1 11:05:07 | 显示全部楼层    本楼为最佳答案   
加一段代码
  1. setLayout(null);//使用绝对布局
  2. JPanel p1 = getPanel();
  3. p1.setBounds(x,y,width,height);
  4. add(p1);
  5. //x和y是坐标,后面是尺寸
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-5-1 11:18:18 | 显示全部楼层

ImageIcon.paintIcon(Component c, Graphics g, int x, int y)

可以设置图片位置

Image.setImage(image.getImage().getScaledInstance(x,y,Image.SCALE_DEFAULT))

设置图片大小为 x * y

设置完图片大小在把他加入到 Jpanel 面板中试试看?

或者可以看看这篇文章,自适应:https://blog.csdn.net/bobo1356/article/details/52917304
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-5-1 13:30:03 | 显示全部楼层

可以了,setBounds有用!谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-5-1 13:31:26 | 显示全部楼层
Twilight6 发表于 2022-5-1 11:18
ImageIcon.paintIcon(Component c, Graphics g, int x, int y)

可以设置图片位置

不知道为什么,我按那个scdn的连接的那个最终代码样例运行没有显示
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-15 04:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表