Reputation: 11
Hello everyone i wanted to ask, i have this program and it has 6buttons each going to show something different, but i wanted to know how can i make it show a picture when i click "Picture 1" also where should those pictures be located so the program knows where to find them? Thanks hope you can help: p.s. i use netbeans
import java.awt.Toolkit;
import java.awt.Dimension;
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Gui {
static JFrame aWindow = new JFrame("This Is Me:");
public static void main(String[] args) {
int windowWidth = 600;
int windowHeight = 500;
aWindow.setBounds(500,500, windowWidth, windowHeight);
aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout flow = new FlowLayout();
Container content = aWindow.getContentPane();
content.setLayout(flow);
for(int i = 1; i <= 5; i++) {
content.add(new JButton("Picture " + i));}
String path = "__2 copy.jpg";
aWindow.setVisible(true);
}
}
Upvotes: 0
Views: 197
Reputation: 347214
how can i make it show a picture when i click "Picture 1"?
You need to first attach a ActionListener
to the button. Within the actionPerformed
method you then need to load the image.
The best way to load images is using the ImageIO
API. How this is achieved depends on how the images are stored.
If the images are stored within the application Jar (bundled resources, see below), you would use something like...
ImageIO.read(getClass().getResource("/path/to/image.jpg");
If they stored externally...
ImageIO.read(new File("relative/path/to/image.jpg");
Once the image is loaded, you can use an ImageIcon
to wrap the image loaded by ImageIO
and apply it to a JLabel
.
See...
For more details.
You could, of course, create your own image component that displayed the image, but that's a reasonably advance topic and I'm not sure if that's what you want.
where should those pictures be located so the program knows where to find them?
This depends. Do you want the images to ALWAYS be available to the program or do you want to change them?
If the images aren't likely to change very often, you can embed them as internal resources to the Jar file. How this is done depends on how you are building the application, but in general terms, you create a directory within your projects source and place the images there. You can then reference the images via the Classloader.
If you want the ability to change the images quickly (and without needing to re-build the application), then I would store them in the same directory as the application Jar. This would allow you to reference them as File
s from a relative location (ie new File("Image.jpg");
)
Updated with example
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestImages {
public static void main(String[] args) {
new TestImages();
}
public TestImages() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JLabel lblPic;
public TestPane() {
setLayout(new BorderLayout());
JPanel buttons = new JPanel(new FlowLayout(FlowLayout.CENTER));
JButton btnFile = new JButton("Load from file");
JButton btnResource = new JButton("Load from resource");
buttons.add(btnFile);
buttons.add(btnResource);
btnFile.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
BufferedImage image = ImageIO.read(new File("Pony01.png"));
lblPic.setIcon(new ImageIcon(image));
} catch (Exception exp) {
JOptionPane.showMessageDialog(TestPane.this, "Failed to load image", "Fail", JOptionPane.ERROR_MESSAGE);
exp.printStackTrace();
}
}
});
btnResource.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
BufferedImage image = ImageIO.read(getClass().getResource("/Pony02.png"));
lblPic.setIcon(new ImageIcon(image));
} catch (Exception exp) {
JOptionPane.showMessageDialog(TestPane.this, "Failed to load image", "Fail", JOptionPane.ERROR_MESSAGE);
exp.printStackTrace();
}
}
});
lblPic = new JLabel();
lblPic.setVerticalAlignment(JLabel.CENTER);
lblPic.setHorizontalAlignment(JLabel.CENTER);
add(lblPic);
add(buttons, BorderLayout.NORTH);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.dispose();
}
}
}
You'll, obviously, need to supply your own images. The embedded resource should life in the top level folder of your source (commonly known as the default
package)
Upvotes: 4