Reputation: 1
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);
}
Upvotes: 0
Views: 144
Reputation: 13479
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);
}
Upvotes: 3
Reputation: 2702
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)
Upvotes: -1