radrow
radrow

Reputation: 7149

Java - placing items in JPanel like in JFrame

I try to do simple game, made of few 'screens' (menu, options, etc), which could work by displaying and hiding several JPanels. When I add some stuff on each panel and run the program, the only thing that appears is empty JFrame. I've made id looking like this:

public class Frame extends JFrame{
JPanel panel1 = new JPanel();
JFrame frame = new JFrame("halo");
JButton button = new Button();
int WIDTH=600,HEIGHT=600;

public Frame(){

frame.add(game);
frame.setSize(WIDTH, HEIGHT);
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.add(panel1);
panel1.setBounds(0,0,WIDTH,HEIGHT)
panel1.add(button);
button.setBounds(10,10,30,30);
}}

Everything essential is imported and declared, it's just 'simpled' version. What's wrong with it?

Upvotes: 0

Views: 556

Answers (3)

MadProgrammer
MadProgrammer

Reputation: 347314

JFrame uses a BorderLayout by default, this means that only one component can occupy each of the five available positions that BorderLayout managers.

By default, if you don't specify where you want the component to be, it will be added to the CENTRE position

You should break your UI down into logical units and use JPanels to manage them

You can use a combination of panels and layouts to generate complex UIs

You should also consider using a CardLayout to allow you to switch between base views

You should also call setVisible on the JFrame last, after you've established the basic UI, otherwise you'll need to call revalidate and and repaint to update the UI

You should avoid using setBounds/setSize/setLocation on components and rely on the use of layout managers, as they are designed to manage the differences in rendering pipelines that occurs across multiple different operating systems and hardware platforms

Upvotes: 1

Mshnik
Mshnik

Reputation: 7042

Setting a layout manager and packing after adding elements should fix the issue. Also it's unclear why you're referring to frame when you're constructing this. Try this version:

public class Frame extends JFrame{
    JPanel panel1 = new JPanel();
    JButton button = new Button();
    int WIDTH=600,HEIGHT=600;

    public Frame(){
    setTitle("Halo");
    setLayout(new BorderLayout());
    add(panel1, BorderLayout.CENTER);

    panel1.setBounds(0,0,WIDTH,HEIGHT);
    panel1.setLayout(new BorderLayout());
    panel1.add(button, BorderLayout.SOUTH);
    button.setBounds(10,10,30,30);

    pack();
    setVisible(true);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

Upvotes: 2

Danny Daglas
Danny Daglas

Reputation: 1491

A few things:

  • Using the classname Frame may conflict with java.awt.Frame.
  • You have to set the frame visible by calling frame.setVisible(true); Before that, call frame.pack();
  • You are subclassing JFrame, but also declaring and preparing a different JFrame. Eliminate one of them.
  • Add the components to the content pane of the JFrame: frame.getContentPane().add(subcomponent);

Upvotes: 5

Related Questions