设置图片问题 setIcon(new ImageIcon())要跟随JLabel的大小变,要用2D画了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 好像有一个方法图片可以得到图片的大小然后再调用setSize()方法最后再刷新一下validate() import java.awt.*;import java.awt.BorderLayout;import java.awt.Component;import java.awt.Graphics;import java.awt.Graphics2D;import javax.swing.Icon;import javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JLabel;public class MIcon implements Icon { private Icon icon = null; public MIcon(Icon icon) { this.icon = icon; } public int getIconHeight() { return icon.getIconHeight(); } public int getIconWidth() { return icon.getIconWidth(); } public void paintIcon(Component c, Graphics g, int x, int y) { float wid = c.getWidth(); float hei = c.getHeight(); int iconWid = icon.getIconWidth(); int iconHei = icon.getIconHeight(); Graphics2D g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g2d.scale(wid/iconWid, hei/iconHei); icon.paintIcon(c, g2d, 0, 0); } public static void main(String[] args) { MIcon icon = new MIcon(new ImageIcon("test.gif")); JLabel label = new JLabel(icon); JFrame frame = new JFrame(); frame.getContentPane().add(label, BorderLayout.CENTER); frame.setSize(800, 600); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }} java中的Swing桌面开发显示模态窗口 序列化问题 如何正序遍历出LinkedList中的行。。 jdk源码问题! 求助!求两个串的最长公共子序列(多个) java中 菜单中的事件处理 數據流打印 JButton问题 Applet中改变字体和大小显示?在线等待,急!!! javadoc! 大家帮忙看一下关于JAVA是参数传递是传值还是传引用的问题??? 求教关于正则表达式翻译问题
然后再调用setSize()方法
最后再刷新一下validate()
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;public class MIcon implements Icon {
private Icon icon = null; public MIcon(Icon icon) {
this.icon = icon;
} public int getIconHeight() {
return icon.getIconHeight();
} public int getIconWidth() {
return icon.getIconWidth();
} public void paintIcon(Component c, Graphics g, int x, int y) {
float wid = c.getWidth();
float hei = c.getHeight();
int iconWid = icon.getIconWidth();
int iconHei = icon.getIconHeight(); Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.scale(wid/iconWid, hei/iconHei);
icon.paintIcon(c, g2d, 0, 0);
} public static void main(String[] args) {
MIcon icon = new MIcon(new ImageIcon("test.gif"));
JLabel label = new JLabel(icon);
JFrame frame = new JFrame();
frame.getContentPane().add(label, BorderLayout.CENTER);
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}