int backw = JXCFrame.this.frame.getWidth();
int backh = frame.getHeight(); // 获取窗口的宽和高
backLabel.setSize(backw, backh); // 设置标签的宽和高
// 导入背景图片
backLabel.setText("<html><body><image width ='"
+ backw + "'height ='" + (backh-90) + "'src="
+ JXCFrame.this.getClass().getResource("welcome1.jpg")
+ "'></img></body></html>");如上代码,frame是一个普通的JFrame,backLabel是一个普通的JLabel,现在把backLabel放进frame里去了,但图片不能自动缩放哪位大虾能帮我解决这个问题?
int backh = frame.getHeight(); // 获取窗口的宽和高
backLabel.setSize(backw, backh); // 设置标签的宽和高
// 导入背景图片
backLabel.setText("<html><body><image width ='"
+ backw + "'height ='" + (backh-90) + "'src="
+ JXCFrame.this.getClass().getResource("welcome1.jpg")
+ "'></img></body></html>");如上代码,frame是一个普通的JFrame,backLabel是一个普通的JLabel,现在把backLabel放进frame里去了,但图片不能自动缩放哪位大虾能帮我解决这个问题?
bufferedimage有getscaledInstance方法,可以采用其中的SMOOTH参数,即用区域平均采样的方法缩放得到缩放后的图片根据这个思路,可以先获得bufferedimage的缩放示例,然后在jframe上绘制——或者自己继承一个JFrame,在onPaint方法中复写,应用上述逻辑。
你可以这样理解,JFrame仅仅只是放一个图片,因为我frame的NORTH放的是其它,NORTH以下放的是这个图片
这个好像是awt的吧?swing的没有吗?
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;
import javax.swing.JButton;
import java.awt.image.*;public class MIcon implements Icon {
private BufferedImage i=null;
private Icon icon = null; public MIcon(Icon icon) {
this.icon = icon;
// icon=new ImageIcon(i);
} 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( "resources/images/thu3.jpg "));
JLabel label = new JLabel(icon);
JFrame frame = new JFrame();
frame.getContentPane().add(label, BorderLayout.CENTER);
// frame.getContentPane().add(new JButton("click"),BorderLayout.NORTH);
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
//设置窗口放大缩小时改变图片大小
frame.addComponentListener(new ComponentAdapter()
{
@Override
public void componentResized(ComponentEvent e)
{
updateBack();
}
}); private void updateBack()
{
if (null != backLabel)
{ int backw = JXCFrame.this.frame.getWidth();
int backh = frame.getHeight(); // 获取窗口的宽和高
backLabel.setSize(backw, backh); // 设置标签的宽和高
backLabel.setLayout(new BorderLayout());
// 导入背景图片
backLabel.setText("<html><body><image width ='"
+ backw + "'height ='" + (backh-90) + "'src="
+ JXCFrame.this.getClass().getResource("welcome1.jpg")
+ "'></img></body></html>");
}
}