Reputation: 861
Can you help me if possible please? I am doing my own little 'Movies' project, whilst trying to learn Java.
I have managed to create a JLabel that displays my text when a button is clicked, its basically about my favorite movies. So, when I click on the Commando button, a brief synopsis of it pops up. So I decided to add an image, which is located in the same folder where my Java Package is.
I have researched on how to apply my image, so that it shows at the same time as the text, but it does not seem to be working. I still have a lot to learn, and any guidance will be really appreciated!!
Thanks in advance for any help!! ;]
Daz.
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextPane;
import java.awt.event.*;
import javax.swing.ImageIcon;
public class Movies {
JFrame frame;
JLabel label;
public static void main(String []args){
Movies gui = new Movies();
gui.go();
}
public void go(){
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton commandoButton = new JButton("Commando");
commandoButton.addActionListener(new LabelListener());
frame.setLayout(null); // equivalent to frame.getContentPane().setLayout(null);
frame.add(commandoButton); // equivalent to frame.getContentPane().add(button);
commandoButton.setBounds(10, 10, 150, 25);
JButton predatorButton = new JButton("Predator");
predatorButton.addActionListener(new LabelListenerA());
frame.setLayout(null); // equivalent to frame.getContentPane().setLayout(null);
frame.add(predatorButton); // equivalent to frame.getContentPane().add(button);
predatorButton.setBounds(10, 40, 150, 25);
JButton expendablesButton = new JButton("The Expendables");
expendablesButton.addActionListener(new LabelListenerB());
frame.setLayout(null); // equivalent to frame.getContentPane().setLayout(null);
frame.add(expendablesButton); // equivalent to frame.getContentPane().add(button);
expendablesButton.setBounds(10, 70, 150, 25);
frame.getContentPane().add(BorderLayout.WEST, commandoButton);
frame.setSize(1000, 350);
frame.setVisible(true);
}
JTextPane labelCommando = new JTextPane();
class LabelListener implements ActionListener {
public void actionPerformed(ActionEvent commandoButton){
labelCommando.setText("A retired elite Black Ops Commando launches a one man war against a group of South American criminals who have kidnapped his daughter to blackmail him into starting a revolution and getting an exiled dictator back into power. ");
frame.add(labelCommando);
labelCommando.setBounds(170, 10, 500, 200);
labelCommando.setOpaque(false);
String imgStr = "images/commando.jpg";
ImageIcon image = new ImageIcon("commando.jpg");
JLabel labelCommando = new JLabel(" ", image, JLabel.CENTER);
label.setIcon(image);
}
} // Close inner class
class LabelListenerA implements ActionListener {
public void actionPerformed(ActionEvent predatorButton){
labelCommando.setText("Not long after they land, Dutch and his team discover that they have been sent under false pretenses. This deception turns out to be the least of their worries though, when they find themselves being methodically hunted by something not of this world...");
frame.add(labelCommando);
labelCommando.setBounds(170, 10, 500, 200);
labelCommando.setOpaque(false);
}
} // Close inner class
class LabelListenerB implements ActionListener {
public void actionPerformed(ActionEvent expendablesButton){
labelCommando.setText("Barney Ross leads the 'Expendables', a band of highly skilled mercenaries including knife enthusiast Lee Christmas, martial arts expert Yin Yang, heavy weapons specialist Hale Caesar, demolitionist Toll Road and loose-cannon sniper Gunner Jensen. When the group is commissioned by the mysterious Mr. Church to assassinate the merciless dictator of a small South American island, Barney and Lee head to the remote locale to scout out their opposition. Once there, they meet with local rebel Sandra and discover the true nature of the conflict engulfing the city. When they escape the island and Sandra stays behind, Ross must choose to either walk away and save his own life - or attempt a suicidle rescue mission that might just save his soul.");
frame.add(labelCommando);
labelCommando.setBounds(170, 10, 500, 200);
labelCommando.setOpaque(false);
}
} // Close inner class
}
Upvotes: 0
Views: 489
Reputation: 168825
..which is located in the same folder where my Java Package is.
This simple statement indicates a number of misunderstandings that might be at the root of the problem.
ZipEntry
in a Jar. How to access the images comes down to an important question. Are the images an application resource?
If the app. is for anyone besides you, they should be included in the Jar as an 'embedded application resource' for easy distribution. Those are accessed by URL
.
If the app. is only for you, a hard-coded path to the directory containing the images should suffice. By passing a String
argument to the ImageIcon
constructor, we indicate that we want a File
based on that path, but a 'relative path' rarely works (unless the developer takes special effort to ensure the image is available at that path relative to the ..current directory). Instead, it would be better to hard-code an absolute path to the directory containing the images, then add the image file name at run-time.
There are also a number of other errors in the code, that have mostly been addressed in other answers, but again. Use Layouts to manage component sizing and positioning.
It seems a JTable
or JList
might be better than a bunch of buttons, as well.
Upvotes: 1
Reputation: 347194
I've scanned through your code and can't find anywhere that you are creating (or adding) label
, now assuming that you're not receiving any errors when you run the program, this would suggest that the code snippet is incomplete.
Make sure that the label
has been create and added to the screen some where.
A call to invalidate
and repaint
would probably also help, but it's difficult to be sure without a code sample that works.
I don't know why people insist on using null layout managers, but I'd suggest getting an test working with layout managers first, that at least removes the doubt that the components haven't been layout correctly.
Upvotes: 1
Reputation: 109813
JLabel label;
isn't intialized have to JLabel label = new JLabel();
JLabel
isn't added to the container, then code nothing shows in the GUI
use Standard LayoutManager instead of setBounds()
use JScrollPane for JTextArea
Upvotes: 1
Reputation: 309
Two issues I've seen:
Upvotes: 1