在盒式布局中设置组件的自定义位置 [英] set custom location for a component in box layout

查看:203
本文介绍了在盒式布局中设置组件的自定义位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个框架,在这个框架内,我有一个具有箱形布局的面板,在这个面板内,我还有4个面板.

I have a frame, inside this frame I have a panel with box layout, inside this panel I have 4 more panels.

        mainFrame = new JFrame("Basket Game");
        mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

        mainPanel.add(options);
        mainPanel.add(pname);
        mainPanel.add(info);
        mainPanel.add(gamearea);    

    mainFrame.setContentPane(mainPanel);
    mainFrame.pack();
    mainFrame.getContentPane().setBackground(Color.LIGHT_GRAY);
    mainFrame.setResizable(false);
    mainFrame.setVisible(true);
    mainFrame.setSize(600,600);

表单如下:

前三个面板对我来说还可以.但是对于最后一个面板(黑色),我想添加一些具有自定义坐标的组件.但是,当我尝试使用自定义坐标添加它们时:

The first 3 panel is ok for me. But for the last panel (black one) I want to add some components with custom coordinates. But when I try to add them with custom coordinates:

basket.setLocation(500, 500);
gamearea.add(basket);

它直接位于面板的顶部中心(坐标不会影响其位置)

It goes directly top-center of the panel (coordinates doesn't affect it's location)

当我将gameareI的布局设置为null时,在面板上看不到我的标签.我想我应该为此做些额外的事情.我该怎么办?

When I set gameareIs layout to null I can't see my label on the panel. I think I should do something extra for it. How can I do that?

推荐答案

问题不在于您的布局管理器(null),也没有任何遗漏.问题很简单,就是500x500超出了game area的范围.

The problem is not with your layout manager (null), nor with anything being left out. The problem is simply 500x500 is outside of the bounds of game area.

public class NullLayout {

    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);
    }
}

请注意,未显示400,400处的Basket;它将不在游戏区域的底部.

Notice that the Basket at 400,400 is not showing; it would be off the bottom of the game area.

这篇关于在盒式布局中设置组件的自定义位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆