gpt4 book ai didi

java - 为什么 JFrame 在添加图像时会自行调整大小

转载 作者:行者123 更新时间:2023-11-30 08:08:06 26 4
gpt4 key购买 nike

框架的大小设置为 500x500,但是当我向其中添加图像时,框架会调整为图像的大小。我试过用这个

jf.setPreferredSize(new Dimension(500, 500));

但随后图像不会出现。

import java.awt.*;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import java.awt.event.*;
public class Test implements ActionListener {
ImageIcon imgIcon;
JFrame jf ;
JPanel jp_image,jp_option;
BufferedImage img;
Test(){
jf = new JFrame("photo editor");
jf.setPreferredSize(new Dimension(500, 500));
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jp_image = new JPanel();
jp_option = new JPanel();

jp_image.setPreferredSize(new Dimension(200, 500));
jp_option.setPreferredSize(new Dimension(300, 500));

JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true, jp_option, jp_image);
jf.getContentPane().add(splitPane);
jf.setSize(1900, 1000);
JMenuBar mbar = new JMenuBar();
jf.setJMenuBar(mbar);
JMenu File = new JMenu("File");
JMenuItem New;
File.add(New = new JMenuItem("New..."));
mbar.add(File);
jf.setVisible(true);
New.addActionListener(this);
}


public void actionPerformed(ActionEvent ae){
String arg = ae.getActionCommand();
if(arg.equals("New...")){
File file = null;
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(" jpg,gif,jpeg,png", "jpg", "gif" , "jpeg" , "png");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("File Choosed: " + chooser.getCurrentDirectory() + "\\" +chooser.getSelectedFile().getName());
file = chooser.getSelectedFile();
}
try{
img = ImageIO.read(file);
jp_image.add(new JLabel(new ImageIcon(img)));
jp_image.setSize(300, 500);
jf.setPreferredSize(new Dimension(500, 500));// no image appears
//jf.pack(); the image appears but the frame resizes
}
catch(Exception e){
System.out.println("No Image Selected");
}
}


}

public static void main(String args[]){
Test t = new Test();
}
}

如何在不调整框架大小的情况下在面板中设置图像。

最佳答案

您正在向 GUI 添加组件,但并未告知组件容器的布局管理器您正在执行此操作。添加 JLabel 后,请务必在容器上调用 repaint()revalidate()。例如:

img = ImageIO.read(file);
jp_image.add(new JLabel(new ImageIcon(img)));
jp_image.setSize(300, 500);
jf.setPreferredSize(new Dimension(500, 500));// no image appears
jp_image.revalidate();
jp_image.repaint();

更好的是 - 在创建 GUI 时只添加 JLabel 一次,并且在获得图像时不要重新添加它,而是简单地设置它的图标。例如:

public class Test implements ActionListener {
ImageIcon imgIcon;
JFrame jf;
JPanel jp_image, jp_option;
BufferedImage img;
private JLabel imageLabel = new JLabel();

Test() {
// ...

jp_image.add(imageLabel); //!!

// ...
}

public void actionPerformed(ActionEvent ae) {
String arg = ae.getActionCommand();
if (arg.equals("New...")) {
// ...

try {
img = ImageIO.read(file);
imageLabel.setIcon(new ImageIcon(img)); //!!
} catch (Exception e) {
e.printStackTrace();
}
}

关于java - 为什么 JFrame 在添加图像时会自行调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33320774/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com