When I type .setLayout(new BorderLayout()); It appears me this: The method setLayout(LayoutManager) in the type JFrame is not applicable for the arguments (BorderLayout)
I´m a beginner and I was following a video but this does not work and I already watched different videos, thank you so much for your help.
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BorderLayout {
public static void main(String []args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setLayout(new BorderLayout());
frame.setVisible(true);
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
panel1.setBackground(Color.red);
panel2.setBackground(Color.green);
panel3.setBackground(Color.yellow);
panel4.setBackground(Color.magenta);
panel5.setBackground(Color.blue);
panel1.setPreferredSize(new Dimension(100, 100));
panel2.setPreferredSize(new Dimension(100, 100));
panel3.setPreferredSize(new Dimension(100, 100));
panel4.setPreferredSize(new Dimension(100, 100));
panel5.setPreferredSize(new Dimension(100, 100));
frame.add(panel1, BorderLayout.NORTH);
frame.add(panel2, BorderLayout.WEST);
frame.add(panel3, BorderLayout.EAST);
frame.add(panel4, BorderLayout.SOUTH);
frame.add(panel5, BorderLayout.CENTER);
}
}
CodePudding user response:
Move frame.setVisible(true);
to the end of your method. Swing layouts are lazy, they won't "magically" update by themselves, instead, you need to tell it when you want a container to be updated, using revalidate
and repaint
to schedule a new layout and pass pass, but, as I've said, the easiest thing to do in your case, is simply setup the window before you make it visible.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.EventQueue;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
panel1.setBackground(Color.red);
panel2.setBackground(Color.green);
panel3.setBackground(Color.yellow);
panel4.setBackground(Color.magenta);
panel5.setBackground(Color.blue);
panel1.setPreferredSize(new Dimension(100, 100));
panel2.setPreferredSize(new Dimension(100, 100));
panel3.setPreferredSize(new Dimension(100, 100));
panel4.setPreferredSize(new Dimension(100, 100));
panel5.setPreferredSize(new Dimension(100, 100));
frame.add(panel1, BorderLayout.NORTH);
frame.add(panel2, BorderLayout.WEST);
frame.add(panel3, BorderLayout.EAST);
frame.add(panel4, BorderLayout.SOUTH);
frame.add(panel5, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
});
}
}