简体   繁体   中英

JFrame labels appear overlapped

i just programmed a program which has a JFrame containing an array of JLabels. The array gets the postition of the single JLabels assigned by a for-loop:

for(int i=0; i<label_entries.length; i++){
    label_entries[i].setLocation(10, i*30);
    label_entries[i].setSize(120,30);
    dialog.add(label_entries[i]);   
}

Do not get confused, my JFrame's name is "dialog". There is one simple problem: The for-loop doesnt work like a for-loop should, and i dont know why, here is the result in my JFrame: Imgur .

Dont care about the single JLabel Entries, the interesting thing is the position of "Telefon". If i set the beginning of the loop to

for(int i=0; i<label_entries.length-1; i++){...}

it is the same problem, just with another JLabel.

I hope you can help me, Greetings from Germany

Edit: Here is the full code:

    JFrame dialog = new JFrame();
    dialog.setBounds(25, 50, 500, 500);
    dialog.setTitle("Eintrag hinzufügen");
    dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    dialog.setVisible(true);

    JLabel[] label_entries = new JLabel[11];
    JTextField[] textfields = new JTextField[11];
    label_entries[0] = new JLabel("Vorname :");
    label_entries[1] = new JLabel("Nachname :");
    label_entries[2] = new JLabel("Nummer :");
    label_entries[3] = new JLabel("Geburtstag :");
    label_entries[4] = new JLabel("Land :");
    label_entries[5] = new JLabel("PLZ :");
    label_entries[6] = new JLabel("Stadt :");
    label_entries[7] = new JLabel("Strasse :");
    label_entries[8] = new JLabel("Hausnummer :");
    label_entries[9] = new JLabel("E-Mail :");
    label_entries[10] = new JLabel("Telefon :");


    for(int i=0; i<label_entries.length; i++){
        label_entries[i].setLocation(10, i*30);
        label_entries[i].setSize(120,30);
        dialog.add(label_entries[i]);   
    }

This should be easier to understand...

One obvious problem is that you are setting absolute positions of your components. Typically a LayoutManager is used for this purpose.

To clear whatever the default layout manager of a JFrame 's content pane is, set it to null just after creating the frame.

JFrame dialog = new JFrame();
dialog.setLayout(null);

As the javadoc of JFrame says:

The default content pane will have a BorderLayout manager set on it.

Javadoc of BorderLayout says:

A border layout lays out a container, arranging and resizing its components to fit in five regions: north, south, east, west, and center. Each region may contain no more than one component, and is identified by a corresponding constant: NORTH , SOUTH , EAST , WEST , and CENTER . When adding a component to a container with a border layout, use one of these five constants, for example:

 Panel p = new Panel(); p.setLayout(new BorderLayout()); p.add(new Button("Okay"), BorderLayout.SOUTH); 

As a convenience, BorderLayout interprets the absence of a string specification the same as the constant CENTER :

 Panel p2 = new Panel(); p2.setLayout(new BorderLayout()); p2.add(new TextArea()); // Same as p.add(new TextArea(), BorderLayout.CENTER); 

Since you call the 1-arg version of add() , all your JLabels are added with BorderLayout.CENTER , and so the last one wins, and the BorderLayout manager then auto-positions it at left-center.

To prevent that from happening, just remove the layout manager:

dialog.setLayout(null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM