Easy add JLabel to JPanel

I have a simple problem since I am not very familiar with the Java GUI. I am trying to make JLable visible in the code below, as it is difficult for me to understand the concept. But the mark is not yet visible, but the frame opens at runtime.

public class Sample extends JPanel { public void Sample() { JPanel p = new JPanel(); JLabel lab1 = new JLabel("User Name", JLabel.LEFT); p.setLayout(new FlowLayout()); p.add(lab1 = new JLabel("add JLabel")); } public static void main(String[] args) { JFrame frame = new JFrame(); frame.getContentPane().add(new Sample()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(200, 200); frame.setVisible(true); } } 
+4
source share
5 answers

You forgot to add the p panel to the sample. Either use add(p) at the end, or just remove the p panel because your class class extends JPanel.

Option 1:

  JPanel p = new JPanel(); JLabel lab1 = new JLabel("User Name", JLabel.LEFT); p.setLayout(new FlowLayout()); p.add(lab1 = new JLabel("add JLabel")); add(p); 

option 2:

  JLabel lab1 = new JLabel("User Name", JLabel.LEFT); setLayout(new FlowLayout()); add(lab1 = new JLabel("add JLabel")); 

Also why are you overriding JLabel initialization? In your code, JLable will always contain the value "add JLabel". If you want to see the "Username", use add(lab1); instead of add(lab1 = new JLabel("add JLabel")); .

Perhaps you just need to:

  JLabel lab1 = new JLabel("User Name", JLabel.LEFT); setLayout(new FlowLayout()); add(lab1); 

Also, the constructor cannot have a return type, so remove void from your constructor.

+19
source

The constructor you are using is not a suitable constructor. The java constructor has no return type, and void is optional. When in the main method you call the new Sample (), it does not actually call your method, but the default constructor, which exists by default.

Try it like this.

 public Sample() { JPanel p = new JPanel(); JLabel lab1 = new JLabel("User Name", JLabel.LEFT); p.setLayout(new FlowLayout()); p.add(lab1 = new JLabel("add JLabel")); } 

also you need to do what @Harry Joy suggested adding add(p); otherwise the panel will not be added.

+2
source

Pay attention to the comments.

 import java.awt.*; import javax.swing.*; public class Sample extends JPanel { public Sample() { // set the layout in the constructor super(new FlowLayout(FlowLayout.LEADING)); // best not to set size OR preferred size! setPreferredSize( new Dimension(200,200) ); JLabel lab1 = new JLabel("User Name"); add(lab1); } public static void main(String[] args) { // construct the GUI on the EDT SwingUtilities.invokeLater( new Runnable() { public void run() { JFrame frame = new JFrame("User Details"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new Sample()); // important! frame.pack(); frame.setVisible(true); } }); } } 

Also note that it is generally not recommended to distribute a component without adding custom functions. This would mean (for example) defining new methods for the Sample panel (which could be better denoted as UserDetails or UserDetailsContainer , if I correctly assume that you are going with this code ..). Or it could be a Login component.

+2
source

The sample is Jpanel.

The sample extends JPanel means you inherit JPanel.

just uncheck JPanel p and all your "p." s

0
source

@SuppressWarnings ("sequential") public class MyGui extends JFrame {

 private MyContentPane myContentPane = new MyContentPane(); public MyGui(){ super("title"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setContentPane(myContentPane); this.createMenu(); this.pack(); this.setVisible(true); } private void createMenu(){ JMenuBar myMenuBar = new JMenuBar(); JMenu xMenu = new JMenu("x"); JMenu x = new JMenu("x"); JMenuItem xItem = new JMenuItem("letter"); JMenuItem exitItem = new JMenuItem("exit"); xItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { myContentPane.xPanel(); } }); xItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0) { myContentPane.setxPanel(); } }); exitItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { System.exit(0); } }); displayMenu.add(letterItem); displayMenu.add(colorItem); fileMenu.add(exitItem); myMenuBar.add(displayMenu); myMenuBar.add(fileMenu); this.setJMenuBar(myMenuBar); } 

}

0
source

All Articles