我想在同一个 JFrame 中显示同一图像的变体,例如在 JFrame 中显示一个图像,然后用同一图像的灰度替换它。
问问题
73941 次
4 回答
45
为了构建 camickr 的解决方案(对于像我这样想要快速复制/粘贴代码的懒人),这里有一个代码说明:
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(new JLabel(new ImageIcon(img)));
frame.getContentPane().add(new JLabel(new ImageIcon(img2)));
frame.getContentPane().add(new JLabel(new ImageIcon(img3)));
frame.pack();
frame.setVisible(true);
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // if you want the X button to close the app
于 2012-02-09T17:02:38.057 回答
8
于 2009-10-26T19:12:00.837 回答
4
我不确定你的问题是什么,但如果你有一个 BufferedImage 那么你只需使用图像创建一个 ImageIcon ,然后你将图标添加到 JLabel 并将标签添加到 GUI 就像任何其他组件一样。
如果您的问题是关于如何创建灰度,我建议您使用这些术语作为搜索关键字搜索网络,我相信您会在那里找到示例。
于 2009-10-26T20:03:17.583 回答
4
万一生命太短了,请阅读官方文档,这是一种多次完成的肮脏方法
private static JFrame frame;
private static JLabel label;
public static void display(BufferedImage image){
if(frame==null){
frame=new JFrame();
frame.setTitle("stained_image");
frame.setSize(image.getWidth(), image.getHeight());
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
label=new JLabel();
label.setIcon(new ImageIcon(image));
frame.getContentPane().add(label,BorderLayout.CENTER);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}else label.setIcon(new ImageIcon(image));
}
于 2020-01-19T17:13:52.143 回答