2016-11-12 50 views
-3

我無法居中我的jdialog在我的jframe中。我試圖做到這一點,但它不起作用:無法將jdialog居中在jframe中

public void open(){ 
     this.setVisible(true); 
     this.setLocationRelativeTo(Main.instance.frame); 
    } 

告訴我如果你需要更多的代碼!謝謝你的幫助!

主要:

public class Main { 

    public static Main instance; 

    public JXFrame frame; 
    public JXPanel panel; 
    public String name = ""; 

    // Windows 
    public WindowCreateProject windowCreateProject; 

    public static void main(String[] args) { 
     instance = new Main(); 
     instance.start(); 
    } 

    public void init() { 
     // Init components 
     Icons.createIcons(); 
     MainMenu.init(); 
     // JXFrame 
     frame = new JXFrame(); 
     panel = new JXPanel(); 
     panel.setBackground(Color.LIGHT_GRAY); 
     panel.setLayout(new CardLayout()); 
     frame.setPreferredSize(new Dimension(1980, 1080)); 
     frame.setDefaultCloseOperation(JXFrame.EXIT_ON_CLOSE); 
     // Add components 
     frame.add(panel); 
     panel.add(MainMenu.menuBar); 
     // JXFrame 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
     frame.setTitle(name); 
     // Create windows 
     windowCreateProject = new WindowCreateProject(); 
     update(); 
    } 

    public void update(){ 
     while(frame.isActive()){ 

     } 
    } 

    public void start() { 
     init(); 
    } 

    public void stop() { 
     System.exit(0); 
    } 

} 

WindowCreateProject:

public class WindowCreateProject extends Window { 

    private static final long serialVersionUID = 1L; 

    public JTextField nameField; 
    public JButton createProject; 
    public JButton cancel; 

    public WindowCreateProject() { 
     super("Create New Project"); 
     // Setup components 
     nameField = new JTextField(); 
     nameField.setSize(100, nameField.getHeight()); 
     createProject = new JButton("Create Project", Icons.ok); 
     cancel = new JButton("Cancel", Icons.cancel); 
     // Add components 
     panel.add(nameField); 
     panel.add(createProject); 
     panel.add(cancel); 
     addActionListeners(); 
    } 

    public void addActionListeners(){ 
     cancel.addActionListener(new ActionListener(){ 
      @Override 
      public void actionPerformed(ActionEvent arg0) { 
       close(); 
      } 
     }); 
    } 

    public void close(){ 
     nameField.setText(""); 
     this.setVisible(false); 
    } 

} 

窗口:

public class Window extends JDialog { 

    private static final long serialVersionUID = 1L; 

    public JPanel panel; 

    public Window(String title) { 
     panel = new JPanel(); 
     this.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); 
     this.setLayout(new GridLayout()); 
     this.setResizable(false); 
     this.setPreferredSize(new Dimension(300, 400)); 
     this.add(panel); 
     this.pack();  
     this.setTitle(title); 
    } 

    public void open(){ 
     this.setVisible(true); 
     this.setLocationRelativeTo(Main.instance.frame); 
    } 

} 

MenuMain: 公共類的MainMenu {

public static JMenuBar menuBar; 
public static JMenu menuProjects; 
public static JMenu menuOptions; 
public static JMenu menuHelp; 

// Projects menu items 
public static JMenuItem createProject; 
public static JMenuItem myProjects; 
public static JMenuItem saveProject; 
public static JMenuItem saveProjectAs; 
public static JMenuItem exportResourcepackTo; 
public static JMenuItem exportResourcepackToMinecraft; 
public static JMenuItem exit; 

public static void init(){ 
    // Init menu 
    menuBar = new JMenuBar(); 
    menuBar.setLayout(new FlowLayout(FlowLayout.LEFT)); 
    menuProjects = new JMenu("Projects"); 
    menuOptions = new JMenu("Options"); 
    menuHelp = new JMenu("Help"); 
    createMenuItems(); 
    // Add components 
    menuBar.add(menuProjects); 
    menuBar.add(menuOptions); 
    menuBar.add(menuHelp); 
    // Add menu items to project menu 
    menuProjects.add(createProject); 
    menuProjects.add(myProjects); 
    menuProjects.add(saveProject); 
    menuProjects.add(saveProjectAs); 
    menuProjects.addSeparator(); 
    menuProjects.add(exportResourcepackTo); 
    menuProjects.add(exportResourcepackToMinecraft); 
    menuProjects.addSeparator(); 
    menuProjects.add(exit); 
    createActionListeners(); 
} 

public static void createMenuItems(){ 
    createProject = new JMenuItem("Create project", Icons.createProject); 
    myProjects = new JMenuItem("My projects", Icons.myProjects); 
    saveProject = new JMenuItem("Save project"); 
    saveProjectAs = new JMenuItem("Save project as"); 
    exportResourcepackTo = new JMenuItem("Export resourcepack to"); 
    exportResourcepackToMinecraft = new JMenuItem("Export resourcepack to minecrat"); 
    exit = new JMenuItem("Exit", Icons.cancel); 
} 

public static void createActionListeners(){ 
    // Project menu 
    createProject.addActionListener(new ActionListener(){ 
     @Override 
     public void actionPerformed(ActionEvent arg0) { 
      Main.instance.windowCreateProject.open(); 
     } 
    }); 
    exit.addActionListener(new ActionListener(){ 
     @Override 
     public void actionPerformed(ActionEvent arg0) { 
      Main.instance.stop(); 
     } 
    }); 
} 

}

+0

'「告訴我你是否需要更多的代碼!」 - 我們需要**少**。您已經發布了大量代碼,與您的問題最爲完全無關,這可能會讓我們陷入不必要的代碼淹沒中,可能會阻止我們看到您的問題並能夠幫助您。 –

+0

請注意,我們不希望看到您的整個程序,特別是長度超過60行的應用程序,而應將您的代碼壓縮到仍然編譯和運行的最小位,並且沒有與您的代碼無關的額外代碼問題,但仍然表明您的問題,換句話說,一個[最小示例程序](http://stackoverflow.com/help/mcve)。 –

+0

請參閱編輯以回答。 –

回答

2

訂單事宜。因此,改變這種:

public Window(String title) { 
    panel = new JPanel(); 
    this.setLocationRelativeTo(Main.instance.frame); 
    this.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); 
    this.setLayout(new GridLayout()); 
    this.setResizable(false); 
    this.setPreferredSize(new Dimension(300, 400)); 
    this.pack();  
    this.add(panel); 
    this.setTitle(title); 
} 

這樣:

public Window(String title) { 
    panel = new JPanel(); 

    // nope: 
    // this.setLocationRelativeTo(Main.instance.frame); 

    this.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); 
    this.setLayout(new GridLayout()); 
    this.setResizable(false); 
    this.setTitle(title); 
    this.setPreferredSize(new Dimension(300, 400)); 
    this.add(panel); // add **before** packing 
    this.pack(); // have to be **after** this!! 

    // yep! 
    this.setLocationRelativeTo(Main.instance.frame); 

    // set visible true here 
} 

在你上面的代碼,你仍然使用了錯誤的順序。你有這樣的:

public void open() { 
    this.setVisible(true); 
    this.setLocationRelativeTo(Main.instance.frame); 
} 

當你應該使用這樣的:

public void open() { 
    this.setLocationRelativeTo(Main.instance.frame); // !! 
    this.setVisible(true); 
    // !!this.setLocationRelativeTo(Main.instance.frame); 
} 

而且,它不會出現,你的的JDialog的所有者窗口設置到主JFrame的,並做到這一點,你需要使用合適的超級JDialog構造函數,一個接受JFrame參數的構造函數。