import java.awt.*;
import java.awt.event.*;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MAIN_FILE extends JPanel implements ActionListener
{
public void paintComponent(Graphics g)
{
int row = 0;
int col = 0;
int x, y;
int colMax = 29;
int rowMax = 41;
super.paintComponent(g);
ImageIcon ground = new ImageIcon("C:\\Programming\\Ground.jpg");
for(col = 0; col <= colMax; col++)
{
for(row = 0; row <= rowMax; row++)
{
x = row * 30;
y = col * 30;
ground.paintIcon(this, g, x, y);
}
}
ImageIcon wall = new ImageIcon("C:\\Programming\\WallTest0000.jpg");
}
public static void main(String[] args)
{
JPanel JP = new JPanel();
JP.setVisible(true);
JFrame jf = new JFrame();
jf.setTitle("Dungeon Thing");
jf.setSize(1230, 870);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(JP);
}
Asked
Active
Viewed 81 times
0

Andrew Thompson
- 168,117
- 40
- 217
- 433
-
1I'm noticing that the `main()` method never uses the class it's in (`MAIN_FILE`). – tar May 08 '14 at 01:22
-
Java class names should be camelcase nouns. Accordingly, `MAIN_FILE` should be renamed to `Main`, or `MainFile` at the very least. – tar May 08 '14 at 01:24
-
`ImageIcon ground = new ImageIcon("C:\\Programming\\Ground.jpg");` By the time of deployment, those resources will likely become an [tag:embedded-resource]. That being the case, the resource must be accessed by `URL` instead of `File`. See the [info page](http://stackoverflow.com/tags/embedded-resource/info) for the tag, for a way to form an `URL`. Also, we should never be trying to load image in a painting method. They should be loaded at start-up and cached as class attributes. – Andrew Thompson May 08 '14 at 01:35
2 Answers
3
After adding the JPanel
, you need to call revalidate()
and repaint()
on the JFrame
.
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(300,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JLabel("Label"));
frame.revalidate();
frame.repaint();
}
Note, if you added the component to the frame before calling setVisible
like David said, you wouldn't need revalidate()
or repaint()
:
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new JLabel("Label"));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

ryvantage
- 13,064
- 15
- 63
- 112
-
1`frame.setLocationRelativeTo(null);` For frame positioning, you cannot go by `setLocationByPlatform(true)`. See [this answer](http://stackoverflow.com/a/7143398/418556) for demo. `frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);` Better to use `JFrame.DISPOSE_ON_CLOSE`.. +1 otherwise. – Andrew Thompson May 08 '14 at 01:52
-
-
OP called for `EXIT_ON_CLOSE` but not any frame positioning, so I should've omitted that. – ryvantage May 08 '14 at 13:46
-1
Once you call jf.setVisible(true)
, the event dispatch thread will be locked, waiting for something to happen.
The call to jf.add(JP)
doesn't even happen until after the window has been disposed of.
Try reordering your operations in main to add the JPanel
to the JFrame
first, then setVisible(true)

user657267
- 20,568
- 5
- 58
- 77

David
- 2,602
- 1
- 18
- 32
-
This is untrue. `public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(300,300); frame.setLocationRelativeTo(null); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); System.out.println("Here"); }` – ryvantage May 08 '14 at 01:24
-
And another: `public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(300,300); frame.setLocationRelativeTo(null); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new JLabel("Label")); frame.revalidate(); frame.repaint(); }` – ryvantage May 08 '14 at 01:25
-
@TAsk The statement in the first paragraph of this answer only applies to modal windows such as a modal dialog or an option pane. The rest is wrong because of that error. – Andrew Thompson May 08 '14 at 01:50