Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 带有图标(gif)的布局管理器_Java_Swing_Layout Manager - Fatal编程技术网

Java 带有图标(gif)的布局管理器

Java 带有图标(gif)的布局管理器,java,swing,layout-manager,Java,Swing,Layout Manager,基本上,我正在尝试添加一个主屏幕,包括4个按钮、3个难度按钮和一个播放按钮。我将按钮添加到JPanel,并将JPanel添加为中心的BoxLayout。为什么按钮仍然一直向右移动?将JLabel的图标设置为on并将其添加到主屏幕JPanel是否可能会扰乱组件流?我希望难度按钮位于gif的顶部,播放按钮位于底部。谢谢你的帮助 //container snake = new JFrame(); snake.setLayout(new BorderLayout()); //

基本上,我正在尝试添加一个主屏幕,包括4个按钮、3个难度按钮和一个播放按钮。我将按钮添加到JPanel,并将JPanel添加为中心的BoxLayout。为什么按钮仍然一直向右移动?将JLabel的图标设置为on并将其添加到主屏幕JPanel是否可能会扰乱组件流?我希望难度按钮位于gif的顶部,播放按钮位于底部。谢谢你的帮助

 //container
    snake = new JFrame();
    snake.setLayout(new BorderLayout());

    //home screen panel
    homeScreen = new JPanel();
    homeScreen.setLayout(new BoxLayout(homeScreen, BoxLayout.X_AXIS));
    homeScreen.setPreferredSize(new Dimension(320, 320));

    JLabel bg = new JLabel();
    ImageIcon icon = new ImageIcon("HomeBG.gif");
    icon.getImage().flush();
    bg.setIcon(icon);
    homeScreen.add(bg);

    easy = new JButton("Easy");
    medium = new JButton("Medium");
    hard = new JButton("Hard");
    play = new JButton("Play");


   //button listeners code here

    homeScreen.add(easy);
    homeScreen.add(medium);
    homeScreen.add(hard);
    homeScreen.add(play);

    snake.add(homeScreen, BorderLayout.CENTER);
    snake.setTitle("Snake Game");
    snake.pack();
    snake.setVisible(true);
    snake.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

您需要更改代码,如下所示

snake = new JFrame();
snake.setLayout(new BorderLayout());

//home screen panel
homeScreen = new JPanel(new BorderLayout());
//homeScreen.setLayout(new BoxLayout(homeScreen, BoxLayout.X_AXIS));
homeScreen.setPreferredSize(new Dimension(320, 320)); // probably you need to remove this line!

JLabel bg = new JLabel();
ImageIcon icon = new ImageIcon("HomeBG.gif");
icon.getImage().flush();
bg.setIcon(icon);
homeScreen.add(bg);

easy = new JButton("Easy");
medium = new JButton("Medium");
hard = new JButton("Hard");
play = new JButton("Play");


//button listeners code here
JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
buttonsPanel.add(easy);
buttonsPanel.add(medium);
buttonsPanel.add(hard);
buttonsPanel.add(play);

homeScreen.add(buttonsPanel, BorderLayout.NORTH);

snake.add(homeScreen, BorderLayout.CENTER);
snake.setTitle("Snake Game");
snake.pack();
snake.setVisible(true);
snake.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

我会使用复合布局。将标高按钮放在a(a中的面板)
FlowLayout
。将播放按钮置于第二个
流程布局中。将这些面板添加到
边框布局的
页面开始
页面结束
。将包含GIF的标签添加到相同边框布局的
中心

顺便说一句-级别按钮应该是单选按钮(在按钮组中-BNI)


1) 为了更快地获得更好的帮助,请发布一个or。2) 以最小尺寸提供ASCII艺术或GUI预期布局的简单绘图,如果可以调整大小,则提供更大的宽度和高度。最后!这会将所有按钮添加到游戏顶部。我相信我能从这里接受。非常感谢你!我总是喜欢定尺寸。我认为这是一件好事。否则它将是其他大小。“我总是设置PreferredSize。”如果需要,可以覆盖它,但不要设置它。“我认为这是一件好事。”人们的想法无法解释。。
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class LayoutManagersWithIcon {

    private JComponent ui = null;

    LayoutManagersWithIcon() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new BorderLayout(4,4));
        ui.setBorder(new EmptyBorder(4,4,4,4));

        JPanel levelPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
        ui.add(levelPanel, BorderLayout.PAGE_START);
        levelPanel.add(new JRadioButton("Easy"));
        levelPanel.add(new JRadioButton("Medium"));
        levelPanel.add(new JRadioButton("Hard"));

        JPanel startPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
        ui.add(startPanel, BorderLayout.PAGE_END);
        startPanel.add(new JButton("Play"));

        JLabel label = new JLabel(new ImageIcon(
                new BufferedImage(400, 100, BufferedImage.TYPE_INT_RGB)));
        ui.add(label);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                LayoutManagersWithIcon o = new LayoutManagersWithIcon();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}