Pergunta

I am new for Java. I need to have MDI in my project. When the user clicks ‘Open’ in menu bar, the internal frame is added and should be full screen on top. I still cannot make it full screen on top when it is open. Also in my code, if I remove the following line from “AddNote” method, I cannot see the internal frame. In logic the JDesktop should add when the form is open.

JDesktopPane desktop = new JDesktopPane();

There is my code:

package MyPackage;

import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import java.io.*;
import java.awt.Dimension;
import java.awt.FlowLayout;



public class MainForm extends JFrame implements ActionListener  {

    private JMenuItem cascade = new JMenuItem("Cascade");
    private  int numInterFrame=0;
    private  JDesktopPane desktop=null;

    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run()
        {

            new MainForm();
        }
    });


}

public  MainForm(){
    super("Example");
    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });

    // Name the JMenu & Add Items
    JMenu menu = new JMenu("File");
    menu.add(makeMenuItem("Open"));
    menu.add(makeMenuItem("Save"));
    menu.add(makeMenuItem("Quit"));

    // Add JMenu bar
    JMenuBar menuBar = new JMenuBar();
    menuBar.add(menu);
    //menuBar.add(menuWindow);
    setJMenuBar(menuBar);


    this.setMinimumSize(new Dimension(400, 500));        
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      

    this.setLayout(new BorderLayout());   
    this.setExtendedState(JFrame.MAXIMIZED_BOTH);
    JDesktopPane desktop = new JDesktopPane();




      this.add(desktop, BorderLayout.CENTER);  
    setVisible(true);


}

public void actionPerformed(ActionEvent e) {

    // Menu item actions
    String command = e.getActionCommand();      

    if (command.equals("Quit")) {
        System.exit(0);
    } else if (command.equals("Open")) {
        // Open menu item action
        JFileChooser fileChooser = new JFileChooser();     
        int returnVal = fileChooser.showOpenDialog(MainForm.this);
        if (returnVal ==  fileChooser.APPROVE_OPTION) {
            numInterFrame=numInterFrame+1;
            File file = fileChooser.getSelectedFile();
            AddNote(numInterFrame);


            // Load file
        } else if (returnVal == JFileChooser.CANCEL_OPTION ) {
            // Do something else
        } 
    } 


     else if (command.equals("Save")) {
        // Save menu item action
        System.out.println("Save menu item clicked");
    }
}

private JMenuItem makeMenuItem(String name) {
    JMenuItem m = new JMenuItem(name);
    m.addActionListener(this);
    return m;
}

    private void AddNote(int index){



       JDesktopPane desktop = new JDesktopPane();
        JInternalFrame internalFrame = new JInternalFrame("PDFAnnotation" + index, true, true, true, true);


       internalFrame.setBounds(0, 0, 600, 100);

        desktop.add(internalFrame);  

       PDFPanel p=new PDFPanel();
        JPanel e =p.getJPanel(); 
        internalFrame.add(e, BorderLayout.CENTER); 
        internalFrame.setVisible(true);
       this.add(desktop, BorderLayout.CENTER);

      /* Dimension size = desktop.getSize();

          int w = size.width ; 
          int h = size.height ; 
          int x=0;
          int y=0;
        desktop.getDesktopManager().resizeFrame(internalFrame, x, y, w, h);*/



    }

}
Foi útil?

Solução

There are a number problems, which are compounded based on the fact that you are shadowing the main desktop variable...

You declare an instance field....

private JDesktopPane desktop = null;

But in your constructor, you declare it again...

JDesktopPane desktop = new JDesktopPane();

This means that the instance field remains null. Instead, in you constructor, you should be using the instance field, for example...

desktop = new JDesktopPane();

This means that in your AddNote method, you can get rid of the creation of yet another JDesktopPane...

//JDesktopPane desktop = new JDesktopPane();
JInternalFrame internalFrame = new JInternalFrame("PDFAnnotation" + index, true, true, true, true);

internalFrame.setBounds(0, 0, 600, 100);

desktop.add(internalFrame);

PDFPanel p = new PDFPanel();
JPanel e = p.getJPanel();
internalFrame.add(e, BorderLayout.CENTER);
internalFrame.setVisible(true);
//this.add(desktop, BorderLayout.CENTER);

Also, because you're using...

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

You won't need

addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
});

You should also have a read of Code Conventions for the Java Programming Language

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top