我正在尝试在java中创建一个GUI,但是JMenuBar给了我两天的艰难时间.有人可以告诉我为什么它没有出现?
import java.awt.*; import javax.swing.*; import javax.swing.JPanel; import java.awt.event.*; import java.awt.image.*; import java.io.*; import java.util.Arrays; import javax.imageio.ImageIO; public class selectionFrame extends JFrame { Font name; Font title; public void setup() //can't use constructer because this isn't given a size until after it is constructed. { //getContentPane().add(menuBar); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().setLayout(new FlowLayout()); //getContentPane().add(j); setJMenuBar(createMenuBar()); //getContentPane().add(createMenuBar()); } public JMenuBar createMenuBar() { JMenuBar menuBar; JMenu m_file; JMenuItem mi_save; JMenuItem mi_load; JMenu m_edit; JMenuItem mi_tileHeight; JMenuItem mi_tileWidth; menuBar = new JMenuBar(); m_file = new JMenu("File"); m_edit = new JMenu("Edit"); mi_save = new JMenuItem("Save file", KeyEvent.VK_S); mi_load = new JMenuItem("Load file", KeyEvent.VK_L); mi_tileHeight = new JMenuItem("Set tile height", KeyEvent.VK_H); mi_tileWidth = new JMenuItem("Set tile width", KeyEvent.VK_W); menuBar.add(m_file); m_file.add(mi_save); m_file.add(mi_load); menuBar.add(m_edit); m_edit.add(mi_tileHeight); m_edit.add(mi_tileWidth); return menuBar; } public static void main(String[] args) //run { selectionFrame sel = new selectionFrame(); sel.setLocationRelativeTo(null); sel.setSize((int) 400 + (sel.getInsets().left + sel.getInsets().right),(int) 400 + (sel.getInsets().top + sel.getInsets().bottom)); sel.setVisible(true); sel.setTitle("Tiles/Meta"); sel.setResizable(false); sel.setFocusable(true); sel.getContentPane().setSize(sel.getSize()); sel.setLocation((int) sel.getX() - (sel.getWidth()/2),(int) sel.getY() - (sel.getHeight()/2)); sel.setup(); sel.repaint(); } }
John Gardner.. 5
那里有很多额外的代码.
public class SelectionFrame extends JFrame { Font name; Font title; public SelectionFrame() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setJMenuBar(createMenuBar()); } public JMenuBar createMenuBar() { JMenuBar menuBar; JMenu m_file; JMenuItem mi_save; JMenuItem mi_load; JMenu m_edit; JMenuItem mi_tileHeight; JMenuItem mi_tileWidth; menuBar = new JMenuBar(); m_file = new JMenu("File"); m_edit = new JMenu("Edit"); mi_save = new JMenuItem("Save file", KeyEvent.VK_S); mi_load = new JMenuItem("Load file", KeyEvent.VK_L); mi_tileHeight = new JMenuItem("Set tile height", KeyEvent.VK_H); mi_tileWidth = new JMenuItem("Set tile width", KeyEvent.VK_W); menuBar.add(m_file); m_file.add(mi_save); m_file.add(mi_load); menuBar.add(m_edit); m_edit.add(mi_tileHeight); m_edit.add(mi_tileWidth); return menuBar; } public void main( String args[] ) { SelectionFrame sel = new SelectionFrame(); sel.setLocationRelativeTo(null); sel.setSize(400 + (sel.getInsets().left + > sel.getInsets().right), 400 + (sel.getInsets().top + sel.getInsets().bottom)); sel.setTitle("Tiles/Meta"); sel.setResizable(false); sel.setFocusable(true); sel.getContentPane().add( new JLabel( "Content", SwingConstants.CENTER), BorderLayout.CENTER ); sel.setLocation(sel.getX() - (sel.getWidth() / 2), sel.getY() - > (sel.getHeight() / 2)); sel.setVisible(true); } }
这显示了一个菜单栏和一切.如果您将内容添加到内容窗格的CENTER(默认情况下为边框布局),则中心会自动填充整个内容区域,您无需调整任何内容.
这显示为带有菜单栏的窗口,一切正常.
你在做什么平台?我在Vista上,我得到了我期望看到的东西.
那里有很多额外的代码.
public class SelectionFrame extends JFrame { Font name; Font title; public SelectionFrame() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setJMenuBar(createMenuBar()); } public JMenuBar createMenuBar() { JMenuBar menuBar; JMenu m_file; JMenuItem mi_save; JMenuItem mi_load; JMenu m_edit; JMenuItem mi_tileHeight; JMenuItem mi_tileWidth; menuBar = new JMenuBar(); m_file = new JMenu("File"); m_edit = new JMenu("Edit"); mi_save = new JMenuItem("Save file", KeyEvent.VK_S); mi_load = new JMenuItem("Load file", KeyEvent.VK_L); mi_tileHeight = new JMenuItem("Set tile height", KeyEvent.VK_H); mi_tileWidth = new JMenuItem("Set tile width", KeyEvent.VK_W); menuBar.add(m_file); m_file.add(mi_save); m_file.add(mi_load); menuBar.add(m_edit); m_edit.add(mi_tileHeight); m_edit.add(mi_tileWidth); return menuBar; } public void main( String args[] ) { SelectionFrame sel = new SelectionFrame(); sel.setLocationRelativeTo(null); sel.setSize(400 + (sel.getInsets().left + > sel.getInsets().right), 400 + (sel.getInsets().top + sel.getInsets().bottom)); sel.setTitle("Tiles/Meta"); sel.setResizable(false); sel.setFocusable(true); sel.getContentPane().add( new JLabel( "Content", SwingConstants.CENTER), BorderLayout.CENTER ); sel.setLocation(sel.getX() - (sel.getWidth() / 2), sel.getY() - > (sel.getHeight() / 2)); sel.setVisible(true); } }
这显示了一个菜单栏和一切.如果您将内容添加到内容窗格的CENTER(默认情况下为边框布局),则中心会自动填充整个内容区域,您无需调整任何内容.
这显示为带有菜单栏的窗口,一切正常.
你在做什么平台?我在Vista上,我得到了我期望看到的东西.