0
【初心者】メニューバーが表示されない
ウィンドウを表示させることは出来たのですが、次にメニューバーを作成しようとしても表示されません。
参考にしたのは以下のサイトです。
https://www.javadrive.jp/tutorial/jmenu/index1.html
ソースコード
Main.java
package practice;
public class Main
{
public static void main(String[] args)
{
Window.Init();
}
}
Window.java
package practice;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class Window
{
static JFrame frame;
private static void MakeWindow()
{
frame = new JFrame("glossary");
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private static void MakeMenu()
{
JMenuBar menubar = new JMenuBar();
JMenu menu1 = new JMenu("Relation");
JMenu menu2 = new JMenu("Index");
menubar.add(menu1);
menubar.add(menu2);
frame.setJMenuBar(menubar);
}
public static void Init()
{
MakeWindow();
MakeMenu();
}
}
最初は、MakeWindow()内で
JFrame frame = new JFrame("glossary");
とし、
MakeMenu()内で
setJMenuBar(menubar);
としていたのですが、「メソッド setJMenuBar(JMenuBar) は型 Window で未定義です」という警告が出たので上記のようにしてみました。
しかし、ブレイクポイントは通過しているようなのですがメニューバーが表示されません。
どうしてかご教授いただければ幸いです。