0

The following class creates a window/frame.

public class Window {

private int width, height;
private JFrame frame;
private Canvas canvas;
private String title;
private JButton button;
private JPanel panel;

public Window(String title){

    System.out.println("Initialization Window...");

    this.title = title;

    width = Reference.width;
    height = Reference.height;

    button = new JButton("cool button");

    CreateWindow();
}

private void CreateWindow(){

    frame = new JFrame(title);
    frame.setSize(width, height);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

    panel = new JPanel();
    panel.add(button);

    canvas = new Canvas();
    canvas.setPreferredSize(new Dimension(width, height));
    canvas.setMaximumSize(new Dimension(width, height));
    canvas.setMinimumSize(new Dimension(width, height));
    canvas.setFocusable(false);

    frame.add(canvas);
    frame.add(panel);//my problem is in this line
    frame.pack();
}

i added to the frame canvas and jpanel when i run it. the size of frame is set to very small a size of a button that i have made. but removing "frame.add(panel) will make it back to normal size. did i miss something?

in case why im using jpanel and canvas. well im using canvas because i use bufferstategy for the drawing graphic, and i need jpanel to add buttons and other things too.

HeRoXLeGenD1
  • 27
  • 1
  • 4

2 Answers2

3
  1. When you add two components in a default fashion to any container that uses BorderLayout, such as a JFrame's, both are added by default in the BorderLayout.CENTER postion and the second component covers the first, and so here the JPanel covers the Canvas, and since the Canvas is not displayed, its preferred size is ignored.
  2. You will instead want to figure out exactly where you want components relative to each other, specifically the JPanel, the Canvas and how they're placed in the JFrame, and then use the layout managers to their advantage rather than fighting the layout manager as your code currently does.
  3. whatever you do, avoid the null layout like the plague.
  4. It's usually a bad idea to mix AWT and Swing components. Are you absolutely positive that you need to use Canvas? JPanels are double buffered by default and that usually smooths out any animation if that's your goal.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

I must say, It would have been simpler if you had just extended JFrame unless you want to extend something else. You need to understand that for code readability and reuseability, you need to follow conventional Java rules and best practices.

@Hovercraft Full Of Eels has explained all you need above. All I do here is to set you right by example so there is no need duplicating what he has said. FlowLayout may be the easiest and simplest layout manager in Java but it is not that very powerful compared to say GridLayout or GrdiBagLayout. Here is the code:

public class Window extends JFrame {

    private int width, height;
    private Canvas canvas;
    private String title;
    private JButton button;
    private JPanel panel;

    public Window(String title){
        super( title );
        System.out.println("Initialization Window...");

        this.title = title;
        setLayout( new FlowLayout() );

        //width = Reference.width;
        //height = Reference.height;

        button = new JButton("cool button");

        createWindow();
    }

    private void createWindow(){


        setSize(width, height);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setVisible(true);

        panel = new JPanel();
        panel.add(button);

        canvas = new Canvas();
        canvas.setPreferredSize(new Dimension(200, 200));
        canvas.setMaximumSize(new Dimension(400, 400));
        canvas.setMinimumSize(new Dimension(200, 200));
        canvas.setFocusable(false);

        add(canvas);
        add(panel);//my problem is in this line
        pack();
    }

}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    It's almost always better not to extend JFrame, not unless you are planning on changing the innate behavior of the JFrame, such as override one of its methods. Usually it's better to extend JPanel or have your class produce a JPanel as this gives your code much greater flexibility. – Hovercraft Full Of Eels Jul 25 '16 at 16:38
  • @HovercraftFullOfEels You said "...as this gives your code much greater flexibility". Well I think that is why I love extending JFrame. If I extend JFrame, then I can add multiple JPanels and give them any kind of behaviors I like either individually or by the use of list. The other thing is that I almost always use multiple JFrames in my programs so it is much easier to view them as separate windows. But since I have not tried your view, I will do so and see what I get. Thank you. – funaquarius24 Jul 29 '16 at 00:03
  • But if your GUI classes are be geared towards creating JPanels, then they can then be placed into JFrames or JDialogs, or JTabbedPanes, or swapped via CardLayouts, or placed into other JPanels as a modular component of a complex GUI -- placed wherever and whenever they are needed. – Hovercraft Full Of Eels Jul 29 '16 at 00:26
  • `"The other thing is that I almost always use multiple JFrames in my programs so it is much easier to view them as separate windows."` -- which can be a very frustrating application design for the user who now may have to deal with windows being constantly thrown at them. Please read the link: [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/questions/9554636) -- for more on this. – Hovercraft Full Of Eels Jul 29 '16 at 00:27
  • Last year, someone brought an application he was writing to help him finish it. When I opened the app, I found that he had used the JInternalFrame. I didn't like the result but I wasn't sure if my multiple JFrame would be the solution. I had to spend hours reading that post you had just shared in order to be sure that I am making the right decision. Discussion with the owner was successful. The use of MVC also solved a lot of problems I may have with my program. In conclusion, I found that the problem a person may have with multiple JFrames is is the inability to write a good program. Thanks. – funaquarius24 Jul 29 '16 at 01:12