我想把图片显示中面板中间,可是得不到想要的结果,而是现实在面板北部
程序代码:
import java.awt.BorderLayout; import java.awt.Image; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class tt extends JFrame { public tt() { JPanel jPanel=new JPanel(); ImageIcon image=new ImageIcon("C:/Users/wenguang/Desktop/新建文件夹/IMG_0030.jpg");//添加图片 JLabel label=new JLabel(image); int width = image.getIconWidth();//获取图片的宽高 int height = image.getIconHeight(); if (width > 1000) { width = 1276; height = 850; } image.setImage(image.getImage().getScaledInstance(width, height,//缩小图片 Image.SCALE_DEFAULT)); jPanel.add(label,new BorderLayout().CENTER); getContentPane().add(jPanel,new BorderLayout().CENTER); setBounds(100, 100, 1276, 850); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args) { new tt(); } }