Thursday 26 October 2017

swing - JPanel won't show up in JFrame(main class) - Beginner Java

I'm just starting out swing in Java... I have this problem
about creating a separate JPanel class to be put in the main class (the one with
JFrame), that the JPanel won't show up in the main class. The program can run but only
the frame would show up. I was hoping that I would see the panel with the 'hallo' label
but no.



I know I've been looking for the other
solutions in this site, but I didn't really get some of it.



This is my JPanel
class:



import
javax.swing.*;

public class CreatePanel extends
JPanel

{
private JPanel panel = new JPanel();

private JLabel narrate;

public void setNarrate(String
label)
{
narrate = new JLabel(label);

panel.add(narrate);
panel.setVisible(true);

}


public JPanel getPanel()
{

return panel;

}
}


This is
the main class with the
JFrame:




import
javax.swing.*;

public class Maine extends
JFrame
{
private static JFrame frame = new
JFrame();

public static void main(String[] args)

{
new Maine();


CreatePanel panel1 = new
CreatePanel();

panel1.setNarrate("Hallo");

panel1.getPanel();
frame.add(panel1);
}


public Maine()
{
frame.setTitle("Detective
Game");

frame.setSize(500,500);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);
frame.setLocationRelativeTo(null);

frame.setIconImage(new ImageIcon("agent.png").getImage());

}
}

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...