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 FlowLayout\n");
}
if (l instanceof BorderLayout)
{
System.out.println("l is FlowLayout\n");
}
}}运行结果是:l is FlowLayout。
但明明用了setLayout(new GridLayout());但为什么结果是那样的?
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 FlowLayout\n");
}
if (l instanceof BorderLayout)
{
System.out.println("l is FlowLayout\n");
}
}}运行结果是:l is FlowLayout。
但明明用了setLayout(new GridLayout());但为什么结果是那样的?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货