如何使jframe中的jpanel可见 [英] how to make jpanel in jframe visible

查看:213
本文介绍了如何使jframe中的jpanel可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试用Java编写一个小游戏菜单.我认为最好有一个Window类(扩展JFrame),然后在其中为不同的屏幕(Menue,Game,GameOver等)放置一个JPanel. 如果我将按钮和其他东西直接放在JFrame中,则一切都正确显示了,但是当我尝试将JPanel放入JFrame中时,它将无法正常工作.这是代码:

I try to write a menue for a little game in Java. I thought it would be a good idea to have a Window class (extending JFrame) and then put a JPanel in it for the different Screens (Menue, Game, GameOver etc) If I put the buttons and stuff directly in the JFrame everything is shwown correct, but when I try to put a JPanel into the JFrame it doesn't work. Here is the code:

   public class Window extends JFrame{



    private final int WIDTH = 800;
    private final int HEIGTH = 600;
    private final int QUADRAT = 50;

    JButton startButton;
    JButton exitButton;
    JButton anleitungButton;
    JLabel gameTitle;

    public Window() {
        super("Study Run");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(null);
        setSize(WIDTH, HEIGTH);
        setResizable(false);
        getContentPane().add(new MenuePanel());
        setVisible(true);
        setLocationRelativeTo(null);

    }

这是我的小组:

public class MenuePanel extends JPanel{

JButton startButton;
JButton exitButton;
JButton anleitungButton;
JLabel gameTitle;


public MenuePanel() {
    super();
    setBackground(Color.CYAN);

    gameTitle = new JLabel("StudyRun", SwingConstants.CENTER);
    gameTitle.setBounds(200, 25, 400, 75);
    gameTitle.setFont(new Font("Arial", Font.ITALIC, 36));
    add(gameTitle);

    startButton = new JButton("start");
    startButton.setBounds(325, 125, 150, 50);
    add(startButton);
    anleitungButton = new JButton("anleitung");
    anleitungButton.setBounds(325, 200, 150, 50);
    add(anleitungButton);
    exitButton = new JButton("exit");
    exitButton.setBounds(325, 450, 150, 50);
    add(exitButton);

    CloseListener closeListener = new CloseListener();
    StartListener startListener = new StartListener();
    AnleitungListener anleitungListener = new AnleitungListener();
    startButton.addActionListener(startListener);
    anleitungButton.addActionListener(anleitungListener);
    exitButton.addActionListener(closeListener);
}

我在网上找到的唯一帮助是,在将框架设置为可见之前,我需要添加面板.那没用.将pack()或revalidate()放在代码中的任何地方也不起作用.将面板设置为不透明或可见也无济于事.我不知道还能尝试什么?!

The only help I found online was, that I needed to add the panel before I set the frame visible. That didn't work. Putting pack() or revalidate() anywhere in the code didn't work either. Also setting the Panel on opaque or visible didn't do anything. I don't know what else to try?!

推荐答案

您的问题在这里:

setLayout(null);

使用空布局时,由编码人员完全负责所有添加组件的位置和大小.您添加的组件没有大小,因此默认为0、0.

When you use null layouts, you the coder are completely responsible for the location and size of all added components. Your added component has no size and so defaults to 0, 0.

一个(不好的)解决方案:给MenuePanel一个大小或界限

A (bad) solution: give the MenuePanel a size or bounds

一个更好的解决方案:学习和使用布局管理器(正如您最确定的搜索已经告诉您的那样).

A much better solution: learn and use the layout managers (as all your searches most assuredly already told you).

这篇关于如何使jframe中的jpanel可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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