java展示图片(java如何显示图片)
华为云服务器特价优惠火热进行中! 2核2G2兆仅需 38 元;4核4G3兆仅需 79 元。购买时间越长越优惠!更多配置及优惠价格请咨询客服。
合作流程: |
本篇文章给大家谈谈java展示图片,以及java如何显示图片对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
微信号:cloud7591如需了解更多,欢迎添加客服微信咨询。
复制微信号
本文目录一览:
Java对话框上显示图片
其实有很多种方法可以解决图片显示大小的问题:
使用photoshop修改. 优点是可以节省系统资源, 显示图片的时候,不用做处理,缺点是需要了解ps的基本操作
使用JDialog 自定义对话框. 优点 可以实现复杂的效果, 缺点,代码量比较多
使用ImageIcon, Image 类 实现图片的缩放,. 优点: 纯java代码解决, 缺点: 如果大量的图片需要缩放, 那么可能影响程序的速度.
方案3的代码如下
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class Test {
public static void main(String[] args) {
ImageIcon icon = new ImageIcon("imgs/1.png"); // 得到icon对象 .注意我的图片地址和你的不一样,注意修改!!
Image image = icon.getImage(); //icon---Image
float scale = 0.5f; //缩放比例 50%
int width = Math.round(icon.getIconWidth()*scale); // 变小 50%的宽
int height= Math.round(icon.getIconHeight()*scale);// 变小50%的高
Image miniIcon = image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
// image 变成指定大小. 缩放模式为 SCALE_SMOOTH(平滑优先)
ImageIcon smallIcon = new ImageIcon(miniIcon);// Image---icon
JOptionPane.showInputDialog(null, "吃了吗?", "标题", 0, smallIcon, null, "默认值");
}
}
效果图
图1 图片显示比例为原图的50%
图2 图片显示比例为原图的120%

Java图片显示不出来,怎么解决
有两个问题:
图片路径没有写对,图片在 src 下,图片路径应是 src/海洋.png,正确的写法应是 image = new ImageIcon("src/海洋.png")
image = new ImageIcon("src/海洋.png") 应该放在 label = new JLabel(image); 前面。
如下例:
import javax.swing.*;
class JPanelDemo extends JPanel {
JLabel label;
JTextField text;
JButton button;
ImageIcon image;
public JPanelDemo() {
image = new ImageIcon("src/test.png");
label = new JLabel(image);
text = new JTextField(20);
button = new JButton("确定");
add(label);
add(text);
add(button);
}
}
public class App extends JFrame {
public App() {
this.setSize(500, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(new JPanelDemo());
}
public static void main(String[] args) {
new App().setVisible(true);
}
}
java中怎样在界面中显示图片
方法一:
[java] view plain copy
JLabel helloLabel = new JLabel("New label");
helloLabel.setIcon(new ImageIcon("E:\\javaSE\u4EE3\u7801\\TimeManager\\asset\\hello.gif"));
helloLabel.setBackground(Color.BLACK);
helloLabel.setBounds(0, 0, 105, 50);
contentPane.add(helloLabel);
方法二:
[java] view plain copy
ImageIcon imagetoshow=new ImageIcon(urlofimagetoshow);
JLabel showimagelabel=new JLabel(imagetoshow);
this.getLayeredPane().add(showimagelabel,
new Integer(Integer.MIN_VALUE)); // 设置JLabel在最底层
showimagelabel.setBounds(0, 0, 500,150);
java显示ico格式图片
--------------------------------------------------------------------------------------------
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ImageApp extends JFrame {
public ImageApp() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(400, 300);
setResizable(false);
getContentPane().setLayout(null);
JPanel panel = new ImagePanel();
panel.setBounds(0, 0, 400, 300);
getContentPane().add(panel);
setVisible(true);
}
public static void main(String[] args) {
new ImageApp();
}
class ImagePanel extends JPanel {
public void paint(Graphics g) {
super.paint(g);
ImageIcon icon = new ImageIcon("D:\\14405937jqhjsppeninjf9.ico");
g.drawImage(icon.getImage(), 0, 0, 400, 300, this);
}
}
}
JAVA中如何显示图片一部分
g.drawImage(img, 显示在窗口的起始横坐标, 显示在窗口的起始纵坐标, 显示在窗口的宽度, 显示在窗口的高度, 156,72, 156+26, 72+36, null);
关于java展示图片和java如何显示图片的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
