При настройке JMenu я пытаюсь создать меню в верхней части окна макета, которое не является списком - PullRequest
0 голосов
/ 28 февраля 2020

Это то, что я использовал для настройки только меню, я хотел выяснить это до того, как закончил остальную часть своего приложения.

import java.awt.*;
import java.awt.Event.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.event.*;

public class Layout_Manager extends JPanel{
    //instance variables for panels
    private JPanel panel1;
    private JPanel panel2;
    private JPanel panel3;
    private JPanel panel4;
    private JPanel panel5;
    private JPanel panel6;
    //instance variables for help and shapes tab
    private JMenu shapesMenu;
    private JMenu helpMenu;
    private JMenuBar Bar1;
    private JMenuItem menuItem;
    private JMenuItem CircleMenuItem;
    private JMenuItem RectangleMenuItem;
    private JMenuItem SquareMenuItem;
    private JMenuItem LineMenuItem;
    Layout_Manager(){
        //set up panel
        panel1 =new JPanel(new FlowLayout(FlowLayout.LEFT));
        //Create MenuBar and add menu to it
        Bar1 = new JMenuBar();
        shapesMenu = new JMenu("Shapes");
        CircleMenuItem = new JMenuItem("Circle");
        CircleMenuItem.setAccelerator(KeyStroke.getKeyStroke(ActionEvent.CTRL_MASK,KeyEvent.VK_C));
        RectangleMenuItem = new JMenuItem("Rectangle");
        RectangleMenuItem.setAccelerator(KeyStroke.getKeyStroke(ActionEvent.CTRL_MASK, KeyEvent.VK_R)); 
        SquareMenuItem = new JMenuItem("Square");
        SquareMenuItem.setAccelerator(KeyStroke.getKeyStroke(ActionEvent.CTRL_MASK,KeyEvent.VK_S));
        LineMenuItem = new JMenuItem("Line");
        LineMenuItem.setAccelerator(KeyStroke.getKeyStroke(ActionEvent.CTRL_MASK,KeyEvent.VK_L));
        shapesMenu.add(CircleMenuItem,RectangleMenuItem);
        shapesMenu.add(SquareMenuItem,LineMenuItem);
        JMenu add = Bar1.add(shapesMenu);
        panel1.add(Bar1);
        panel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        panel3= new JPanel(new FlowLayout(FlowLayout.LEFT));
        panel4= new JPanel(new FlowLayout(FlowLayout.LEFT));
        panel5 = new JPanel(new GridLayout(5,1));
        panel5.add(panel1);
        panel5.add(panel2);
        panel5.add(panel3);
        panel5.add(panel4);

        add(panel5);

}           
}

Я добавил другие панели с намерением других панели помогают формировать менеджер компоновки, но я не могу понять, почему он не создает меню ????

это тоже мой тестер, я следовал примерам, которые я использовал до того, как это сработало, вот почему это бросает меня за все oop.


import javax.swing.*;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author ethubbard
 */
public class GUI_ShapeTester {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        JFrame frame = new JFrame("Layout Manager");
        Layout_Manager layout = new Layout_Manager();
        frame.add(layout);
        frame.pack();
        frame.setVisible(true);
        frame.setLocation(null);
    }

}

Я думал, что это все, что мне нужно было сделать, чтобы создать GUI и иметь только строку меню вверху, ясно, что мне не хватает другого компонента, но я не могу понять, что.

1 Ответ

0 голосов
/ 28 февраля 2020

Вы неправильно строите свои меню / пункты меню. Также JMenuBar следует добавить к JFrame методом setJMenuBar(). Ниже приведен исправленный код вашего конструктора Layout_Manager:

 Layout_Manager(){
        //set up panel
        panel1 =new JPanel(new FlowLayout(FlowLayout.LEFT));
        //Create MenuBar and add menu to it
        Bar1 = new JMenuBar();
        shapesMenu = new JMenu("Shapes");
        CircleMenuItem = new JMenuItem("Circle");
        CircleMenuItem.setAccelerator(KeyStroke.getKeyStroke(ActionEvent.CTRL_MASK,KeyEvent.VK_C));
        RectangleMenuItem = new JMenuItem("Rectangle");
        RectangleMenuItem.setAccelerator(KeyStroke.getKeyStroke(ActionEvent.CTRL_MASK, KeyEvent.VK_R)); 
        SquareMenuItem = new JMenuItem("Square");
        SquareMenuItem.setAccelerator(KeyStroke.getKeyStroke(ActionEvent.CTRL_MASK,KeyEvent.VK_S));
        LineMenuItem = new JMenuItem("Line");
        LineMenuItem.setAccelerator(KeyStroke.getKeyStroke(ActionEvent.CTRL_MASK,KeyEvent.VK_L));
        shapesMenu.add(CircleMenuItem);     // corrected
        shapesMenu.add(RectangleMenuItem);  // corrected
        shapesMenu.add(SquareMenuItem);     // corrected
        shapesMenu.add(LineMenuItem);       // corrected
        Bar1.add(shapesMenu);               // corrected
        panel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        panel3= new JPanel(new FlowLayout(FlowLayout.LEFT));
        panel4= new JPanel(new FlowLayout(FlowLayout.LEFT));
        panel5 = new JPanel(new GridLayout(5,1));
        panel5.add(panel1);
        panel5.add(panel2);
        panel5.add(panel3);
        panel5.add(panel4);        
        add(panel5);      
        JFrame frame = new JFrame("Layout Manager");   //create JFrame
        frame.add(this);
        frame.setJMenuBar(Bar1);                       // add menu bar built earlier
        frame.pack();
        frame.setVisible(true);
}  

Затем из вашего класса GUI_ShapeTester вы можете просто создать объект Layout_Manager, например:

    public static void main(String[] args) {
        new Layout_Manager();
    } 
...