简体   繁体   中英

Java jframe adding canvas and jpanel to it

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.

  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.

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM