簡體   English   中英

為什么我的Jframe無法顯示?

[英]Why won't my Jframe show up?

我將其設置為可見,執行所有需要做的事情,例如大小和布局,並且當我單擊運行時它不會顯示。 有人知道為什么嗎? 我看過我過去做過的其他視圖類,沒有發現任何不同

package model;

import java.awt.*;
import javax.swing.*;
import java.lang.reflect.Method;

@SuppressWarnings("serial")
public class View extends JFrame {
    private static final int FRAME_WIDTH = 1000;
    private static final int FRAME_HEIGHT = 800;
    private static final int FRAME_X_ORIGIN = 250;
    private static final int FRAME_Y_ORIGIN = 250;

    private JButton solveButton;

    private static Controller myController;
    public View(Controller controller)
    {
        myController = controller;
        this.setVisible(true);
        this.setLayout(null);
        this.setSize(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
        Container contentPane;
        this.setTitle("NQueens");

        this.setResizable(false);
        this.setBackground(Color.WHITE);

        solveButton = new JButton("Solve");
        solveButton.setBounds(500,460,80,80);
        this.add(solveButton);
        this.setVisible(true);
    }
}

您正在執行此操作this.setSize(FRAME_X_ORIGIN, FRAME_Y_ORIGIN); 您的意思不是this.setSize(FRAME_WIDTH, FRAME_HEIGHT); 有了它,您的“解決”按鈕就會偏離框架的邊緣。

您需要調用父構造函數。 只需添加

super("My Window Title");

在構造函數的頂部。 而且您有兩次調用setVisible(...)方法。 卸下最前面的一個。 應該像

public View(Controller controller)
{
    super("NQueens");
    myController = controller;

    this.setLayout(null);
    this.setSize(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
    Container contentPane;

    this.setResizable(false);
    this.setBackground(Color.WHITE);

    solveButton = new JButton("Solve");
    solveButton.setBounds(500,460,80,80);
    this.add(solveButton);
    this.setVisible(true);
}

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM