有人能告诉我如何在java中进行这种自定义布局吗? 尝试在北jpanel网格布局中添加菜单栏,在中间jpanel网格布局中添加按钮+图形,然后将它们添加到具有borderlayout的框架,但没有显示。 提前致谢
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);
}
}
答案 0 :(得分:0)
忘了添加这2行。感谢
menuBar.add(菜单); return menuBar;