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.
– Augusto Vasques
I put, but even I put only one button;-;
– Cerf Pascal
In the three components (
frame
,painel
andconfirm
) callingsetLayout(null);
after initiated before making any visual operation. To position the button useconfirm.setLocation(0,0);
– Augusto Vasques
I’m running the code and nothing appears on the screen
– Cerf Pascal
I’ll ask the question how the code looked and how the screen looks;-;
– Cerf Pascal
The
setLayout(null);
have to come before adding the components. Also put this here:frame.setPreferredSize(new Dimension(500, 500));
and this hereframe.pack();
before Visible set.– Augusto Vasques
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
andBorderLayout
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 containerJPanel
.– Piovezan
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
andGridLayout
for example make interesting combinations, associated with generic containerJPanel
. 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.– Piovezan