Button does not appear in window

1

My button called btnLimpar does not appear. This is the code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Frame extends JFrame {
    private JButton btnEnviar = new JButton("EnviarMSG");
    private JButton btnLimpar = new JButton("Limpar");
    private JTextField txtCampo = new JTextField();
    private JLabel lblExibir = new JLabel();

    public Frame() {
        super("Meu JFrame executado em terminal");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(getOwner());
        setSize(400, 330);
        setVisible(true);
        setLayout(null);

        txtCampo.setFont(new Font("arial", Font.PLAIN,40));
        txtCampo.setBounds(15, 20, 360, 70);
        add(txtCampo);

        btnEnviar.setBounds(210 ,110, 170, 70);
        add(btnEnviar);

         btnEnviar.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

          lblExibir.setText(txtCampo.getText());

            }
        });

        btnLimpar.setBounds(210 ,110, 170, 70);
        add(btnLimpar);

        btnLimpar.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                lblExibir.setText(null);
                txtCampo.setText(null);
                txtCampo.requestFocus();                        
            }       
        });

        lblExibir.setFont(new Font("arial", Font.PLAIN, 20));
        lblExibir.setOpaque(true);
        lblExibir.setBackground(Color.gray);
        lblExibir.setForeground(Color.red);
        lblExibir.setBounds(15, 220, 360, 70);
        add(lblExibir);
    }

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

Print

    
asked by anonymous 24.03.2015 / 21:12

1 answer

1

One button is on top of the other, because when creating them you have defined the same position of them inside your JFrame:

btnEnviar.setBounds(210 ,110, 170, 70);

...

btnLimpar.setBounds(210 ,110, 170, 70);

One possible solution is to change the position of btnLimpar like this:

btnLimpar.setBounds(20 ,110, 170, 70);

Where the parameters are:

setBounds(int x, int y, int largura, int altura);

x and y are the coordinates of the position plane of the element.

Result:

  

    
24.03.2015 / 21:36