how to make jpanel in jframe visible

为君一笑 提交于 2019-12-13 03:46:44

问题


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

    }

And this is my Panel:

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

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?!


回答1:


Your problem is here:

setLayout(null);

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.

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).




回答2:


It's best to remember that Java uses Flowlayout() as a default.

 public Window() {

    setLayout(new FlowLayout());
}

So your basically overwriting the layout to null as explained in the previous answer.Also if you plan to use different class and panels to add to a JFrame from different classes use a getter

class SomePanel{
public JComponent getPanel(){
return panel;
 }
}

Then add to JFrame..

class MyFrame{
add(new SomePanel().getPanel());

}



来源:https://stackoverflow.com/questions/47460521/how-to-make-jpanel-in-jframe-visible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!