How can I change the position of my Jbutton in Swing?

Asked

Viewed 287 times

0

Hello, I’m a beginner and very layman in Java, I was creating a program, and in this program had a button, I wanted to know how I change some button of place in Swing, if someone can help me, I thank :)

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;

public class inicio{

    public static void main(String[] args){

        JFrame frame = new JFrame("Tela de incio");
        JPanel painel = new JPanel();
        JButton confirm = new JButton("CONFIRMAR");
        frame.add(painel);
        painel.add(confirm);
        confirm.setBounds(10,10,10,10);
        frame.setVisible(true);


     }

}

even when I put "setBounds(example, example);" nothing happens the code I used was the following

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;


public class inicio{

     public static void main(String[] args){

         JFrame frame = new JFrame("Tela de incio");
         JPanel painel = new JPanel();
         JButton confirm = new JButton("CONFIRMAR");
         frame.add(painel);
         painel.add(confirm);
         frame.setLayout(null);
         painel.setLayout(null);
         confirm.setLayout(null);
         confirm.setLocation(50,50);
         frame.setVisible(true);


    }

}
  • Enter the code of what you have tried in the question.

  • 1

    I put, but even I put only one button;-;

  • In the three components (frame , painel and confirm ) calling setLayout(null); after initiated before making any visual operation. To position the button use confirm.setLocation(0,0);

  • I’m running the code and nothing appears on the screen

  • I’ll ask the question how the code looked and how the screen looks;-;

  • The setLayout(null); have to come before adding the components. Also put this here: frame.setPreferredSize(new Dimension(500, 500)); and this here frame.pack(); before Visible set.

  • Java Swing has a few tricks, and the documentation says leave to use setLayout(null) only in the last case. It is preferable to work with layouts and combine them. FlowLayout, BoxLayout and BorderLayout for example make interesting combinations. I suggest reading the tutorials of Oracle, if you do not find the option of reading in Portuguese, and learn how to deal with them mainly, among others you can learn, associated with the generic container JPanel.

  • Java Swing has a few tricks, and the documentation says leave to use setLayout(null) only in the last case. It is preferable to work with layouts and combine them. FlowLayout, BoxLayout, BorderLayout and GridLayout for example make interesting combinations, associated with generic container JPanel. I suggest reading the tutorials of Oracle, if you do not find the option of reading good quality in Portuguese, and learn how to work with them, among others that you may want to learn.

Show 3 more comments

1 answer

0

try to create things in reverse, for example from the inside out. set first the button and its properties (such as the location, setLocation(x,y)) and after all its set properties then include in the frame/panel (in your case painel.add(confirm)).. the x and y locations set in the button setLocation will be used to define the location where it will appear in the panel. Remember to give a painel.repaint(); in the end.

Browser other questions tagged

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