如何创建菜单« 新建文件 » 在gui java中

kpbwa7wx  于 2021-06-27  发布在  Java
关注(0)|答案(0)|浏览(237)

关闭。这个问题需要细节或清晰。它目前不接受答案。
**想改进这个问题吗?**通过编辑这个帖子来添加细节并澄清问题。

5天前关门了。
改进这个问题
我创建了这个图形界面。我想激活“新建文件”菜单,这样,当您单击此菜单时,将显示一个新页面,而不是一个新窗口。我该怎么做?
我的代码:

public class Fenetre extends JPanel implements MouseListener, MouseMotionListener {

    private static final long serialVersionUID = 1L;
    static final int CREATE_VERTEX = 1;
    static final int START_CREATE_EDGE = 2;
    static final int SERIES_CREATE_EDGE = 3;

    static final int voisin = 5;

    int state;
    Vertex vertexselected;

    private JFrame fen = new JFrame();

    Fenetre(){  

        addMouseListener(this);
        addMouseMotionListener(this);

        fen.setTitle("INTERFACE GRAPHIQUE");
        fen.setSize(600,400);
        fen.setLocationRelativeTo(null);
        fen.add(this, BorderLayout.CENTER);
        fen.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        fen.setJMenuBar(createmenu());

        fen.setVisible(true);

        fen.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent arg0) {
                // TODO Auto-generated method stub
                int clikedButton = JOptionPane.showConfirmDialog(fen, "Voulez-vous enregistrer avant de quitter ?");
                if (clikedButton == JOptionPane.YES_OPTION) {
                    enregistrement();
                    fen.dispose();
                }
                if (clikedButton == JOptionPane.NO_OPTION) {
                    fen.dispose();
                }
            }
        });
    }

    JMenuBar createmenu() { 
        JMenuBar MyMenuBar = new JMenuBar();
        MyMenuBar.setBounds(0, 0, 600, 30);

        JMenu MenuFichier = new JMenu("Fichier");
        MyMenuBar.add(MenuFichier);
        MenuFichier.setMnemonic( 'F' );
        JMenu MenuEdition = new JMenu("Edition");
        MyMenuBar.add(MenuEdition);
        MenuEdition.setMnemonic( 'E' );

        JMenuItem MenuNouveau = new JMenuItem("New file");
        MenuNouveau.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method s/tub
            }

        });
        MenuNouveau.setMnemonic( 'N' );
        MenuNouveau.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, KeyEvent.CTRL_DOWN_MASK));
        MenuNouveau.setIcon(new ImageIcon("icons/new.png"));
        MenuFichier.add(MenuNouveau);

        JMenuItem MenuOuvrir = new JMenuItem("Ouvrir");
        MenuOuvrir.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub

            }
        });
        MenuOuvrir.setMnemonic( 'O' );
        MenuOuvrir.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, KeyEvent.CTRL_DOWN_MASK));
        MenuOuvrir.setIcon(new ImageIcon("icons/open.png"));
        MenuFichier.add(MenuOuvrir);

        JMenuItem MenuEnregistrer = new JMenuItem("Enregistrer");
        MenuEnregistrer.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                enregistrement();
            }
        });
        MenuEnregistrer.setMnemonic( 'E' );
        MenuEnregistrer.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_DOWN_MASK));
        MenuEnregistrer.setIcon(new ImageIcon("icons/save.png"));
        MenuFichier.add(MenuEnregistrer);

        MenuFichier.addSeparator();

        JMenuItem MenuQuitter = new JMenuItem("Quitter");
        MenuQuitter.addActionListener( (e) -> System.exit(0));
        MenuQuitter.setMnemonic( 'Q' );
        MenuQuitter.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, KeyEvent.ALT_DOWN_MASK));
        MenuQuitter.setIcon(new ImageIcon("icons/exit.png"));
        MenuFichier.add(MenuQuitter);

        JMenuItem MenuSommet = new JMenuItem("Creer un sommet");
        MenuSommet.addActionListener( new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                state = CREATE_VERTEX;
            }
        }); 

        MenuEdition.add(MenuSommet);

        JMenuItem MenuArrete = new JMenuItem("Creer un arrete");
        MenuArrete.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                state = START_CREATE_EDGE;
            }
        });
        MenuEdition.add(MenuArrete);

        return MyMenuBar;
    }

    Vertex vertexvoisin(int x, int y) {
        Iterator<Vertex> it = Vertex.iterator();
        while (it.hasNext()) {
            Vertex v = (Vertex) it.next();
            if (Math.abs(x - v.getX()) + Math.abs(y - v.getY()) < voisin)
                return v;
        }
        return null;
    }

    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub
        Vertex a;

        switch(state) {
        case CREATE_VERTEX:
            new Vertex(e.getX(), e.getY());
            repaint();
            return;
        case START_CREATE_EDGE:
            vertexselected = vertexvoisin(e.getX(), e.getY());
            if (vertexselected == null)
                return;
            state = SERIES_CREATE_EDGE;
            return;
        case SERIES_CREATE_EDGE:
            a = vertexvoisin(e.getX(), e.getY());
            if (a == null || a.equals(vertexselected))
                return;
            new Edge(vertexselected, a);
            repaint();
            state = START_CREATE_EDGE;
            return;
        }
    }

    public void mouseDragged(MouseEvent e) {}

    public void mouseMoved(MouseEvent e) {}

    public void mouseClicked(MouseEvent e) {}

    public void mouseEntered(MouseEvent e) {}

    public void mouseExited(MouseEvent e) {}

    public void mouseReleased(MouseEvent e) {}

    public void paint(Graphics gr) {
        super.paint(gr);
        Color color = gr.getColor();

        gr.setColor(Color.black);
        Iterator<Edge> itE = Edge.iterator();
        while (itE.hasNext()) {
            Edge a = itE.next();
            gr.drawLine(a.getOrg().getX(), a.getOrg().getY(), a.getExt().getX(), a.getExt().getY());
        }

        Iterator<Vertex> itV = Vertex.iterator();
        while (itV.hasNext()) {
            Vertex s = itV.next();
            gr.setColor(Color.black);
            gr.fillOval(s.getX() - 5, s.getY() - 5, 10, 10);
            if (s.getEtiquette() != null) {
                gr.setColor(Color.blue);
                gr.drawString(s.getEtiquette(), s.getX() + 5, s.getY() - 5);
            }
        }
        gr.setColor(color);
    }

    private void enregistrement() {
        JFileChooser file = new JFileChooser();
        int res = file.showSaveDialog(this);
        if(res != JFileChooser.APPROVE_OPTION)
            return;
        try {
            PrintStream print = new PrintStream(file.getSelectedFile());
            print.println(Vertex.numberVertex());
            Iterator<Vertex> ItV = Vertex.iterator();
            while(ItV.hasNext()) {
                print.println(ItV.next());
            }

            print.println(Edge.numberEdge());
            Iterator<Edge> ItE = Edge.iterator();
            while(ItE.hasNext()) {
                print.println(ItE.next());
            }

            print.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return;
        }
    }

    public static void main (String[] args) throws UnsupportedLookAndFeelException {
        UIManager.setLookAndFeel(new NimbusLookAndFeel());

        new Fenetre();
    }

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题