-
August 8th, 2013, 07:22 PM
#1
java image display
Simple question here I basically want to display an image within the frame from the click of a button. My problem is getting that image to display. Here is the code Thanks...
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyImage extends JFrame
{
private JButton open;
private JPanel mainPanel;
public MyImage()
{
setTitle("Open Image");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setSize(700,700);
open = new JButton("open");
open.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
ImageIcon zz = new ImageIcon("barkley.jpg");
//mainPanel.add(zz);
setVisible(true);
pack();
}
});
mainPanel = new JPanel();
getContentPane().add(mainPanel);
mainPanel.setLayout(null);
mainPanel.add(open);
mainPanel.add(zz);
open.setBounds(0,0,100,100);
}
public static void main(String[] args)
{
MyImage mi = new MyImage();
}
}
-
August 15th, 2013, 12:09 AM
#2
Re: java image display
Why is //mainPanel.add(zz); commented out?
-
August 15th, 2013, 12:54 AM
#3
Re: java image display
i commented out mainPanel.add(zz) because i thought since i had it elsewhere in my code i didn't need it to be there. But i found the solution to my problem. PS, I know my current solution is sloppy but at least i got it to work for me
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyImage extends JFrame
{
private JButton open, remove;
private JPanel mainPanel;
private JLabel imgLabel;
public MyImage()
{
setTitle("Open Image");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setSize(700,700);
open = new JButton("open");
open.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
ImageIcon zz = new ImageIcon("barkley.jpg");
//imgLabel = new JLabel();
imgLabel.setIcon(zz);
mainPanel.add(imgLabel);
// mainPanel.add(new JLabel(zz));
//imgLabel.setBounds(50,50,200,200);
//mainPanel.validate();
//mainPanel.repaint();
//imgLabel.setVisible(true);
//mainPanel.add(imgLabel);
//setVisible(true);
//pack();
}
});
remove = new JButton("remove");
remove.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
imgLabel.setVisible(false);
}
});
imgLabel = new JLabel();
mainPanel = new JPanel();
getContentPane().add(mainPanel);
mainPanel.setLayout(null);
mainPanel.add(open);
//mainPanel.add(imgLabel);
mainPanel.add(remove);
imgLabel.setBounds(50,50,400,400);
open.setBounds(0,0,100,100);
remove.setBounds(100,50,100,100);
}
/*public void paintComponent(Graphics g)
{
if(this.imgLabel!=null)
{
g.drawImage(imgLabel, 0,0,null);
}
} */
public static void main(String[] args)
{
MyImage mi = new MyImage();
}
}
-
March 7th, 2014, 02:16 AM
#4
Re: java image display
Originally Posted by jumpman8947
Simple question here I basically want to display an image within the frame from the click of a button. My problem is getting that image to display.
Google is a good friend. Check following links:
http://www.coderanch.com/t/427715/GU...-image-Trouble
http://stackoverflow.com/questions/1...ing-java-swing
-
March 8th, 2014, 03:05 AM
#5
Re: java image display
If you feel your problem has been solved, please mark your thread Resolved.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|