Buttons are not displayed correctly

Asked

Viewed 41 times

1

I am trying to add 2 Jbuttons to a program, I do all the process to add, but shows only one.

package ldegraphic;
import java.awt.Graphics;
import javax.swing.*;

public class LDEGraphic extends JFrame {
    JButton jb = new JButton("Adicionar");
    JButton jb2 = new JButton("Remover");

public LDEGraphic(){       
    setTitle("Lista Duplamente encadeada");
    setSize(900, 600);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);

    setLayout(null);
    jb.setBounds(10, 10, 100, 60);
    add(jb);
    jb2.setBounds(10, 30, 120, 60);
    add(jb2);
}

public void paint(Graphics g){
    g.drawRect(100, 500, 60, 25);
    g.fillRect(100, 500, 60, 25);
}

public static void main(String[] args) {
    new LDEGraphic();
}

}
  • Why are you using absolute layout? There’s a special reason for this?

  • No special reason

1 answer

3

First that you are adding a button practically on top of each other, this is the problem of using absolute layout without being absolutely sure how to use and its consequences. The method setBounds works with coordinates and dimension, where the first two arguments represent, respectively, the position x and y (cartesian plan) of the component on the screen, and the two remaining its width and height. Only by adjusting the position of the second according to the height of the first, it is possible to display both. But I insist on recommending:

Avoid using absolute layout, unless it is of extreme necessity and you know the consequences of it, because absolute layout makes it difficult to maintain the screen and makes your application look different depending on the monitor and resolution that is running.

There are several layouts so you don’t have to worry about positioning or manually organizing components. Not to mention that using layouts makes your code easier to maintain than inserting a lot of setbounds, and if you need to change the position of any component, you will have to position all manually.


Then you overwrite the method paint, and the documentation says that it is inadvisable to overwrite this method in swing programs. If you want to make drawings on the screen, overwrite the method paintComponent of some Jpanel and add it to the screen. The cause of a button not being displayed is precisely the fact that you have overwritten this method.

Another problem I noticed is the fact that you didn’t dispatch the screen to the EDT, recommend that read here the importance of this and the consequences that can occur when this is not done and in this answer shows some ways to start the application within this thread.

There are more problems in this code, but it’s out of the question to pack everything, so I just adapted what was pointed out in the answer.

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import javax.swing.*;

public class LDEGraphic extends JFrame {
    
    
    JButton jb = new JButton("Adicionar");
    JButton jb2 = new JButton("Remover");
    JPanel painel;

    public LDEGraphic() {
        setTitle("Lista Duplamente encadeada");
        setSize(900, 600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);

        painel = new JPanel(){
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);    
                g.setColor(Color.BLACK);
                g.drawRect(100, 300, 60, 25);
                g.fillRect(100, 300, 60, 25);

            }
        };
        
        painel.setLayout(null);
        jb.setBounds(10, 10, 100, 60);
        painel.add(jb);
        jb2.setBounds(10, 70, 120, 60);
        painel.add(jb2);
        getContentPane().add(painel);
        
    }
    

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new LDEGraphic());
    }

}
  • I am very grateful for the answer, I would also like to thank you for the tips, I will read and deepen in the context, again grateful

  • @Danielb in tag swing has several good answers for study, with other explanations that can help you also in learning. Any questions about them, just ask ;)

  • Thank you very much =D

  • @Danielb remembering that if the answer has helped you with the doubt, you can accept it for the matter to be settled.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.