[英]JMenuBar won't show up in JFrame
我知道这个问题已经问了很多,但似乎对我没有任何帮助,所以我会再问一次。 我试图获得一个JMenuBar和一个JMenu,以显示在扩展JFrame的Window类中。 这是我的相关代码:
public class Window extends JFrame {
//class variables
JMenuBar menuBar;
JMenu menu;
Window() throws IOExcpetion {
menuBar = new JMenuBar();
menu = new JMenu("A Menu");
menuBar.add(menu);
this.setJMenuBar(menuBar);
this.add(menuBar); //I've tried with and without this
menu.setVisible(true);
menuBar.setVisible(true);
this.setVisible(true);
while(true) {
repaint(); //my paint method doesn't touch the JMenuBar or JMenu
}
}
杀...
while(true) {
repaint(); //my paint method doesn't touch the JMenuBar or JMenu
}
这阻塞了事件调度线程,使系统无法绘制任何东西……
也...
menu.setVisible(true);
menuBar.setVisible(true);
默认情况下,Swing组件是可见的,因此以上内容毫无意义,我知道您在抓碎面包屑,但是您应该提防,这很少出现问题
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestWindow extends JFrame {
//class variables
JMenuBar menuBar;
JMenu menu;
TestWindow() throws IOException {
menuBar = new JMenuBar();
menu = new JMenu("A Menu");
menuBar.add(menu);
this.setJMenuBar(menuBar);
// this.add(menuBar); //I've tried with and without this
// menu.setVisible(true);
// menuBar.setVisible(true);
this.setVisible(true);
// while (true) {
// repaint(); //my paint method doesn't touch the JMenuBar or JMenu
// }
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
TestWindow frame = new TestWindow();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.