Hey everyone, it's been awhile since I've posted here, I was hoping that someone could help me figure out what's wrong with my program which I've been bruising my head over for the last two days or so. The following program (which compiles fine) is supposed to read a jpeg/gif from a chooseFile dialogue and display it in a frame I've drawn. If you comment out the whole "paint" method you can run it and see what the interface is SUPPOSED to look like, though if you try to open a picture it'll just return the filename. If you run it as is, the first thing you'll notice is that the buttons disappear along with just about everything else in the interface. If you move your mouse around the frame it'll uncover the three buttons along the bottom and you can click "OPEN." It'll even display the picture if you open one, but basically everything else disappears. Does anyone know what's wrong or, possibly, what's missing from the program that screws up the interface like that?
Thanks for reading this post.
package picviewer;
import java.awt.*;
import java.awt.event.*;
import java.awt.Toolkit;
import java.awt.image.*;
import java.awt.image.BufferedImage;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.filechooser.*;
public class Body extends JFrame implements ActionListener
{
private JTextField jtfNum1, jtfNum2, jtfNum3, jtfNum4;
private JButton jbtOpen;
private JButton jbtSave;
private JButton jbtOk;
JPanel p1 = new JPanel();
Image image;
public void createImage(String a)
{
Image pic = Toolkit.getDefaultToolkit().getImage(a);
image = pic;
}
public static void main(String[] args)
{
Body frame = new Body();
frame.setSize(495,245);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
//COMBO BOX STUFF HERE
public Body()
{
setTitle("Edit Image File");
JPanel Picture = new JPanel();
Picture.setLayout(new BorderLayout());
Picture.add(new JLabel(" "), BorderLayout.WEST);
Picture.add(new JLabel(" "), BorderLayout.CENTER);
Picture.add(new JLabel(" "), BorderLayout.SOUTH);
Picture.add(new JLabel(" "), BorderLayout.EAST);
p1.setLayout(new FlowLayout());
p1.setBorder(new TitledBorder(new LineBorder(Color.black, 1)," Image "));
p1.add(Picture);
JPanel p2 = new JPanel();
p2.add(new JLabel("Image Width:"));
p2.add(jtfNum1 = new JTextField(6));
p2.add(new JLabel("Image Height:"));
p2.add(jtfNum2 = new JTextField(6));
p2.setBorder(new TitledBorder(new EtchedBorder()," Source Image "));
jtfNum1.setEditable(false);
jtfNum2.setEditable(false);
JPanel p3 = new JPanel();
p3.add(new JLabel("Image Width:"));
p3.add(jtfNum3 = new JTextField(6));
p3.add(new JLabel("Image Height:"));
p3.add(jtfNum4 = new JTextField(6));
p3.setBorder(new TitledBorder(new EtchedBorder()," Formatted Image "));
jtfNum3.setEditable(false);
jtfNum4.setEditable(false);
JPanel p4 = new JPanel();
p4.setLayout(new BorderLayout());
p4.add(p2, BorderLayout.NORTH);
p4.add(p3, BorderLayout.SOUTH);
JPanel p5 = new JPanel();
p5.add(new JLabel(" "), BorderLayout.WEST);
p5.add(jbtOpen = new JButton("Open"));
p5.add(jbtSave = new JButton("Save"));
p5.add(jbtOk = new JButton("Ok"));
getContentPane().setLayout(new BorderLayout());
getContentPane().add(p1, BorderLayout.WEST);
getContentPane().add(p4, BorderLayout.EAST);
getContentPane().add(p5, BorderLayout.SOUTH);
jbtOpen.addActionListener(this);
jbtSave.addActionListener(this);
jbtOk.addActionListener(this);
}
public void paint(Graphics g)
{
//p1.repaint();
MediaTracker track = new MediaTracker(this);
track.addImage(image, 1);
try
{
track.waitForID(1);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
g.drawImage(image, 10, 40, p1);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == jbtOpen)
{
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(this);
{
if (returnVal == JFileChooser.APPROVE_OPTION)
{
String a = (chooser.getSelectedFile().getPath());
System.out.print(a);
createImage(a);
}
}
}
if (e.getSource() == jbtOk)
{
System.exit(0);
}
}
}
Thanks for reading this post.