写了一个GUI小程序,就在JFrame上显示一张图片import javax.swing.*;
import java.awt.*;
import java.util.Date;public class MainPane extends JFrame{ /**
*
*/
private static final long serialVersionUID = 1L; private FacePanel imFace = new FacePanel("1.jpg",70);
public static void main(String[] args){
new MainPane();
}
public MainPane(){
JPanel jp = new JPanel(new GridLayout(1,2));
JLabel jlb = new JLabel("Me I'm");
jp.add(imFace);
jp.add(jlb);
setLayout(new BorderLayout(1,1));
add(jp,BorderLayout.NORTH);
setTitle("Main Panel");
setSize(250,600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
class FacePanel extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
private Image faceIcon;
private int sideLength;
private FacePanel(String path,int sideLength){
faceIcon = new ImageIcon(path).getImage();
this.sideLength = sideLength; }
protected void paintComponent(Graphics g){
super.paintComponent(g);
if(faceIcon != null)
g.drawImage(faceIcon,5,5,sideLength,sideLength,this);
}
}
}
但是为什么1.jpg被挤压成一个小点了,不能显示全部啊????
大牛们帮忙看看是什么问题哈。。
import java.awt.*;
import java.util.Date;public class MainPane extends JFrame{ /**
*
*/
private static final long serialVersionUID = 1L; private FacePanel imFace = new FacePanel("1.jpg",70);
public static void main(String[] args){
new MainPane();
}
public MainPane(){
JPanel jp = new JPanel(new GridLayout(1,2));
JLabel jlb = new JLabel("Me I'm");
jp.add(imFace);
jp.add(jlb);
setLayout(new BorderLayout(1,1));
add(jp,BorderLayout.NORTH);
setTitle("Main Panel");
setSize(250,600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
class FacePanel extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1L;
private Image faceIcon;
private int sideLength;
private FacePanel(String path,int sideLength){
faceIcon = new ImageIcon(path).getImage();
this.sideLength = sideLength; }
protected void paintComponent(Graphics g){
super.paintComponent(g);
if(faceIcon != null)
g.drawImage(faceIcon,5,5,sideLength,sideLength,this);
}
}
}
但是为什么1.jpg被挤压成一个小点了,不能显示全部啊????
大牛们帮忙看看是什么问题哈。。
可能NORTH因为是水平延展的,不足以显示照片的全部身高
换成CENTER呢
add(jp, BorderLayout.CENTER);
setPreferredSize(new Dimension(faceIcon.getWidth(),factIcon.getHeight()));
return new Dimension(100, 100);
}
通过preferredSize设置它的size,使之能正常显示图片
add(jp,BorderLayout.CENTER);
因为BorderLayout布局有个特点,就是当其它的区上没有内容时,默认会让CENTER区的内容尽最大可能去占用容器的空间
import java.awt.*;public class MainPane extends JFrame {
public MainPane() {
super("Swing Demo");
JPanel panel = new JPanel(new GridLayout(2,1));
JLabel imgLbl = new JLabel(new ImageIcon(ClassLoader.getSystemResource("com/ims/tools/images/leaf.gif")));
JButton testBtn = new JButton("Button");
panel.add(imgLbl);
panel.add(testBtn); this.getContentPane().add(panel, BorderLayout.CENTER);
this.setSize(200, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
//应该养成习惯,界面显示类的代码应该在EDT中执行
public void run() {
new MainPane().setVisible(true);
}
});
}
}