关于LayoutManager的问题
import javax.swing.*;import java.awt.*;
public class G extends JFrame
{
public G()
{
setLayout(new GridLayout());
JPanel p1 = new JPanel(new GridLayout(2, 1));
JPanel p2 = new JPanel(new BorderLayout());
p1.add(new JButton("B1"));
p2.add(p1, BorderLayout.EAST);
add(p2, BorderLayout.NORTH);
add(new JButton("B2"), BorderLayout.WEST);
}
public static void main(String[] args)
{
G g = new G();
g.setSize(100, 100);
g.setLocationRelativeTo(null);
g.setVisible(true);
LayoutManager l = g.getLayout();
if (l instanceof FlowLayout)
{
System.out.println("l is FlowLayout\n");
}
if (l instanceof GridLayout)
{
System.out.println("l is GridLayout\n");
}
if (l instanceof BorderLayout)
{
System.out.println("l is BorderLayout\n");
}
}
}
输出结果为:l is BorderLayout。不明白为什么是这个答案,因为之前已经用了setLayout(new GridLayout());
请问这是什么原因?
补充:Java , Java相关