我创建了一个包含图像的简单框架程序。但是图像的大小与帧不同。如果我放大框架,图像尺寸保持不变?
如何使图像与框架尺寸相同?
这是我目前的代码:
JPanel panel1 = new JPanel();
ImageIcon image1 = new ImageIcon("C:\\Users\\Dark Mangetsu\\Downloads\\Ceng102_Lab10.1\\image\\flower.jpg");
JLabel label1 = new JLabel(image1);
panel1.add(label1);
Color color1 = new Color (200, 0 ,100);
panel1.setBorder(BorderFactory.createLineBorder(color1, 3));
JFrame f = new JFrame("Frame");
f.setLayout(new BorderLayout(5,5));
f.add((panel1),BorderLayout.WEST);
f.setSize(320,200);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
答案 0 :(得分:3)
您可以绘制图像而不是使用标签。
ImageIcon icon = new ImageIcon("C:\\Users\\Dark Mangetsu\\Downloads\\Ceng102_Lab10.1\\image\\flower.jpg");
Image image = icon.getImage();
JPanel panel1 = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, getWidth(), getHeight());
}
};
也不确定,但我认为您可能希望将面板添加到CENTER而不是西部(如果您希望图像在框架中居中)。
也不是,如果你想要面板的preferredSize,你也必须覆盖面板的getPreferredSize()
。
JPanel panel1 = new JPanel() {
...
@Override
public Dimension getPreferredSize() {
return new Dimension(320, 200);
}
};
然后您可以pack()
首选f.pack();
//f.setSize(320, 200);
框架,而不是设置尺寸
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestBackgroundResize {
public TestBackgroundResize() {
JFrame frame = new JFrame();
frame.setContentPane(createBackgroundPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private JPanel createBackgroundPanel() {
return new JPanel() {
BufferedImage image;
{
try {
image = ImageIO.read(getClass().getResource("/marioblobs/mario.png"));
} catch (IOException ex) {
Logger.getLogger(TestBackgroundResize.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(320, 200);
}
};
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new TestBackgroundResize();
}
});
}
}
{{1}}
答案 1 :(得分:0)
我认为代码缺少对pack()
方法的调用。
以下是示例代码:
public class ImageToPanel {
public static void main(String[] args) {
ImageToPanel itp = new ImageToPanel();
itp.go();
}
private void go() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() { createAndShowGUI(); }
});
}
private void createAndShowGUI() {
JFrame f =new JFrame();
f.setLayout(new BorderLayout());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setPreferredSize(new Dimension(640,400));
JLabel label = new JLabel( new ImageIcon("wallpaper.jpg") );
f.add(label, BorderLayout.CENTER);
JButton button = new JButton("Quit");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
f.add(button, BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
}
}
<强> SCREENSHOT 强>
在下面的屏幕截图中,1920x1200壁纸在640x400帧中无缝约束。
经过测试
修改强>
ImageIcon image1 = new ImageIcon("wallpaper.jpg");
JLabel label1 = new JLabel(image1);
Color color1 = new Color (200, 0 ,100);
label1.setBorder(BorderFactory.createLineBorder(color1, 3));
JFrame f = new JFrame("Frame");
f.setLayout(new BorderLayout(5,5));
f.add(label1,BorderLayout.WEST);
f.setSize(320,200);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);