Java 如何使图像大小适合ImageIcon和JLabel大小
我的项目是打开一个图像并再次保存,我使用ImageIcon、JLabel和JPanel来显示它(我使用ImageIcon和JPanel,但它不起作用,ImageIcon无法添加到JPanel)。当我打开它时,它总是显示图像,但不是JFrame的全尺寸。 我在OpenImage类中编写的代码扩展了JFrameJava 如何使图像大小适合ImageIcon和JLabel大小,java,swing,jpanel,jlabel,imageicon,Java,Swing,Jpanel,Jlabel,Imageicon,我的项目是打开一个图像并再次保存,我使用ImageIcon、JLabel和JPanel来显示它(我使用ImageIcon和JPanel,但它不起作用,ImageIcon无法添加到JPanel)。当我打开它时,它总是显示图像,但不是JFrame的全尺寸。 我在OpenImage类中编写的代码扩展了JFrame public class Draw_JPanel extends JFrame{ Load_image panel_im = new Load_image(); public
public class Draw_JPanel extends JFrame{
Load_image panel_im = new Load_image();
public void OpenImage()
{
JFileChooser fc = new JFileChooser();
int result = fc.showOpenDialog(null);
if(result == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
String name = file.getName();
try {
image = ImageIO.read(file);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
imageic = new ImageIcon(image);
height1 = imageic.getIconHeight();
width1 = imageic.getIconWidth();
picLabel = new JLabel(new ImageIcon(image));
panel_im.add(picLabel,BorderLayout.CENTER);
}
this.pack();
}
以及Load_image类的代码
public class Load_image extends JPanel{
public Load_image()
{
this.setBackground(Color.RED);
}
}
很抱歉,我无法上传图像我为您编写了一个简单的示例。它在的帮助下绘制图像并调整其大小。也可以保存图像。试试看,我觉得它对你有帮助
public class Example extends JPanel{
private static BufferedImage scaledImg;
public static void main(String[] args) {
Example e = new Example();
JFrame f = new JFrame();
f.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 1;
f.add(e,c);
c.fill = GridBagConstraints.NONE;
c.weightx = 0;
c.weighty = 0;
c.gridx = 1;
JButton b = new JButton("save");
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
File outputfile = new File("c:\\image.png");
try {
ImageIO.write(scaledImg, "png", outputfile);
} catch (IOException e) {
e.printStackTrace();
}
}
});
f.add(b,c);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
@Override
protected void paintComponent(Graphics arg0) {
super.paintComponent(arg0);
Graphics2D g = (Graphics2D) arg0;
BufferedImage img = null;
try {
img = ImageIO.read( Example.class.getResource("/res/3_disc.png"));
} catch (IOException e) {
e.printStackTrace();
}
scaledImg = Scalr.resize(img,getSize().height,getSize().width,new BufferedImageOp[]{});
g.drawImage (scaledImg, null, 0, 0);
}
}
这里的“/res/3_disc.png”
是我项目中的图像
“c:\\image.png”
图像输出我修复了我的代码,我添加了setBounds()方法和setLayout()方法,修复后的代码如下:
setLayout(null);
imageic = new ImageIcon(image);
height1 = imageic.getIconHeight();
width1 = imageic.getIconWidth();
picLabel = new JLabel(new ImageIcon(image));
picLabel.setSize(width1, height1);
picLabel.setBounds(0, 0, width1, height1);
panel_im.setSize(width1, height1);
panel_im.setBounds(-5, -5, width1, height1);
panel_im.add(picLabel);
this.pack();
tks for all您看到图像的完整尺寸了吗?或者您尝试调整图像大小以填充所有
JFrame
区域?我尝试将图像大小调整为JFrame我认为您的图像具有固定大小,因此您的标签无法调整其大小,如果您要调整图像大小,请尝试读取或查找其他项目并保存,但如果我将图像调整为JFrame,则在保存时,它的大小将会改变。如何将imageIcon打包到JLabel,然后打包到JPanel?我正在尝试使用您的代码,但什么是Scalr对象?我在任何地方都找不到您声明它的Scalr
它是库中的类,我将文本指向它。下载它并添加到类路径