java jframe中添加组件_java的;在自定义位置添加组件的基本方法(jframe)

如果你还没有尝试过null布局,那么看看这段代码,可能会有所帮助

public static void main(String[] args) {

SwingUtilities.invokeLater(NullLayout::new);

}

NullLayout() {

JFrame frame = new JFrame("Basket Game");

JPanel mainPanel = new JPanel();

mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

for (int i = 0; i < 4; i++) {

JPanel strip = new JPanel();

strip.setMaximumSize(new Dimension(Integer.MAX_VALUE, 50));

strip.setBorder(BorderFactory.createTitledBorder("Strip " + i));

strip.add(new JLabel("Strip " + i));

mainPanel.add(strip);

}

JPanel gamearea = new JPanel();

gamearea.setLayout(null);

mainPanel.add(gamearea);

for (int i = 0; i < 5; i++) {

int x = i * 100, y = i * 100;

JPanel basket = new JPanel();

basket.setSize(200, 50);

basket.setLocation(x, y);

basket.setBackground(Color.YELLOW);

basket.add(new JLabel("x = " + x + ", y = " + y));

gamearea.add(basket);

}

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setContentPane(mainPanel);

frame.pack();

frame.setResizable(false);

frame.setSize(600, 600);

frame.setVisible(true);

}}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值