谁能用JAVA给我写个程序点开文件选择器JFileChooser,在JPanel上显示所显示的图片
图片大小要适应JPanel大小,最好是拉伸JPanel的时候图片也会跟着变化大小适应JPanel的大小publicstaticvoidmain(Stringargs[])...
图片大小要适应JPanel大小,最好是拉伸JPanel的时候图片也会跟着变化大小适应JPanel的大小
public static void main(String args[]) {
JFrame1 jf =new JFrame1();
JPanel jp=new JPanel();
jp.setBackground(Color.red);
jf.getContentPane().add(jp);
jf.setLocationRelativeTo(null);
jf.setVisible(true);
});
}
你能帮我看下我写的哪有问题,为什么JPanel加载不上去 展开
public static void main(String args[]) {
JFrame1 jf =new JFrame1();
JPanel jp=new JPanel();
jp.setBackground(Color.red);
jf.getContentPane().add(jp);
jf.setLocationRelativeTo(null);
jf.setVisible(true);
});
}
你能帮我看下我写的哪有问题,为什么JPanel加载不上去 展开
展开全部
package p1;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;
public class MyPanel extends JPanel
{
private static final long serialVersionUID = 1L;
BufferedImage bi = null;
public MyPanel ()
{
final JFileChooser chooser = new JFileChooser ();
FileNameExtensionFilter filter = new FileNameExtensionFilter ("Some Images", "jpg", "gif", "png", "jpeg");
chooser.setFileFilter (filter);
JButton jb = new JButton ("选择图片");
jb.addActionListener (new ActionListener ()
{
@Override
public void actionPerformed ( ActionEvent e )
{
int returnVal = chooser.showOpenDialog (MyPanel.this);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
try
{
bi = ImageIO.read (new File (chooser.getSelectedFile ().getAbsolutePath ()));
repaint ();
}
catch (IOException e1)
{
e1.printStackTrace ();
}
}
}
});
JFrame jFrame = new JFrame ();
jFrame.setLayout (new BorderLayout ());
jFrame.setSize (300, 200);
jFrame.add (this, BorderLayout.CENTER);
jFrame.add (jb, BorderLayout.SOUTH);
jFrame.setLocationRelativeTo (null);
jFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
jFrame.setVisible (true);
}
@Override
public void paint ( Graphics g )
{
super.paint (g);
g.drawImage (bi, 0, 0, this.getWidth (), this.getHeight (), null);
g.dispose ();
}
public static void main ( String[] args )
{
new MyPanel ();
}
}
追问
我已经有了JFrame,希望把你写的这个Panel加到JFrame上去,怎么写,谢谢
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询