0

谁能告诉我如何在java中做这个自定义布局?尝试在北 jpanel 网格布局中添加菜单栏,在中间 jpanel 网格布局中添加按钮+图形,然后将它们添加到具有边框布局的框架中,但没有显示。提前致谢

在此处输入图像描述

class {
JFrame frame ;

static JGraph jgraph ;

final mxGraph graph = new mxGraph() ;
JPanel jpanel = new JPanel() ;
...



public void GraphD() {


JPanel middlePanel = new JPanel (new GridLayout (2, 0));
                middlePanel.add(graphComponent);

frame.add (middlePanel, BorderLayout.CENTER); }

public void imgbtn() {  




                 JPanel middlePanel = new JPanel (new GridLayout (2, 0));



                JButton imgbtn = new JButton("Export as Image") ;





                middlePanel.add(imgbtn);
frame.add (middlePanel, BorderLayout.CENTER);

public void Menu()  {

                    JPanel northPanel = new JPanel (new GridLayout (1, 0));

                    JMenuBar menuBar;
                    JMenu menu, submenu;
                    JMenuItem menuItem;

                    menuBar = new JMenuBar();
                    menu = new JMenu("A Menu");
                    menu.setMnemonic(KeyEvent.VK_A);
                    menu.getAccessibleContext().setAccessibleDescription(
                            "The only menu in this program that has menu items");
                    menuBar.add(menu);
                    menuItem = new JMenuItem("Option A",
                             KeyEvent.VK_T);
             menuItem.setAccelerator(KeyStroke.getKeyStroke(
            KeyEvent.VK_1, ActionEvent.ALT_MASK));
           menuItem.getAccessibleContext().setAccessibleDescription(
            "This doesn't really do anything");
              menu.add(menuItem);
              menu.addSeparator();
              submenu = new JMenu("A submenu");
              submenu.setMnemonic(KeyEvent.VK_S);

              menuItem = new JMenuItem("An item in the submenu");
              menuItem.setAccelerator(KeyStroke.getKeyStroke(
                      KeyEvent.VK_2, ActionEvent.ALT_MASK));
              submenu.add(menuItem);

              menuItem = new JMenuItem("Another item");
              submenu.add(menuItem);
              menu.add(submenu);


              northPanel.add(menuBar);
              frame.add (northPanel, BorderLayout.CENTER);
                }


        public static void main(String[] args)
            {


                GUIquery frame = new GUIquery();
                frame.setLayout (new BorderLayout ());
                frame.pack();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(400, 320);
                frame.setVisible(true);


            }

}
4

1 回答 1

0

忘记添加这两行。谢谢

menuBar.add(菜单); 返回菜单栏;

于 2013-09-15T20:56:35.857 回答