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());
}
}
Why are you using absolute layout? There’s a special reason for this?
– user28595
No special reason
– DanielB