鱼C论坛

 找回密码
 立即注册
查看: 1781|回复: 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 主类:
package storeFrame;

import java.awt.Color;
import java.awt.Image;
import java.net.URL;

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

import StorePanel.BackgroundPanel;

public class LoginFrame extends JFrame{
        private URL url = null;// 声明图片的URL
    private Image image=null;// 声明图像对象
        private BackgroundPanel bgPanel = null;
        
        
        public LoginFrame() {
                super("aaaa");
                JPanel pane = new JPanel();
                this.add(getPanel());
                this.setSize(700,500);
                this.setLocation(300, 200);
//                this.setUndecorated(true);
                this.setVisible(true);
        }
        
        private BackgroundPanel getPanel() {
                if(bgPanel==null) {
                        url = LoginFrame.class.getResource("/img/flowerBook.jpg");
                        image = new ImageIcon(url).getImage();
                        bgPanel = new BackgroundPanel(image);
                }
                return bgPanel;
        }
        public static void main(String[] args) {
                new LoginFrame();
        }
} 

BackgroundPanel 类
 package StorePanel;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.JPanel;

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

        /**
         * 面板构造方法
         * 
         * @param image
         *            -背景图片对象
         */
        public BackgroundPanel(Image image) {
                super(); // 调用超类的构造方法
                this.image = image; // 为图像对象赋值
                initialize();
        }

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

        /**
         * 初始化面板大小
         */
        private void initialize() {
                this.setSize(300, 200);
        }
}


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

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

最佳答案

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-5-1 11:05:07 | 显示全部楼层    本楼为最佳答案   
加一段代码
setLayout(null);//使用绝对布局
JPanel p1 = getPanel();
p1.setBounds(x,y,width,height);
add(p1);
//x和y是坐标,后面是尺寸
想知道小甲鱼最近在做啥?请访问 -> 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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

可以了,setBounds有用!谢谢
想知道小甲鱼最近在做啥?请访问 -> 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的连接的那个最终代码样例运行没有显示
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-7-4 14:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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