Hi fellow programmers! I am trying to add two JLabel to a JFrame, but the second add method that added the label seems to overwrite my first add method. I tried to solve this problem using two different label variables and using the setLocation method, which provides different coordinates for each label. But I canβt solve it. Why can't I add two tags to my program? By the way, I am not getting any errors. It seems more like a logical mistake that I cannot solve.
Here is my current code:
import javax.swing.*; public class test { private static JLabel label; private static JLabel label1; public static void main(String[] args){ initializeLabel(); initializeImage(); initializeFrame(); } private static void initializeLabel(){ label = new JLabel(); label.setText("hi"); label.setLocation(54,338); } private static void initializeImage(){ label1 = new JLabel(); label1.setText("sss"); label1.setLocation(55, 340); } private static void initializeFrame(){ JFrame frame = new JFrame(); frame.add(label1); frame.add(label); frame.setVisible(true); } }
source share