简体   繁体   English

Java JFrame向其添加canvas和jpanel

[英]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. 我在运行时将其添加到框架canvas和jpanel中。 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? 但是删除"frame.add(panel)将使其恢复正常大小。我错过了什么吗?

in case why im using jpanel and canvas. 以防为什么我使用jpanel和canvas。 well im using canvas because i use bufferstategy for the drawing graphic, and i need jpanel to add buttons and other things too. 我正在使用画布,因为我使用bufferstategy绘制图形,并且我也需要jpanel添加按钮和其他东西。

  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. 当您以默认方式向使用BorderLayout的任何容器(例如JFrame的容器)添加两个组件时,默认情况下,这两个组件都将添加到BorderLayout.CENTER位置,第二个组件覆盖第一个,因此JPanel覆盖了Canvas,并且由于不显示“画布”,因此将忽略其首选尺寸。
  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. 相反,您将想找出要相对于彼此的组件的确切位置,特别是JPanel,Canvas以及它们在JFrame中的放置方式,然后利用布局管理器发挥其优势,而不必与布局管理器抗衡代码目前可以。
  3. whatever you do, avoid the null layout like the plague. 无论您做什么,都要避免像瘟疫这样的null布局。
  4. It's usually a bad idea to mix AWT and Swing components. 混合使用AWT和Swing组件通常是一个坏主意。 Are you absolutely positive that you need to use Canvas? 绝对肯定需要使用Canvas吗? JPanels are double buffered by default and that usually smooths out any animation if that's your goal. JPanels默认情况下是双缓冲的,如果这是您的目标,通常可以平滑任何动画。

I must say, It would have been simpler if you had just extended JFrame unless you want to extend something else. 我必须说,如果您只是扩展JFrame除非您想扩展其他内容,否则会更简单。 You need to understand that for code readability and reuseability, you need to follow conventional Java rules and best practices. 您需要了解,为了使代码具有可读性和可重用性,您需要遵循常规的Java规则和最佳实践。

@Hovercraft Full Of Eels has explained all you need above. @Hovercraft Full Of Eels已经在上面解释了您所需要的。 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 . FlowLayout可能是Java中最简单的布局管理器,但与GridLayoutGrdiBagLayout相比,它并不是那么强大。 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();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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