Buttons do not display correctly

1

I'm trying to add 2 JButtons to a program, I make the whole process to add, but it 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();
}

}
    
asked by anonymous 21.10.2017 / 16:38

1 answer

3

First you're adding a button practically on top of the other, this is the problem of using absolute layout without being absolutely sure how to use it and its consequences. The setBounds method 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 remaining two are its width and height. Only by adjusting the position of the second according to the height of the first, you can display both. But I insist on recommending:

  

Avoid using absolute layout unless you are in dire need and know the consequences , because absolute layout makes it difficult to maintain the screen and make your application look different depending on the monitor and resolution being executed.

     

There are several layouts managers for you to you do not have to worry about manual positioning or organization of components. Not to mention that the use of layouts makes your code easier to maintain than inserting a lot of setbounds , and in case you need to change the position of some component, you will have to position all of them manually.

Then you override the paint method, and the documentation says is not advisable override this method in swing programs. If you want to make drawings on the screen, overwrite the paintComponent method of some JPanel and add it to the screen. The reason for a button not being displayed is precisely the fact that this method has been overwritten.

Another problem I noticed is that you did not forward the screen to EDT , I recommend that read here the importance of this and the consequences that may occur when you do not do this and this answer shows you some ways to start the application within this Thread.

There are more problems in this code, but it is beyond doubt to get everything sorted out, 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());
    }

}
    
21.10.2017 / 16:58