I am new to using swing. I am trying to get a picture to be displayed in a Jframe when a button is pressed. I cannot figure out how to make this work. This is what I have so far:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
public class JTresume extends JFrame
{
public static void main (String[] args)
{
final String picPath = "C:/Users/Joe/Pictures/me.jpg";
// Set up JFrame
JTresume f = new JTresume();
f.setTitle("Resume");
f.setSize(300, 300);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
// Set up two JPanels and add to JFrame
JPanel p1 = new JPanel();
p1.setBackground(Color.red);
f.add(p1, BorderLayout.NORTH);
// Add Buttons to JPanel 1
JButton button1 = new JButton("Picture");
p1.add(button1);
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(null, "Picture Pressed");
getImage(picPath);
}
});
} // End MAIN
private static void getImage(String x)
{
try
{
ImageIcon img = new ImageIcon(x);
JLabel label = new JLabel("", img, JLabel.CENTER);
JPanel p2 = new JPanel(new BorderLayout());
p2.setLayout(null);
p2.add(label, BorderLayout.CENTER);
}catch(Exception ie){
JOptionPane.showMessageDialog(null, "Error! " + ie.getMessage());
}
}
} // END
Thanks to anyone that can help.